One of the lesser-known, but important computer files is the hosts file. It’s a basic text file without a file extension that was designed to map or override IP addresses before accessing a domain name server. While often used for running a development website, it’s also used to block domains.
Another way to think of a 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 browser first checks your hosts file to see if it has an IP address for nytimes.com. If the hosts file has an entry it is used. Otherwise, a domain name server is queried to get the IP address.
One benefit of using the hosts file is precedence. Most 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 types of Internet advertisements became invasive, some people used the hosts file to bypass the ads. People would add an entry to their hosts file that redirected an ad server away from the intended destination.
A more common use these days is for local web development. For example, I have a local instance of this website on my PC that I use for testing and development. To get the site to display in my browser, I would add a line to my hosts file like the one below.
When I type the test domain, 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.
Location of Hosts Files
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 look at the file in case you ever need to change the contents. The hardest part is probably finding the file and it is not just limited to the Windows operating system. Each OS handles the file a bit differently.
- Microsoft Windows:
- Mac OS X:
Blocking Websites Using the Hosts File
Apart from local development, there are other reasons you might want to use this file. For example, if there are sites you don’t want people to access and you don’t have a router or software that allows you to block 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 is used to add a comment.
Now, 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 about hosts file, 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.