The HOSTS file
The hosts file is a text file that the operating system uses to map hostnames to IP addresses. It is named 'hosts' and is a plain text file. The hosts file should contain an IP address and a domain name separated by a space or tab character. Comments can be added in the file by using a # character at the start of a line. This article is about how to use a hosts file to prevent your computer from going to web sites that you wish to block i.e. ones that host malicious or adult content.
How the hosts file works
When your computer wishes to connect to another computer it needs to know the other computer's IP address. There are several ways that it can find this out but the first place it will look is in its own hosts file.
The hosts file is a text file which is made up of IP Address and host or domain names separated by white-space characters such as spaces or tabs.
It looks something like this where lines that start with the hash (#) character represent comments, which are ignored:
# This is an example hosts file containing made-up IP addresses 10.0.0.1 somemachinename 192.168.0.1 anothermachinename 22.214.171.124 www.somewebsite.com 127.0.0.1 www.somenaughtysite.com
If you were to type www.somewebsite.com into your web browser your computer will look at the hosts file first to see of it is listed, and if it is it will go to the IP address associated with it, in this case 126.96.36.199
The loopback address
You will notice that the last line in the example above has an IP address of 127.0.0.1. This is known as the loopback address. The loopback address goes nowhere. It is a very useful way of making sure that your computer will never visit this web site.
The hosts file as a simple outbound firewall
The hosts file can be a great way to protect yourself or other users of your computer from ever visiting specific websites, such as those with adult or malicious content by creating a list of all the web sites you wish to block and associating them with the loopback address. Rather than typing the list yourself you can download block-lists of malicious and adult web sites and add them to your own hosts file.
Here are a few free block-lists:
Where is the hosts file located?
The hosts file is located in the following locations.
- On Windows: C:\Windows\System32\drivers\etc\
- On Unix, linux and iOS: /etc/hosts
- Apple Mac: /private/etc/hosts
- Android: /system/etc/hosts
- Symbian: C:\system\data\hosts
The hosts file is a free and easy way of blocking web sites.
If you have any feedback regarding this article, or you have a suggestion for a new article, or just want to say thanks for the info then feel free to drop me an email at firstname.lastname@example.org.
Article updated: 14th May 2015