What the hosts file is

On the Internet, machines are identified by IP (Internet Protocol) addresses. These are big numbers that allow messages to be sent to that machine. They are not human-friendly. To make it easier and more flexible, machines can also have human-readable names. For example, "amazon.com" is much easier to remember and use than "204.74.108.1".

To use a name, there needs to be a way to convert the name to an IP address. This is typically handled by Domain Name Servers (DNS), which are computers that your computer can ask what IP address corresponds to a given name. Before consulting DNS, your computer first examines its hosts file to see if the name is there. If so, it will use that address and not use a DNS.

So, why would you want to change around what your computer calls other computers?

  • To give computers on your network names
  • To temporarily replace DNS when your DNS goes down
  • To block obnoxious advertisements

So, say that you have a home network and you have 2 machines: "192.168.1.50" and "192.168.1.51". Having names would make addressing them simpler. For example, you could name them: "work" and "play", if those are their functions. Then, to access files on the machine, you would only need to type "work" instead of "192.168.1.50". You can even make a new top level domain to enclose them, such as "work.local".

I used to frequently have problems with my Internet Service Provider's (ISP) DNS. It would go down every so often. That means that it would become difficult to access most websites despite that my actual Internet connection was working just fine. First, it is handy to keep the IP addresses of other DNSs (from other ISPs even) around so you can use them if your main DNS fails. Second, you can keep the IP addresses of your most commonly visited sites in a backup hosts file. That way, if you ever have problems, you can temporarily use that hosts file to make your favorite sites work.

Unfortunately, many websites have very annoying advertisements that come from external sites. These may flash, blink, shake, popup, and do many other things in an attempt to attract your attention. The worst ones will even exploit vulnerabilities in your browser to install malicious software on your computer without you knowing it. Other advertisers have space on so many different websites that they can collect detailed information on which sites that you visit. Many programs already exist to alleviate this problem. There are many standalone programs to block popups, for example. The hosts file can also be used to this end. Since these advertisements also are referred to by name, you can use the hosts file to give the wrong address for that name, making your browser unable to find the requested file. The simplest thing to do is to specify the address as "127.0.0.1", which is an IP address which means "myself". That way, all of the requests will immediately be handled and it will not be able to find the files (unless you happen to be serving advertisements from your own machine).

This is my hosts file. You can use it on your own machine if you like, after unzipping it. I have accumulated the names of many annoying advertisement sites as well as added in other people's lists. Every time a site annoys me, I add it to the list. Note that this type of blocking is much stronger than most other forms. If you use the FireFox AdBlock extension, you block only for the FireFox browser. With the hosts file, it is blocked for the whole operating system.

How to edit the hosts file

So, I have described what the hosts file is. Now how do you use it?

First you have to know where the hosts file is. Its location varies by operating system.

For Linux and some Unix it is at:

/etc/hosts

On Windows NT: (including 2000 and XP)
(Note that the file might not exist and would need to be created. There should be some similarly named files in that directory.)

c:\windows\system32\drivers\etc\hosts

For other operating systems, check on Wikipedia.

The example above with the work and play computers would have a hosts file that looks like this:

#Lines beginning with '#' are comments and are ignored

#Machines on the local network:

#IP address    Name
192.168.1.50   work.local
192.168.1.51   play.local

The hosts file is a plain text file. It can be editted with any common text editor, such as notepad or gnome-text-editor. Before modifying the file, make sure to make a backup. The format is that each line has an IP address followed by some spaces and then the name. A name can consist of letters, numbers, hyphens, and periods.

So, say that you had an awful flashing image from obnoxiousads.com. You could have a hosts file like this:

#Annoying advertisements:

#IP address    Name
127.0.0.1   obnoxiousads.com

Now, instead of that image, it will fail to get the file. This makes visiting sites much more comfortable and safe.