The hosts file is one of the important files on the machines, related to networking. It is used to map hostnames to IP addresses and pretty much is similar in functionality to a DNS server.
The hosts file is given preferential treatment to other host name mapping services such as DNS servers. Also, the hosts file is in direct control of the system, unlike the DNS servers which means it can be used to override the host name resolutions.
The main functionality of the hosts file is to map local host names with in the local networks, which are usually not resolved by other methods. You will usually find entries for the “local” names such as localhost and loopback being mapped back to the machines.
The hosts file is located in the folder /etc/ and is a text file. The file can be accessed by using the absolute path /etc/hosts. This means that it can be modified locally using any text editor, provided you have the required permissions to do so. You can take advantage of the fact that the /etc/hosts is preferred over DNS and other host mapping method, to perform some functions locally and system-wide.
Blocking IP Addresses
You can block certain host names and IP addresses by mapping it to a non-existent IP or the localhost. This can be used to block several ad networks as well, which means it can function as a Ad blocker for your browser.
It is pretty tedious to find and map all ad networks manually into the hosts file. You can download some hosts file from internet which are crowd sourced and have very good coverage of commonly seen ads.
Another useful function is to temporarily block web domains, such as facebook.com or twitter on the machine thus blocking someone from accessing these sites using any of the browsers.
The advantage of using a hosts file is that it can work across browsers rather than having to have a separate Ad blocker for each of the browsers that you use. But it is quite possible that the maintenance of the file can become quite tedious over time and trouble shooting is cumbersome for large hosts files.
DNS Lookup Time
You can manually map the hostnames for your commonly visited web domains. This can reduce the domain name look up times, as the machine does not have to query the domain server and can map it locally.
There is usually a DNS cache that is maintained by system in order to make the domain look-ups faster. Even with the DNS cache, a local mapping can be faster. This however requires you to keep the entries current at all times. If there is a change in the DNS entry for the hostname at some time, it is quite possible that you will miss it and keep pointing to the old address.
Sometimes you will want to map multiple names to the same IP address. This is equivalent of creating multiple names for the same machine. These names are also known as the aliases to the machine. This is quite often useful in software testing and other scenarios.
One of the uses of aliases is that you can create shortcuts for your commonly used web domains. For example, you can shorten the web domain to something that you can remember easily. Remember that by doing this you are also effectively blocking and re-routing the domain that is your shortcut, if there is one. In the example below, you can use f.b as a shortcut for facebook.com.
220.127.116.11 twitter.com t.c t.com
Although there are several different things that can be done with hosts file, it is quite often not desirable to do so because of the effort required to maintain it. However it will often depend on your own specific requirement, but is often a very quick, easy and dirty way to get it done.