How To Use Hosts File - A Scenario

Domain Name

Instead of accessing our machines via IP address, we use a user-friendly name. This name + IP mapping is done in Domain Name System. These user-friendly names are called domains. For example,, 

Domain Name to IP Resolution

Domain Name to IP address mapping is added in DNS (Domain Name System). When we try to access a host by hostname or domain name, the name is resolved to IP address using DNS Servers. 
NSLookup stands for Name Server Lookup. It can be used to find IP address against the host/domain name or domain name by IP address. Finding the domain name against an IP address is called Reverse DNS Lookup. It is a very useful command-line utility to resolve DNS issues. Here is an example of how to use it on command prompt.

What is the hosts file?

It is a plain text file. Operating System uses this file to map the hostname to IP address. When we try to access a host by host/domain name, OS first checks if there is an entry for this in the hosts file or not. If it finds a mapping, it uses this mapped IP address for communication with the host machine. Here is an example of how we define entries in this file
Location of hosts file on windows: %SystemRoot%\System32\drivers\etc\host
Location of hosts file on Linux: /etc/hosts
Let see how we can use hosts file in a load balancer scenario to access website from specific server.

Case Study

We have a website which was deployed on a single web server earlier. For scalability purposes, we have deployed it on multiple web servers. The request doesn’t go directly to the web servers now. Accessibility is through Load Balancer now and the domain of the website is mapped with IP address of load balancer server. The request goes to load balancer and it forwards requests to specific web server considering the load of web servers.
Here is a sample configuration for understanding purposes.
Server 1 - Website configuration in Web Server
Website is mapped with IP:
Server 2 - Website configuration in Web Server
Website is mapped with IP:

Load Balancer Configuration

VIP: is mapped with (,
DNS Entry for Website & Load Balancer VIP
IP: Domain Mapped:
Now if we type in browser; Domain to IP resolution happens through DNS and request goes to Load Balancer ( and then considering load/settings, LB forwards request to or web servers.


What if we want to send our requests to a specific web server only for testing purposes?

Solution 1

In load balancer configuration, we can disable mappings of other web servers & keep one mapping enabled. But if our website is in production, the web server may be overloaded and end users may face performance issues. And also it will require a change in load balancer which may impact the overall accessibility of website during production hours.

Solution 2

We can update hosts file on our machine and add mapping of domain and IP of a specific web server in it. After this when we’ll access the website in a browser, it will not go to load balancer but will go directly to specific web server. 
For example, we want to access which is hosted on, we can make the following entry in our hosts file.
Now on command prompt, we can check if it is working or not
It should show Now if we type in the browser, our request will be going directly to web server, not to load balancer.
Another use of hosts file can be when you are working on your local machine and you want to access your working website with a domain name (e.g. instead of localhost. You may add following entry in the hosts file to achieve this. 


Hosts file on our machine allows us to define domain to IP mapping. We can add mappings in this file to access the website from the specific server if website is behind a load balancer. Sometimes, we use this for testing/debugging purposes and sometimes we intentionally route our traffic to specific server to gain performance.

Similar Articles