Knowledgebase

What is a hosts file and how to use it

The hosts file is a file on your local computer that's used for mapping domain names to IP addresses. It can be useful for various things. For example, when you transfer your site to a different hosting account and you point the domain name of that site to the nameservers responsible for the new account, some time has to elapse before the domain name resolves to the IP address of the new hosting account. It may take up to two days or even more for the DNS change to propagate globally. During that time you won't be able to access the site on the new location by opening the domain name with your web-browser unless you make a change to the hosts file.

You can use the hosts file to associate the IP address of the new account with the domain name. As we mentioned, that file is on your local computer and it's part of the operating system. Windows users can find it in the windows directory on their local computer. To be more precise, open the hard drive on which your windows is installed, locate the directory WINDOWS>system32>drivers>etc and inside the etc folder you'll see the hosts file. You can edit the file with a text editor. To do this, click on the file with the right mouse button, from the menu that appears select Open and from the list of applications that appears select a text editor (e.g. Notepad, Wordpad) and open the file.

In the file put the IP address on a new line followed by a single space and the domain name:

12.34.56.79 yourdomain.com www.yourdomain.com

Make sure that you list the domain both with and without the www part. Separate domain names with a single space. You can associate an IP address with more than one domain name or you can also list subdomains of that domain name:

12.34.56.79 yourdomain.com www.yourdomain.com sub1.yourdomain.com

If you want to map another IP address to a different domain name(s), put this on a new line in the hosts file. After the DNS change propagates globally you can remove the respective line in the file.

You can also use the hosts file to block certain sites from being opened from your local computer. To do this put the following in the hosts file:

127.0.0.1 domain.com www.domain.com

This will block the site domain.com (list it both with and without the www part) so that it cannot be accessed from your local computer.

Was this answer helpful?

 Print this Article

Also Read