What is the 'hosts' file and how could I use it?

RSS

What is the 'hosts' file and how could I use it?

Last updated January 20th, 2015

The 'hosts' file is a file that resides within the operating system on your local machine and allows you to manually map hostnames to IP addresses.

The 'hosts' file can be particularly useful when testing a website which does not yet have a domain name pointing to it.

For example, you may have recently setup a new website to be used at the hostname example.org. However, the DNS for your example.org domain name may not have been configured to point to the web server's IP address yet. Adding an entry to the 'hosts' file on your local system for example.org will allow you to override the real DNS for example.org and essentially "trick" your system into thinking it points somewhere else.

Where do I find my 'hosts' file?

On a Unix-based operating system (e.g. OS X or Linux), the 'hosts' file will normally reside at:

/etc/hosts

On a modern version of Microsoft Windows, the 'hosts' file will normally reside at:

C:\WINDOWS\system32\drivers\etc\hosts

What will I find in my 'hosts' file?

The default contents of the file will usually look similar to this:

127.0.0.1    localhost
::1          localhost

Each line in the file represents a 'hosts entry'. The entries contain two fields separated by white space (tabs or spaces). The first field represents the IP address that the entry maps to, while the second field represents the hostname that is mapped.

In the example above, the first entry is mapping the hostname of localhost to the IP 127.0.0.1

Any blank lines in the 'hosts' file will be ignored. Lines that begin with a '#' symbol are comments within the file and will also be ignored.

How do I add an entry to my 'hosts' file?

Adding an entry to your 'hosts' file is as simple as editing the file with a plain text editor of your choice and appending an additional line with an appropriate IP address and hostname.

Please note that administrative privileges are usually required to edit the 'hosts' file on most systems. On a Unix-based system (e.g. OS X or Linux) this will most likely require you to be logged in as the 'root' user or to use the 'sudo' command to elevate your privileges.

Can we improve this article? Suggest an improvement to this article.

Suggest an Improvement

Thank you for helping us to improve this Knowledge Base article. Please type your suggestions below:

Still need help? Get in touch with us.

If you aren't able to find the help you need then please send us a question and we can help you further.