Step-by-Step: How to write a HOSTS file
December 14, 2003

There are several reasons that you might need to use a HOSTS file.  First let me explain what a HOSTS file does.  A HOSTS file maps an IP address to a domain name or any word you choose on the computer that the HOSTS file resides.  For example, if you type in "loopback" into your browser, it will automatically translate to "127.0.0.1" which is a self reference to the computer you are working on.  The HOSTS file serves as a mini DNS server for each computer.  When your computer tries to resolve a domain name into an IP address, the HOSTS file is first checked and then the DNS server is checked.

There are a few reasons that you may need a HOSTS file.

1.  Your router doesn't support loopback.

This means that from behind the LAN (a computer connected to the router), you cannot connect to the same router coming through the WAN IP, back into the LAN (where the server is), thus the name "loopback".
For example, on a computer behind a router, let's say: 192.168.0.100, you try to access your webserver which has a LAN IP address of 192.168.1.20.  Let's say your WAN IP address is 123.123.123.123.  If you have loopback, you can access the server by typing in the WAN IP of 123.123.123.123 which will get forwarded to the internal LAN IP of the server: 192.168.1.20.  Without loopback, you CAN access the server by the LAN IP, but you CANNOT access the server by the WAN IP or domain name if you are behind the same router as the server is.  Without loopback, you cannot connect from inside the LAN to the WAN, back into the the LAN again.

It's sound complicated, but it really isn't.  The common complaint from people who have routers without loopback is "I can see my website (by domain name) from the WAN, but not on the LAN".

2.  You are setting up your website and don't have your DNS servers configured yet or are waiting for the changes to be propagated throughout the Internet. 
With the HOSTS file, you can still access your webserver using the domain name even though the DNS servers aren't working yet.  But remember, everybody else on the Internet still won't be able to access your site until the DNS servers settings are completely spread throughout the Internet.
3.  You are playing with/testing Host Headers (IIS) or Virtual Hosting (Apache).
A HOSTS file is good If you want to learn how to setup Host Headers or Virtual Hosting without buying the actual domain names and setting up the requisite DNS servers.  You get to practice setting up the webserver. 

If you do already own the domain names, you can use the HOSTS file so that you don't have to wait for the DNS setting to take hold.  Also, if your router doesn't support loopback, using a HOSTS file is going to be the only way you'll be able to access your websites using HOSTS Headers and Virtual Hosting from behind the LAN. 

I would suggest that you only use the HOSTS file to the extent that you have to.  It is always good to see your website as the rest of the world sees it.  If you use a HOSTS file for your website, then you'll never know if your DNS servers are working correctly or not since the HOSTS file is read before the DNS servers are read.  For example, if you are using a HOSTS file and your DNS server went down, you would falsely believe that your website is up because YOU can access it.  However, nobody else could see your website because the DNS servers are down.  So, only use the HOSTS file for the period of time you need them.

Good general rules of use:

1.  Make sure your server has a static IP address.  Otherwise, the server IP address may not match the one listed in your HOSTS file.

2.  If you router doesn't support loopback, then you must use a HOSTS file for all of your websites.  No way around it.  Disadvantage is that you don't know if people on the Internet can actually see your website or not.  You must keep check this yourself from the WAN occasionally to be sure.

3.  If you are using a HOSTS file while you wait for DNS changes to propagate, once you find out that the DNS servers are displaying the correct data, remove the entries from your HOSTS file.  That way you can see your website as the rest of the world sees it.

----------------

Let's play with this thing.  Make sure to backup the HOSTS file before you start messing with it.

Open up Notepad.exe

 

Depending on your operating system, navigate to the location of where the HOSTS file is located.

In notepad: File --> Open

The HOSTS file can be found:

Windows 95/98/Me
c:\windows\

Windows 2000
c:\winnt\system32\drivers\etc\

Windows XP
c:\windows\system32\drivers\etc\

The HOSTS file does not have an extension such as .txt or anything.  It's just plain "HOSTS"

In the "Open" Dialog box, change the "Files of type" box from "Text Documents (*.txt)" to "All Files". 

You should see this:

 

Click on the file name "hosts" and click "Open"

 

Everything with a "#" in front of it is commented out and is not processed.  You'll see at the bottom of the file, there is already one entry in the file:

127.0.0.1       localhost

That is the format of the HOSTS file.  First you type in the IP address, then after at least one space, the domain name or word that you want mapped to that IP number.

Here are some restriction on the HOSTS file that I discovered after some experimentation.  I think I have too much free time... sigh...

  • You cannot use wildcards in the domain name to reference an IP number.  IE, you can't use the "*" or asterisk sign as a wild card in the host name or domain name.  If you do use an asterisk, it will be read as an asterisk, not as a wild card.
  • You cannot refer one domain name to another domain name.  You can only map an IP address to a name, you cannot map a name to a name.
  • You must have separate entries for www.domain.com and domain.com
  • You cannot use port numbers.  For example, if your server runs on port 8000 or something, you can't map 123.123.123.123:8000 to yourdomain.com.  Ports are not understood.  This is a pain for those of you who run your webserver on a non standard port AND have a router that doesn't support loopback.  My advice is to buy a router that does support loopback, since the HOSTS file won't help you here.
  • The HOSTS file is read from top to bottom, and if you have a domain name mapped to multiple IP addresses, it will go to the first IP addresses and ignore the second one.  However, if the first one fails to go anywhere, the second IP will be read and then used.
Now let's do an example here.

Let's pretend that your webserver has an LAN IP number of 192.168.0.20.  You have 3 domain names: domain.com, bananas.com, ilikepeaches.com.  Here's what your HOSTS file would look like.

 

I put spaces between the entries for clarity.  The spaces between lines are not necessary.

As you can see, it's pretty simple.  After you make your changes, go to the "File" menu and select "Save". 

You're done.

How do you check if your HOSTS file is working correctly?  All you have to do is open up a DOS command window and try to ping the domain name you entered into the HOSTS file.  The program should then attempt to ping the IP address you specified in the HOSTS file, NOT the IP number you have on your DNS servers.

For example, when I try to ping banana.com, I get:

 

You can test each entry to be sure. 

Remember, the HOSTS file is read by only your local computer.  People from the Internet see what's on your DNS servers, so don't forget to remove the entries in the HOSTS file once your DNS changes propagate across the Internet. That way, you'll see your sites as everybody else sees them.

Brian
 



Step by Step Menu