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 (see How can I preview or modify my website without changing the live site?).
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.