Hosts file plays a vital role in networking. You can compare Hosts file with the Phone book of your phone. When you want to call someone from your phone book you just click on the name of the person, you want to call. You do not need to remember the phone number of that person at all. Because, the number is already stored in your phone book. Hosts file serves the same purpose. It may sound that I am talking about DNS (Domain Name Server).
But Hosts file is definitely different from DNS.
Each and every website has at least one Global IP address. I am saying “at least one”, because sites like Google, Facebook, Yahoo, and MSN have a lot of servers all over the world to provide users fast and most relevant results. To access a site from your computer you need to send a request to its IP address. But it is very much difficult to remember all the IP addresses. DNS (Domain Name Server) handles that job. Then, what’s the role of Hosts file?
To discuss that first you need to have an idea on how your computer connects to the website, that you have entered into your Web-browser’s address bar.
Connecting through the Internet to a website
Whenever you type a Web address, i.e., www.facebook.com in your web browser, your computer first consults with your Hosts file. You can find your Hosts file in
location. C:\ may vary according to your OS root drive. Default Hosts file looks like following:
Your computer first searches whether there is any IP address for that web address(domain name) in your Hosts file. If there is no entry for that web address in theHosts file then only your computer sends a query to a DNS (Domain Name Server) to resolve the IP address for that web address.
Suppose you do not have any IP entry for www.facebook.com in you Hosts file (Generally we don’t have). So, if you type www.facebook.com in your web browser, your computer will send a query to DNS to resolve its IP address. Once it get the IP address of www.facebook.com from DNS, your web browser will be connected with that IP address and web page will start loading. Now, the question is, if you put an arbitrary IP address (Which is not the actual IP address of www.facebook.com) for www.facebook.com entry in your Hosts file, what will happen? Let’s check the situation with an example.
Open your Run window and type cmd to open Command Prompt. Now, type ping www.google.com and hit Enter. Thus you will get the IP address of www.google.com. For me it is 18.104.22.168
Now you need to edit your Hosts file. To do that, first you have to Take Ownership. Now, open the Hosts file with notepad or notepad++ and add the following line
22.214.171.124 is the IP address of www.google.com
Now, if you type www.facebook.com in your web browser and hit enter, you will see your browser is loading www.google.com. The reason is quite clear. You have created an entry for www.facebook.com which has the IP address of google.com in your Hosts file. It proves that computer first searches its local Hosts file to get the IP address for a Web address. If it does not find the web address in the Hosts file then only sends a request to DNS to find the IP address of that web address. So, Hosts file has priority 1 and DNS has priority 2. So, this Hosts file is powerful enough to musk original DNS entry and you can assign an IP address of your wish for an entry. You can also configure host file to block any website or IP.
Hope, you have enjoyed reading. Feel free to share you own views.