The Windows Hosts File
Learn how to speed up browsing and block unwanted websites and ads with a special Windows file called the "Hosts" file.
In a previous article on Internet names and addresses where some of the terms used here are defined, I discussed how the more friendly mnemonic format that we use, such as "www.microsoft.com", has to be converted to a numeric form that computers understand and that this is done on the Internet by special computers called name or DNS servers. The translation between human-friendly names and numeric IP names is made using large lookup tables where the correspondence between the two formats is kept.
Many people are unaware that their own computer also has the capability of using a local database called a “hosts” file that is stored in \Windows\system32\drivers\etc\. This local file, of course, is of necessity much smaller than what DNS servers use and goes back to a time when it was intended primarily for use on local networks. Nonetheless, there are applications of this file on the Internet at large that you will often see recommended. Speeding up browsing and blockage of ads and malware are applications of hosts files that I will discuss.
Format of Hosts File
The “hosts” file is a plain text file named just that, hosts. It may have to be created. Note that there is no extension on the file name. If you look in the folder \Windows\system32\drivers\etc\, you may find a default hosts file or a text file called "hosts.sam". This file with the extension “sam” is a sample file and has no function other than to illustrate the format. On more recent systems, the default hosts (or the hosts.sam) file has the contents shown below (it is slightly different in Windows XP). This sample explains how to make entries in the Hosts file:
# Copyright (c) 1993-2009 Microsoft Corp. # # This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
# 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
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
# For example:
# 220.127.116.11 rhino.acme.com # source server
# 18.104.22.168 x.acme.com # x client host
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
Make particular note of the so-called “loopback” entry:
This entry is a not an actual Internet IP address but defines a local address
and can be used to direct the computer to send a packet to itself. This
function is used in ad blocking and is discussed below.
No wildcards are allowed and only the main domain name is a valid entry. Directories and files at a site are not supported. Thus “www.microsoft.com” is a valid entry but “www.microsoft.com/windowsxp/” is not.
Since the hosts file is a text file, reading, editing, or creating it should be done in Notepad or other text editor. However, it has no extension so double-clicking will not open it automatically but will bring up the “Open with” menu instead. You can then select Notepad to open it. If your system has no hosts file and you use Notepad to create one, be sure to save the file with no extension. Notepad will automatically tack on a .txt extension otherwise. When saving, save as “hosts”, including the quotation marks.
In Windows Vista and Windows 7, the Notepad (or other text) editor must be run with administrator privileges.
If a hosts file exists, it is automatically searched during any process using the Windows TCP/IP stack. The search is done first before any attempt to find a DNS server is made. Thus any mistakes in hosts will result in an error message.
Creating a Hosts File
Generally, the entries in the hosts file have to be created one way or the other. The IP address corresponding to an URL has to be looked up and entered. While those familiar with the PING function can do this themselves, the task would rapidly become too tedious for more than a few sites. Also, many sites regard PING as a nuisance and block it. Fortunately, others have created hosts files that can be downloaded.
Speeding Up Browsing
Many of the so-called Web accelerators that are available as freeware or as part of commercial packages make use of the hosts file. The idea is that if you can resolve IP addresses on your own computer instead of waiting for a DNS server to do it, you can cut the time required to find a Web site. If you have a slow connection or if the servers are very busy, you might shave a second or two off the connection time of your most used sites. Or on the rare occasion when DNS servers are down, you might even be able to continue to use the Web.
However, there are several drawbacks to using a hosts file. The most obvious is its size limitation. Only a small subset of all the registered Web addresses will be in a hosts file. This can be useful in speeding up your home page and other pages that you visit regularly but many sites will still need the DNS server. Of course, many people visit only a relatively small number of sites on any regular basis and the fractional seconds saved each time may be attractive to them. Note that a hosts file that is much over 100 KB can actually slow up browsing unless the service "DNS Client" is set to manual start. (Managing services is discussed on another page. If you are on a local network, the setting for DNS Client should be left alone.)
Another problem is that the numerical IP corresponding to a particular URL can change. This can cause unexpected "The page cannot be displayed" error messages and inability to connect to sites. Thus, it is necessary to make sure that the hosts file is kept up-to-date.
My own personal experience is that no overall gain in efficiency results from using the hosts file to resolve IP addresses. The time saved in access time is more than cancelled by the time spent updating the IP addresses and the aggravation of sites that won’t connect. Those who rarely access more than a few sites might possibly benefit as long as they keep in mind the chance of not being able to connect to a site because the IP address has changed.
Perhaps the biggest use of the hosts file is to block sites that are regarded as undesirable or to block ads. This done by assigning the loopback IP 127.0.0.1 to an URL that you wish blocked. Thus an entry might be: “127.0.0.1 www.unwanted.com” (without quotes). Any request for such an IP address just gets sent right back to your own computer.
In order to see how ads can be blocked this way, we need to look briefly at the process involved in downloading a Web page to our computer. A Web page consists of many files which the browser puts together and forms into a single page for display on our computer. The various files need not come from a single source and many, especially ads and banners, can be from URLs other than the one initially addressed. To see where a graphic or ad comes from, right-click on it to bring up the context menu. Then left-click "Properties". The Properties Sheet will show the URL that is the source of the graphic. (This doesn't work for Adobe/Macromedia Flash ads.)
By putting a list of the URLs of the largest advertising agencies into the hosts file, many ads can be blocked. A number of people have compiled hosts files with large numbers of URLs for ads . When using this method, Web pages may have areas with the error message, “The page cannot be displayed"” where the ad would normally appear. Or you may just get a red “X” with a little bit of text. Ads that originate on the same site as the main page, however, will still appear. Because the blocked ad files do not have to be downloaded, pages tend to connect faster.
There can be problems, however. The compilers of ad-blocking hosts files can be a little zealous about privacy and sometimes block sites many of us would like to see. The files also often block counters and other java script applets that are pretty harmless or even useful. Further, sometimes using a hosts file can keep an entire page from downloading or interfere with navigation. The subject of ad-blocking is rather complicated, with philosophical as well as technical aspects, and cannot be discussed in detail in our limited space. There are many ad-blocking software programs that do more than use just a hosts file.
The bottom line on a hosts file, from my own personal view, is that it is probably not worth the effort for ad blocking. I have tried using one a number of times and I have always ended up removing it. I find alternate methods with browser extensions to be preferable.
Blocking Malware or Undesirable Sites
Hosts files are also sometimes recommended as a way to block known phishing sites or sites where downloads of malware can occur. Also known pornographic sites are sometimes blocked this way. Some may like this method but my personal opinion is that using browser extensions or add-ons is better. The latest versions of major browsers also have anti-phishing defenses built in.
Host File Hijacking by Malware
Some malware tries to use the hosts file by altering it so that some well-known addresses are directed to the wrong place. Thus, instead of going to microsoft.com, you might end up on a cleverly faked phishing site. A number of programs will warn you if something tries to alter your hosts file. I like WinPatrol. It has many other functions as well.