The HOSTS file
Summary
The hosts file is a computer text file that is used by a computer's operating system to match hostnames to IP addresses.
How the hosts file works
When your computer wishes to talk to another computer on the local network or the internet it needs to know the remote 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 hostname pairs 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 123.11.1.456 www.somewebsite.com 127.0.0.1 www.somenaughtysite.com
If you typed one of the hostnames or web addresses above that are on the right into your web browser or another application, 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 next to it on the left.
The loopback address
You will notice that the last line in the example above is to a made-up naughty web site and that the IP address is 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 specific web sites or hostnames.
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 content. By creating a line in the hosts file that starts with the loopback address followed by the address of the web site you wish to block, whenever something on your computer wishes to visit this site the hosts file will send them nowhere.
You can download block-lists of dodgy web sites so you yourself don't need to add them to your hosts file one at a time. Some security tools will add these lists of sites to block to your hosts file for you based on categories (adult, malware, spam, nsfw, etc) and some will even back up your hosts file and protect it from deletion or unauthorised modification.
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
Last word
The hosts file is a great way to tell your computer what the IP address of a remote computer is without it needing to talk to network and internet servers. It is also a great way of building up a list of web sites that you wish to block access to from your computer.
Article date: 27th February 2011
