One of the lesser-known but important computer files is your Hosts file. It’s a basic text file without a file extension. Your operating system uses it to map or override IP addresses before accessing the domain name system (DNS) servers. While often used for accessing a development website, you can also use it to block domains and programs.
Another way to think of the hosts file is as an address translator and redirector. If you went to New York City and asked to go to the New York Times Company, someone would translate your request into 229 West 43rd Street. A similar process happens on the web. Your web browser first checks your hosts file to see if it has any IP addresses for nytimes.com. If your hosts file has an entry, it is used. Otherwise, a domain name server is queried to get the IP address.
Benefit of Hosts File
One benefit of using the hosts file is precedence. Most operating systems access this file first because it’s loaded into the computer’s memory at startup. There are some exceptions, such as people who use proxy servers.
When various Internet advertisements became invasive, some people used the hosts file to bypass the ad servers. People would add an entry to their hosts file that redirected an ad server away from the intended destination. Today, people use ad blockers.
Hosts File Example
A more common use these days is for local web development. For example, I have a local instance of this website on my PC for testing. To display the website in my browser, I would add a line to my hosts file like the one below.
When I type the test domain name, www.timeatlas.local, into my web browser, the system first looks in the hosts file and finds the entry 127.0.0.1. This IP address is a universal address assigned to the localhost that is your PC. So, rather than going out to the web, the request would stop at my PC. In this case, timeatlas.local doesn’t exist on the internet.
Hosts File Location
The good news is you control the hosts file provided you have admin access to your PC. You can add, edit, or delete entries using a text editor. However, there may be security issues that deny you access. Microsoft support has provided a knowledge base article on modifying the hosts or lmhosts file.
It’s probably worth 5 minutes to review the file if you ever need to edit the hosts file. The hardest part is probably finding the file, and it is not just limited to the Windows operating system. Each operating system (OS) locates the file a bit differently.
- Microsoft Windows:
- Mac OS X:
How to Block Websites Using Hosts Files
Apart from local development, there are other reasons you might want to modify your hosts file. For example, if you don’t want people to access sites and don’t have a router or software that blocks domains.
For example, if I wanted to block access to Amazon, I could add this line to my Hosts file.
127.0.0.1 www.amazon.com #Block Amazon
In this instance, I’m telling the computer to assign the IP address 127.0.0.1 for Amazon. The # sign represents a comment line.
If someone uses that computer and types www.amazon.com in the address bar, they would get an error message. The text varies based on which browser you use.
If you want to find out more hosts files, there are plenty of articles and resources. Some people have compiled their own lists that you can download. Others have programs that make editing the file easier. And if you do decide to change your hosts file, please remember to make a backup first.