How to Use HOSTS File to Block or Redirect Unwanted Websites
The hosts file is one of the interesting and useful feature of both Windows and Linux operating systems that provides many handy options in addressing network nodes. For instance, you can use the hosts file to block annoying ads, pop-ups, banners, porn sites, or even redirect one website to another.
In this post I will show you how to play around with the hosts file to accomplish some of the above mentioned jobs.
What is a hosts File?
The hosts file is just like any other computer file that is used by the operating system to map hostnames and domain names on to their corresponding IP addresses.
In other words, when you type “google.com” on your browser’s address bar, your computer will look for the hosts file to see if it contains the corresponding IP address for the domain name you typed (google.com). In case if no entry is present in the hosts file, the request is then passed on to the DNS (Domain Name System) server to obtain the IP address.
hosts file vs. DNS
When it comes to resolving the hostnames, the hosts file is given the first priority over the Domain Name System. In most cases, the hosts file remains unconfigured on the operating system and hence the DNS is used by the computer to resolve the domain names to their corresponding IP addresses.
However, if the hosts file is found to contain a valid entry for a given hostname or domain name, then this is picked up and used by the operating system.
Where is the hosts File Located?
Windows: In case of the Windows operating system, the hosts file is located at:
Unix/Linux: In case of Linux it is located at:
How to Block a Website?
To block a specific website, all you have to do is just add a new entry for that site in the hosts file. I would prefer to backup the original file and create a new one instead of modifying it. So, if something goes wrong, you can just delete it and restore the original one! Well, here is a step-by-step procedure on how to create a new hosts file on Windows:
Go to “My Computer” and you should see the “Menu bar” on top. In case of Vista and Win 7, just click “Alt” to unhide the “Menu bar”.
Now in the menu bar, click on “Tools -> Folder Options”. In the “View” tab uncheck the option that says: “Hide extensions for known file types” and click on “OK”.
Open a new text pad (New Text Document.txt) and rename it to: hosts. You will get a pop-up message with a warning. Just click on “Yes”.
Please note that the renamed file hosts should not have any extension such ashosts.txt.Open the hosts file using the notepad. Now let us assume that you want to block the sites Facebook and Youtube on your computer. For this all you have to do is add the following lines to your hosts file:
127.0.0.1 facebook.com
127.0.0.1 www.facebook.com
127.0.0.1 youtube.com
127.0.0.1 www.youtube.comEach entry should be placed on an individual line. The IP address and the hostname should be separated by a space or a tab. In this way, you can block any given website by just pointing it to the IP address 127.0.0.1.
Navigate to %systemroot%\system32\drivers\etc. Here you should see the original hosts file, rename it to hosts_bak. Just copy and paste the new hosts file that you have created. That’s it. You’ve now successfully blocked those websites from loading on your computer.
If you are using Vista/Win 7 you may have to take ownership of the original hosts file before renaming it.

How to Redirect a Website?
Suppose you want to redirect Friendster.com to Google.com, all you have to do is just add the following lines to your hosts file:
74.125.236.72 www.friendster.com
Here 74.125.236.72 is the IP address of Google.com. To find out the IP address of any given website, just go to command prompt and ping it. For example: ping google.com. You should get a ping response that displays the IP address as shown below:

From now on, whenever you type “friendster.com” on your browser, you will be redirected to “google.com”.
How does hosts file work?
Whenever you block a website using the hosts file, you point it to the IP address 127.0.0.1 which in fact is the IP address of your own computer. In other words, the IP 127.0.0.1 points to your own computer and doesn’t exist outside.
So, whenever you type the URL of a website for which an entry is present in the hosts file, your computer will load the IP as 127.0.0.1 instead of the actual one and tries to establish a connection with this IP address (127.0.0.1) but fails. As a result the website doesn’t load at all.
The redirection of the website works by pointing the domain name of one website to the IP address of another website (as configured by the hosts file) instead of it’s original IP address. So, whenever you type friendster.com, it is pointed to Google.com’s IP address and thus, the Google page loads up instead of the actual page from friendster.com.
How to Block the Annoying Ads?
I have found a great solution to keep all the annoying ads, banners, pop-ups and other parasites at bay by using a pre-configured hosts called MVPS hosts file. This hosts file contains a long list of entries to block all those annoying ads and pop-ups that usually load while you are surfing the Internet. You can also receive regular updates on this to stay up-to-date. You can download MVPS hosts file from the link below:
If you are playing around with the hosts file and something goes wrong, just delete the hosts file itself and rename the original file from hosts_bak to hosts. This should bring back everything to normal. I hope you like my post after a long gap. Waiting for your comments. ![]()

