How to define static hostnames for IP Addresses in Windows (counterpart of Linux`s /etc/hosts file in Windows)?

Problem:

If you want to define a static fully qualified domain name (FQDN) on your windows XP/Server 2003 machine, so that your computer does not contact a DNS server to resolve a particular IP address then you can do so by using the instructions explained below.

Scenario:

This can be useful on a number of situations one such situation is as follows.

Let's say you have a web server hosting www.example.com and you want to add a new machine to this web address for load balancing purposes. Once you are done setting up the new machine you'll test it by using the direct IP address of that machine. You cannot access the new machine using www.example.com because every time you try that your computer will be forwarded to the old machine (whose IP address is stored in the DNS server). In this situation you can use the instructions below to give a static IP address (IP address of the new machine) to www.example.com, so that your computer connects with the new server (when you access it) using www.example.com.

Benefits and Drawbacks:

The lookups are really fast because they use a local file on your computer. You can use this technique to store IP addresses for the most visited domain names. But, I can see two problems with that solution (i) it is not scalable and (ii) the solution is not dynamic (more on this later).

How to define static mappings:

In order to create an IP address(es)-to-hostname(s) map on your local computer you have to make changes to the file C:WINDOWSsystem32driversetchosts. 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 may be inserted on individual lines or following the machine name denoted by a '#' symbol.

Example IP Address to Hostname Entry:

192.168.1.101 www.google.com

Insert the line above to C:WINDOWSsystem32driversetchosts, save the file, and go to www.google.com using a web browser. You'll either see some other website (if 192.168.1.101 is hosting some other/similar website) or the browser will show you an error message (if 192.168.1.101 is not hosting some other/similar website).

Drawbacks in using the hosts file for all (or large no. of) websites:

  • Solution is not scalable:

    You cannot apply this solution to a large number of computers. The problem is that this solution of IP-to-hostname mapping is not scalable because if you have a large number of computers and you have a long list of websites on each computer and if some website's IP address changes then you have to either manually change all files on all computers (too much work in this case) OR you'll have to copy a single modified file to all computers (as the number of websites grow the file will get bigger in size hence eat up resources required to copy the file to all computers). There can be some other solutions to these problems but again there will be potential problems with almost all such solutions.
  • Solution is not dynamic:

    This solution is not dynamic because if some website's IP address changes then that change will not be reflected on your local hosts file automatically. You'll have to check individual entries in your hosts file for a change to find out if the IP-to-hostname map is valid or not.

For the above mentioned reasons (or may be more), we use DNS as the solution. But, you can use the hosts file occassionally to test some server or you can insert entries for the most visited websites like www.google.com, www.slashdot.org etc.

Did this tutorial help a little? How about buy me a cup of coffee?

Buy me a coffee at ko-fi.com

Please feel free to use the comments form below if you have any questions or need more explanation on anything. I do not guarantee a response.

IMPORTANT: You must thoroughy test any instructions on a production-like test environment first before trying anything on production systems. And, make sure it is tested for security, privacy, and safety. See our terms here.