The hosts file is a text file within an operating system that contains associations of IP addresses, hostnames and aliases for the system and the machines on the network that the system is aware of. While Berkeley Internet Name Domain (BIND a.k.a. DNS) servers will give the IP addresses for servers on the Internet, and within a local network if one is set up for it, the first systems any computer will know about as it boots are the ones in the hosts file.
- 1 Creating a hosts file
- 2 Location of hosts file
- 3 Uses of the hosts file
- 4 Considerations for using a hosts file
- 5 Cool things to do with a hosts file
Creating a hosts file
Format of entries
The format of entries within a hosts file is written as one line per entry containing the IP address of the host, followed by the host's canonical name followed by any aliases assigned to the host. Examples:
127.0.0.1 localhost localhost.localdomain 192.168.1.15 thiscomputer.example.com thiscomp 188.8.131.52 wiki.linuxquestions.org LQWiki lqwiki
Rules of hosts files
- The fields within an entry must be separated by some number (greater than 0) of blanks and/or tabs.
- Host names must begin with an alphabetic character and end with an alphanumeric.
- Host names must be made up of only alphanumeric characters, minus signs ('-') and periods ('.').
- If a line contains a hash ('#') character, anything following the hash is ignored.
Location of hosts file
For the majority of Unix-based systems, the location for the hosts file is /etc/hosts.
For as much as Microsoft has tried rewriting all of computing and networking their own way, at some point, they had to join the rest of the world. Using the Microsoft terminology, in Windows XP/2K, the hosts file is located at %windir%\system32\drivers\etc\hosts.
Uses of the hosts file
As written above, when a system is booting up, the first computers it will know about are the ones listed in the hosts file. For most systems, the entries in the hosts file will contain information about important hosts on the local network for when DNS isn't accessible/available.
Network Information Service (NIS) is a network management system developed by Sun Microsystems. Networks that use NIS take the entries from the hosts files of the systems on the local network to build the NIS host database. On most NIS networks, every system on the local network will have an entry in their hosts file for every other host on the local network.
Very small private networks that are isolated from contact with the Internet or any other network use the hosts file instead of DNS. Since the network is not connected to the Internet, setting up a DNS server on such a small network tends to be more work than it's worth.
Considerations for using a hosts file
DNS versus hosts
When setting up a hosts file, the question to keep in mind is; Who/What does this machine have to know about? Who/What is going to tell this machine? Well, there is a file that tells the machine which to check first when looking for another machine; nsswitch.conf. This file is also under the /etc/ directory and tells the machine what order it should look at DNS, NIS and the hosts file.
When using hosts files on a small network, and you have to make a change in a hosts file on one machine on the local network, you need to make sure that the change gets reflected in *all* hosts files on the network.
Cool things to do with a hosts file
Bypass broken/incorrect DNS
There are times that a website or server to which you are trying to connect on the internet has incorrect/false entries in the DNS tables. You know the server is there by IP address, but can't connect to the domain because of DNS entries. In cases like this, you can just make an entry in your hosts file. An old example from my hosts file:
184.108.40.206 www.openwrt.org downloads.openwrt.org wiki.openwrt.org dev.openwrt.org openwrt.org forum.openwrt.org
This was an entry I had in my hosts file during a period that the OpenWrt project was moving their servers. The new DNS had propagated already and where DNS was pointing, there was no server yet. So, I added the above line to be able to access the server at the previous location. As of this writing, openwrt.org is correctly responding at 220.127.116.11, so I have since commented out the entry.
Many folks get tired of those floating flash-banner ads that all but completely cover the screen, forcing you to click to just get to the page you asked for. Then there are all the banner ads that will also create a bunch of popups on your screen. Well, when you have at last had enough of those servers, you can stop the receiving of their traffic with a simple entry in your hosts file. Here's an example:
127.0.0.1 irritating.exampleadserver.com alternate.exampleadserver.com
Looping the entry for the offending advertising server back to your localhost will cause the areas mapped for the ads to give 404 errors in those advertising spots on the webpage.