The hosts file is a file in an operating system that is used to map hostnames to IP addresses. It is a plain text file and can be found in the
Windows\System32\drivers\etc\ directory in a windows system and in
/etc in Linux. Windows users can simply use notepad to edit it and Linux users can run
sudo gedit /etc/hosts to edit it.
When you open the file you might see something like this-
#This is an example of the hosts file
You can add lines to the file by writing an IP address followed by a hostname. The first line associates the IP address 127.0.0.1 to the hostname localhost. If you enter localhost or 127.0.0.1 into your browser's address bar, you'll see an error page unless you have a LAMP(or WAMP in case of windows) server installed. The most common application of the hosts file is that it is used to block websites on a computer.
Now, how do you block a website on your computer? It's no big deal. All you need to do is add a line to the hosts file. Lets suppose you want to block Facebook. Add the following line to the hosts file-
This will associate the hostname facebook.com with the IP address 127.0.0.1 and you won't be able to access the site now. You may also add lines for subdomains like www.facebook.com if you wish to block certain subdomains only. Note that you can't block a certain directory of a website like www.google.com/adsense/. Blocking a website can be handy at times and very effective if you have only noobs accessing the computer.
Almost every Indian student who has attended computer lab classes in school or at college would have felt bored and jobless at some instance. We all would admit we've considered messing with the school/college computers a bit as a cure for our boredom 😛 . I've used the hosts file to have my share of fun in my college's computer lab. We were told that the only website we were allowed to access was Google(read as search engines). I used my jobless mind to experiment around with the hosts file and redirected google.com to a lets say, NSFW website 😀 . Not that I'm suggesting anything (innocently blinking eyes), but if you ever feel like redirecting a domain to some other site, here's what you need to do:
1. First of all decide the the site to which you want to redirect the site. Now you need to find the IP address for that domain. Note that if the site is hosted on a shared host, it won't have a dedicated IP address associated with it. So its always a good idea to pick a famous website that is more likely to be on a dedicated server. Lets take the example of facebook for instance. Suppose I want to redirect google.com to facebook. Use any online service to get the IP address for facebook.com. You'll get 126.96.36.199. This is the IP address you want to redirect the hostname google.com to.
2. Add a line for the IP and the hostname you want to redirect to the hosts file. In this case, it would be
188.8.131.52 google.com. Mischief managed, you're done.
Needless to say, try stuff at your own risk and stay out of trouble at school/college. There was one more occasion when I used the hosts file in college. We were supposed to launch the website for Technosearch 2011 on the stage and we had no internet access there. I used the hosts file to point technosearch.net to 127.0.0.1 to make the launch look better. Hope this post helped you understand the purpose of the hosts file better and helps you use it for your work.