as an engineer your posts and scripts are very helpful to me for the general knowledge of the computer … i am very thankful to you
Hey Srikanth it has really being a long time. Love your work. pls keep it up and don’t abandon us again
This is an amazing thread, I really liked how you have explained the whole process of hosts file.
But, I had a devious idea (not sure if it can work though)
My hosts file has the following in it,
“# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost”
I’m using an office wireless LAN which blocks certain websites. My question is whether the hosts file can be used to redirect to a website (blocked by LAN). As I’m typing this, I’m beginning to feel that this is improbable!
useful information. thank you for sharing.
Thanks. I always like your posts.
I added
127.0.0.1 facebook.com
127.0.0.1 http://www.facebook.com
127.0.0.1 youtube.com
127.0.0.1 http://www.youtube.com
this to hosts file and saved it. As soon as i close the file my antivirus says file is infected and is now cleaned. Now when i open the file the above lines are gone from the file.
@ dinesh s
If your antivirus has a proactive defense feature, please disable it and try again…
i want to block internet connection in network computers , firewall always blocked….?? now i am used address check boxip enable type.. tel me any other type..
@Shrikantn
very nice article about Host files..
I really liked it..i was always confused about it..
now everything is cleared.. Thanks for such good knowledge sharing.
sir my collge has blocked all websites such as facebook i have tried to acess it using the method of converting the website ip to hexdecimals and type to the address its nt woking plz help…… to gain acess to site even gohacking is blocked….
sir plz help
with regards …
I tried this on Windows XP X86 SP2 but did not work, I read your article “How to Block a Website” which says Windows Vista/7 any suggestion? BTW I love this article keep up the good work.
@ Winston
It should work! Try rebooting your PC and clear the browser cache as well.
Hi Srikanth, I have a doubt.. What if we remove the hosts file from the computer.. What is the impact of this on the way the computer communicates with the network?
@Gary : Hi Gary, That wouldn’t work on your office LAN. The hosts file will redirect the traffic to the destination IP address. This happens on your computer. When this traffice reaches your office LAN, it will still be blocked. The HOSTS file will do the redirections only on your computer, but not on the network.
Leave your response!
EMAIL SECURITY »
How Do Email Spam Filters Work
If you are the one who works with emails on a daily basis, you are most likely to be using a SPAM FILTER to ease the job of sifting through a large number of spam emails …
HOW STUFFS WORK »
How Antivirus Software Works
Due to ever increasing threat from virus and other malicious programs, almost every computer today comes with a pre-installed antivirus software on it. In fact, an antivirus has become one of the most essential software package for every computer.
Even though every one …
HOW-TO GUIDES »
How to Use HOSTS File to Block or Redirect Unwanted Websites
The hosts file is one of the interesting and useful feature of both Windows and Linux operating systems that provides many handy options in addressing network nodes. For instance, you can use the hosts file …
INTERNET SECURITY »
How to Use HOSTS File to Block or Redirect Unwanted Websites
The hosts file is one of the interesting and useful feature of both Windows and Linux operating systems that provides many handy options in addressing network nodes. For instance, you can use the hosts file …
NETWORK HACKS »
Access Your Facebook Account with 3 Passwords
Did you know that you can login to your Facebook account using 3 different passwords? Seems interesting isn’t it? Yep! Unlike any other online account which has only one password to access, Facebook lets you …
Categories
I am the author and founder of GoHacking.Com. It all started in November 2007 as a small blog with just a few pages.
However, with the passage of time, the blog picked up more traffic and the response from the readers was also increased. This made me take up blogging more seriously.
You can read more about me or get in touch with me on Google+ or contact page.
Most Commented
Blogroll