Skip to content

HOSTS file

Overview

The HOSTS file is used to translate a host name to an IP (Internet Protocol) address without querying the DNS (Domain Name System) server.

For example, suppose you would like to search the web using Google. You start your browser and enter the 'www.google.com' host name. Communication on the Internet is based on IP addresses rather than host names, so behind the scenes the operating system checks if the HOSTS file has a mapping for www.google.com to an IP address. If there's no mapping in the HOSTS file the operating system queries the DNS server that returns 64.233.183.99 - which is Google's IP address - and then the browser requests Google's search page.

The remainder of the document provides more detailed information about the HOSTS file, such as how to edit it manually, how to use it for blocking content, known problems and work-arounds, etc.

HOSTS file example

The following is an example of a HOSTS file that redirects www.google.com to 216.109.118.74, which is Yahoo's IP address. It also redirects wwww.yahoo.com to 64.233.183.99, which is Google's IP address. The line starting with # is a comment that will be ignored.

# An example HOSTS file.
216.109.118.74 www.google.com
64.233.183.99 wwww.yahoo.com

Blocking unwanted content

The HOSTS file is often used to block potentially unwanted content, such as advertisements, cookies, hijackers, spyware, adware, pornography, etc.

If you know the host name of the machine that serves the unwanted content, you can redirect it to 127.0.0.1, which is the local IP address of your machine. For example, assume you have redirected the ad.doubleclick.net ad server to 127.0.0.1. Every time your browser tries to load an image or a flash animation from the ad.doubleclick.net ad server, it will be redirected to your machine instead. As a result, the browser will not be able to show any content from this ad server.

Notification of blocked content

Unfortunately, there's no user-friendly notification when the HOSTS file blocks a site. In fact, Internet Explorer users will only get a general 'The page cannot be displayed' message, assuming you are not running a local web server. The same message appears when a web site is unavailable for other reasons that HOSTS file blocking.
If an image is blocked, you'll see a box with a red cross. Icon showed by Internet Explorer when an image is blocked by the HOSTS file The same box will also appear if the image is unavailable for reasons other than HOSTS file blocking.

Editing the HOSTS file

The HOSTS file is named 'hosts' and editable with any text editor, such as Notepad or Wordpad. Before editing, backup the current HOSTS file. The exact location of the file is determined by the 'HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Tcpip\Parameters\DataBasePath' registry key. The file generally appears at:

Problems and workarounds

HOSTS file and machines using a proxy

Sometimes machines use a HTTP (Hyper Text Transfer Protocol) proxy to handle the task of downloading and caching web content. The proxy also handles the translation from host names to IP addresses. As a result, the local HOSTS file may not be queried.

To configure Internet Explorer to use the local HOSTS file, please go through the following sequence: Start Internet Explorer -> Tools -> Internet Options -> Connections. Double-click your connection in the list labelled "Dial-up and Virtual Private Network settings. Check the box for "Bypass proxy servers for local addresses".
Note that this fix only applies to users running Internet Explorer using a connection configured to use a HTTP proxy.

Notes

  1. The HOSTS file example above uses IP addresses for Google and Yahoo, valid at the time of writing but they may change in future.
  2. It is not possible to use wildcards to block a range of servers, such as *.doubleclick.net.
  3. The HOSTS file can only map host names to IP addresses. Mapping an IP to another IP or a host name to another host name is not possible.
  4. The claims made here about the HOSTS file refers to Microsoft Windows platform.

Comments

No comments posted yet.

Leave a reply