Windows Hosts File

Kailash Chandra Behera | Wednesday, May 06, 2020

Introduction

Recently I was migrating all the web applications, WCF and Web services to Microsoft Azure Pass Environment. Where I had to keep the domain name of applications and services the same as the domain names of applications and services in the current environment (Old server).

Sometimes I had to redirect the request to the new server forcefully for testing because I have mapped single domain names to two different servers and two different IP addresses.

Example: -

Let’s say I have purchased a domain address www.kailashsblogs.com and I have two different host server having IP address like 192.168.1.1 and 192.168.1.2. I hosted my web application on both servers and mapped to the domain name.

When I browse my web application first time then the request should go to the server having IP address 192.168.1.1 then in second browse to 192.168.1.2.

I achieved above with the help of the hosts file available in the windows. Whenever it required to redirect, I used to make an entry in the hosts file and reverting it back when redirection was not required.

In this blog, we are going to discuss the host file and how to make entry to redirect the request to another server.

Getting Started

The Hosts file is used by the operating system to map human-friendly hostnames to numerical Internet Protocol (IP) addresses which identify and locate a host in an IP network. The host file is one of several system resources that address network nodes in a computer network and is a common part of an operating system's IP implementation

The Hosts file contains lines of text consisting of an IP address in the first text field followed by one or more hostnames. Each field is separated by white space (Tabs are often preferred for historical reasons, but spaces are also used). Comment lines may be included, and they are indicated by a hash character (#) in the first position of such lines. Entirely blank lines in the file are ignored.

 192.168.1.2 www.kailashsblogs.com # will redirect to the second server.  

How to Modify Hosts File

We will see here how to add the IP address and hostname in the hosts file to redirect, follow the below steps to modify the hosts file.

  1. Open Notepad with administrator privileges
  2. Browse to C:\Windows\System32\drivers\etc\hosts (Or paste this into the address bar)
  3. Open the file, you may find below text inside it.
  4.  # Copyright (c) 1993-2006 Microsoft Corp.  
     #  
     # This is a sample HOSTS file used by Microsoft TCP/IP for Windows.  
     #  
     # This file contains the mappings of IP addresses to host names. Each  
     # entry should be kept on an individual line. The IP address should  
     # be placed in the first column followed by the corresponding host name.  
     # The IP address and the host name should be separated by at least one  
     # space.  
     #  
     # Additionally, comments (such as these) may be inserted on individual  
     # lines or following the machine name denoted by a '#' symbol.  
     #  
     # For example:  
     #  
     #   102.54.94.97   rhino.acme.com     # source server  
     #    38.25.63.10   x.acme.com       # x client host  
     # localhost name resolution is handle within DNS itself.  
     #    127.0.0.1    localhost  
     #    ::1       localhost   
    
  5. Add your domain and IP details like below
  6.  192.168.1.2 www.kailashsblogs.com # will redirect to the second server.  
    
  7. Once the hosts file is changed, then save the file.
  8. To verify the redirection, open command prompt with administrator privileges.
  9. Type ipconfig/flushdns like below then press enter key.
  10. If you get a success message, then ping to your domain address like below.
  11. If you found a change of IP address, then the task is completed

Reset the Hosts file back to the default

If you find something wrong and you want to reset the hosts file, then to reset the Hosts file back to the default, follow these steps according to your operating system.

  1. Open Notepad with administrator privileges
  2. Copy the following text, and then paste the text into the file:
  3.  # Copyright (c) 1993-2006 Microsoft Corp.  
     #  
     # This is a sample HOSTS file used by Microsoft TCP/IP for Windows.  
     #  
     # This file contains the mappings of IP addresses to host names. Each  
     # entry should be kept on an individual line. The IP address should  
     # be placed in the first column followed by the corresponding host name.  
     # The IP address and the host name should be separated by at least one  
     # space.  
     #  
     # Additionally, comments (such as these) may be inserted on individual  
     # lines or following the machine name denoted by a '#' symbol.  
     #  
     # For example:  
     #  
     #   102.54.94.97   rhino.acme.com     # source server  
     #    38.25.63.10   x.acme.com       # x client host  
     # localhost name resolution is handle within DNS itself.  
     #    127.0.0.1    localhost  
     #    ::1       localhost   
    
  4. Save the file.
  5. Your are done with reset.

Thanks