Block or Redirect Unwanted Websites Using HOSTS File

Block Websites Using Hosts FileThe 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:

%systemroot%\system32\drivers\etc\hosts

NOTE: In most cases this should be: C:\Windows\System32\drivers\etc\hosts

Unix/Linux: In case of Linux it is located at:

/etc/hosts

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:

  1. 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”.

  2. 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”.

Windows Folder Options

  1. 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 as hosts.txt.
  2. 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.com

    Each 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.

  3. 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       friendster.com
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:

Ping Test

From now on, whenever you type “friendster.com” on your browser, you will be redirected to “google.com”.

How does a 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 its 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 this post on hosts file will help you block unwanted websites on your computer. Waiting for your comments. :)

78 Comments

  1. kiran
    September 3, 2012 at 8:36 PM

    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


    • Amosemani
      December 18, 2012 at 9:46 AM

      Hi this is mani you have any idea about virus program please help me


  2. fun
    September 4, 2012 at 2:13 AM

    Hey Srikanth it has really being a long time. Love your work. pls keep it up and don’t abandon us again


  3. Gary
    September 4, 2012 at 12:04 PM

    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!


    • Tom
      March 15, 2013 at 4:48 AM

      No, it is not possible to do this via the hosts file. Your computer may try to find the “blocked” site, but since it is still using the Wireless LAN, and since the packets sent from your computer must travel through the Wireless LAN router to get to the internet, they will be filtered by the router before they get to the internet. Therefore, you won’t be able to get around restrictions on the router by modifying the hosts file.


  4. siva_phd9
    September 4, 2012 at 12:18 PM

    useful information. thank you for sharing.


  5. Soumeet
    September 4, 2012 at 12:33 PM

    Thanks. I always like your posts.


  6. dinesh s
    September 4, 2012 at 5:06 PM

    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.


    • Srikanth
      September 4, 2012 at 5:54 PM

      @ dinesh s

      If your antivirus has a proactive defense feature, please disable it and try again…


    • pradeep
      October 29, 2012 at 8:07 PM

      can u pls tell me … hw to to do it windows xp ? i need this information very much ..pls help me


    • Srikanth
      October 30, 2012 at 7:19 PM

      @ pradeep

      The same procedure applies to Win XP also.


  7. sridhar
    September 4, 2012 at 6:13 PM

    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..


  8. Anirudha
    September 4, 2012 at 8:36 PM

    @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.


  9. jision j
    September 5, 2012 at 1:20 AM

    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 …


  10. Winston
    September 5, 2012 at 4:30 AM

    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.


    • Srikanth
      September 5, 2012 at 8:47 AM

      @ Winston

      It should work! Try rebooting your PC and clear the browser cache as well.


  11. manu14143
    September 5, 2012 at 8:00 PM

    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?


  12. manu14143
    September 5, 2012 at 8:21 PM

    @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.


  13. Winston
    September 7, 2012 at 5:42 AM

    @ Srikanth thanks a lot.


  14. Dee
    September 7, 2012 at 10:11 AM

    I was all so very interesting, although it was very much over my head. I will need to re-read this page in order to do it justice.
    Thanks so much for giving me this chance.


  15. Kushal
    September 7, 2012 at 2:47 PM

    thanks bro


  16. vikas kumar
    September 8, 2012 at 12:45 PM

    thank you so much sir!
    blocking and redirecting worked quite well.All the information that you have given is 100% correct.I am very very thankful to you.one thing I want to tell you that I redirected http://www.facebook.com to http://www.google.com to successfully but when I redirected https://www.facebook.com to http://www.google.com, it was failed.
    Is it difficult to redirect the secure version of any website?

    I’m an electrical engineering student and I request you to give the hacking tips regularly.
    again thank you very much for information about blocking and redirecting.


  17. Sri Lankan
    September 13, 2012 at 2:51 AM

    That’s great…
    Thank you So much!!!


  18. Mahesh
    September 21, 2012 at 5:25 PM

    Is Ip add. 127.0.0.1 is only for example sake ? Because it’s fine if someone has static Ip add. but what reguarding dynamic Ip add?
    As you have explained it system look for Ip add and as that Ip add. Is of your own website doesn’t load.


    • Srikanth
      September 24, 2012 at 10:50 AM

      @ Mahesh

      127.0.0.1 is not just for the sake of example. It is the IP address that points to one’s own computer (loop back address). In other words, 127.0.0.1 when accessed points to my own computer, when accessed by you points to your own computer. For more info refer my other post: Private vs. Public IP address


  19. avinash
    September 22, 2012 at 6:22 PM

    hello sir ! my query is just regarding to “how to install windows7 from USB”.
    if i have a Iso image of win7 in my hard drive then how to take boot path from that image.

    just give a blog on how to install multiple OS on a single Hard drive.(winxp after win7)


  20. Sethman2099
    September 23, 2012 at 6:26 AM

    WOW!! Thanks!! What you explained perfectly in your article worked for my computer! Thanks for creating this article. It was very helpful and informative. :D


  21. Naveen Garg
    September 26, 2012 at 1:20 AM

    we can also surf block websites by using another website !! like http://www.kproxy.com !! if its also block then try http://www.server1.kproxy.com !! :)


  22. Adelaide
    September 27, 2012 at 9:40 AM

    I have been using Qustodio to achieve the same and have been pretty satisfied with it. With minimal user intervention, it blocks the sites in real time and also tracks the data that my kids download from the web; all anonymously. I am not a computer literate person and so playing with the files is a little scary for me.


  23. ankit
    October 3, 2012 at 8:22 AM

    if you google “how to block any web site?” it will show same method but none of the result share how this method works. impressive article though.


  24. Nishant
    October 8, 2012 at 10:12 AM

    My PC blocks all blogspot websites…any solution?


  25. POS systems Cape Town
    October 12, 2012 at 3:02 PM

    I don’t even know how I ended up here, but I thought this post was good. I don’t know who you are
    but certainly you are going to a famous blogger if you are not already
    ;) Cheers!


    • Srikanth
      October 14, 2012 at 10:32 AM

      Thank you! :)


  26. Rock
    October 16, 2012 at 9:42 AM

    Solid post , keep the it up…… bosss


  27. Shashank chauhan
    October 21, 2012 at 8:22 PM

    But sir how can IP 127.0.0.1 of every computer.


    • Frank
      October 31, 2012 at 4:48 AM

      The original specification defines 127.0.0.1 to be the IP of the local host. There’s nothing magic about it, all computers are programmed to route those packets directly back to the computer from which they’re sent. Which isn’t used as much in Windows as it used to be in *NIX where you would frequently have things that used the loop back connection.


  28. Student
    October 28, 2012 at 6:11 AM

    this is the first page i found, which explains how this method works. impressive article ,tnx! i have one question though.

    how can i block the “whole” website? For exampele i want to block everything with “9gag.com”..i tried to block 9gag.com and i wrote “www.9gag.com” into the hosts file and it worked. Then i typed http://www.9gag.com/trending into my browser and the page was loaded. So i typed http://www.9gag.com/trending into hosts, it worked too. But when i google for 9gag i can still open the page or when i open it from my bookmarks. In my bookmarks is the page saved as “http://9gag.com/trending”, so i typed this in the hosts file but without results :(


    • Srikanth
      October 29, 2012 at 11:46 AM

      This is one of the limitations of HOSTS file. It doesn’t accept wildcard (* and ?). So, you’ve to explicitly mention every URL that you want to block! :(


    • Student
      October 30, 2012 at 1:42 PM

      Oki, tnx for the info!


  29. kira
    November 5, 2012 at 3:28 PM

    Sir, these methods are not working for ads.alpha00001.com


  30. Roni
    November 16, 2012 at 7:59 PM

    I am a student of Computer Science. I have seen that many antivirus softwaes blocks illegal, violent, pornographic,…etc websites. I want to know how it detects those websites ?
    Another thing I am interested in learning hacking so if you tell me from where I have to start and name some books to read as a beginer in this topic.


  31. john
    November 16, 2012 at 11:50 PM

    Hi Srikanth,
    Great way of blocking, if I could just figure out how to get it run on my Windows XP Pro machine.It seems that my laptop is not accessing the given SAMPLE mapping of IPs/Host names.Is there any file within the OS with mapping of all/more IPs/Hosts?
    My second question is related to the given by you process of adding a website through copy/paste, rename, etc. Why is that so?
    Can’t I just modify the existing hosts.bak and save it? Obviously, after backing it up.
    And finally, if I succeed in applying your method of blocking, will it be a two way blockage, meaning the blocked website will have no access to my PC for whatever reason?
    Thanks a lot for your time in maintaining this very useful @ helpful website.

    Blessings.


    • Srikanth
      November 26, 2012 at 12:07 PM

      @ john

      1. Rename the original hosts file to hosts.bak and try creating a new hosts file. This should solve your first problem in most cases (also make sure that the entries are not commented using the # symbol)
      2. It’s simple! If you’ve created the hosts file in some other location (say desktop) and added all the entries, you will have to drop this file in it’s actual (%systemroot%\system32\drivers\etc) location in order to make it work. Of course, you can even modify the existing hosts file as well.
      3. No, it is only a one way blockage! :(


  32. Pradeep
    November 22, 2012 at 8:41 AM

    Any working host addresses to block yahoo messenger??


  33. amit
    November 28, 2012 at 11:40 AM

    sir this info is very helpful for me….thanx


  34. Abhijith Mulavana
    November 30, 2012 at 8:11 PM

    This ideas are cool and great dude.
    I loved it!!


  35. cute mj
    December 17, 2012 at 7:20 AM

    yeah nice, i like this i make my enemy loss.,.,. thaK u,.,:)


  36. asem
    December 31, 2012 at 4:27 AM

    first of all thanks for the post.
    and my problem is very interesting, I tried this on my laptopn and it worked, but then I did the same on my second laptop, but it doesn’t work.

    I’ve put
    127.0.0.1 http://www.facebook.com
    127.0.0.1 facebook.com

    but the browser still goes to Facebook pretty normal, without any problems, like that I didn’t changed anything. I cleared chache and history and reboot my comp, it didn’t work, what’s wrong?


    • asem
      December 31, 2012 at 4:56 AM

      I’ve found out that when I was using Notepad++ for editing it didn’t put the breaks between the line, so after I tried the notepad, everything worked out.


    • Srikanth
      January 2, 2013 at 12:07 PM

      @ asem

      Remove the http:// and then try. So the settings would be:

      127.0.0.1 http://www.facebook.com
      127.0.0.1 facebook.com


  37. Hai
    January 3, 2013 at 4:34 AM

    Your mode of telling all in this piece of writing is truly fastidious,
    all be capable of without difficulty understand it,
    Thanks a lot.


    • Shashikant
      January 29, 2013 at 5:05 PM

      hiiiiiiii,Srikant,can u tell me that how to remove blocked sites from my hosts file,can i deleted the mention sites for remove the restriction??/


    • Srikanth
      January 31, 2013 at 1:46 PM

      Yes just delete them. That’s it!


  38. Shashikant
    January 29, 2013 at 5:07 PM

    and my second question is how i can use internet access in my office,everything is disable even u can’t see the system settings,internet options also disable please help me..,.


  39. wasim
    February 4, 2013 at 2:31 AM

    Great article

    How do i convert hosts.txt to just hosts?

    Thanks

    Wasim


  40. Ashokkumar G
    February 22, 2013 at 8:10 PM

    is that any possibility to block all or many sites using the wild cart symbols like: 127.0.0.1 *.*


    • Srikanth
      February 23, 2013 at 12:02 AM

      The HOSTS file isn’t smart enough to accept the wildcard entry! So you’ll have to mention each entry that you want to block.


  41. Kwaku Nyantakyi
    February 23, 2013 at 2:10 PM

    My name is Kwaku Nyantakyi from Ghana (Africa)i want to become a good hacker but i don’t know anything about programming and hacking so i really need help.
    please friends help me.


  42. Savi
    February 24, 2013 at 9:28 AM

    My fb account was hacked and my virus scanner found a dsn entry on it. Was just wondering if I had a keylogger remotely put on my computer from this. Please explain how this happened . Thanks


  43. Ramesh
    March 6, 2013 at 10:34 PM

    Hi Srikanth, Ramesh here. ;) This worked fine with facebook. I actually tried to redirect it to your site. But, it just said website offline or something. But I achieved what I want. There was a problem with one more site. I applied the same method to omegle.com and http://www.omegle.com sadly it doesn’t either redirect or says its offline. It takes me to the same site itself.. What can be the problem??? Can you please help me???


    • Srikanth
      March 7, 2013 at 3:13 PM

      Do it for both “www” and “non-www” versions of the domain and it should work.


  44. Nuru
    March 18, 2013 at 10:54 PM

    am glad whatever you post here its true.i have tested all things u have mentiond but is working. wish you to post another. but in wind7 is not working to block website or to redirect.i have access the hosts file and make some change but if i want to save it can’t. why? thanks for all


  45. hes
    March 28, 2013 at 8:57 AM

    Hey this is good


  46. Gra Gra
    April 10, 2013 at 12:02 PM

    Thank-you so much for this!!


  47. Bill Mark
    April 11, 2013 at 11:09 AM

    now i can do kidding with my friends PC..
    i re-direct http://www.facebook.com into porn sites!! ^_^

    thanks a lot!


  48. Jiwan Tamang
    April 15, 2013 at 4:38 PM

    i have use following code to block some website from my pc but it is not possible can i get my solution
    127.0.0.1 http://www.youtube.com
    127.0.0.1 http://www.google.com
    127.0.0.1 http://www.google.com.np


    • Srikanth
      April 15, 2013 at 5:08 PM

      Remove the “http://” and then try…


  49. Robin Swinney
    August 31, 2013 at 10:03 PM

    I love this post. Thank you for being so helpful to us who can’t seem to use willpower! I have blocked many websites but I can’t seem to block msn.com. Can you help.

    Robin


  50. George
    September 13, 2013 at 3:25 PM

    Hi Srikanth!

    I tried to redirect http://www.youtube.com to http://www.youtubeforchildren.com:
    The entry in the hosts file is:

    174.37.158.3 http://www.youtube.com

    but it doesn’t work…

    How can I redirect this site??

    Thanks,
    George


  51. indian alex
    September 22, 2013 at 12:47 AM

    thank u very much for your info. keep it up………..


  52. Eva Harper
    October 11, 2013 at 4:37 PM

    For all the rubbish, malicious or simply irresponsible advice and fixes we get on the Net, this guy has done a great job! It is concise but very well explained. Above all, is informative!
    My only criticism, :|
    I hate the word “hacker” or “hacking” – it kind of smells bad if you know what I mean!


  53. robert
    October 25, 2013 at 3:09 AM

    what is the name of the link they use to hack because i don’t know maybe they may send to me


  54. Md. Mehedi Hasan
    November 4, 2013 at 3:04 PM

    Thank you for share an important tricks. I want to know more about it. May I Change the settings using any bat file? actually i want that i will make a bat file. if any one click the bat file. then auto meticaly change his host file.

    Please help me


  55. Ranjita Limbu
    November 10, 2013 at 11:02 PM

    Very nice idea ..i can lock easily harmful site for my child such as facebook youtube thank you lot admin


  56. Vikas
    November 17, 2013 at 10:13 AM

    I am getting prompts to install tuto4PC, I have not done it till now. Unwanted sites are opening even when computer is idle while writing a mail some game will open up or something like that can you help me.


  57. Steven
    January 9, 2014 at 12:29 AM

    Can you explain more fully how to ping a website using the command prompt? Such as how to access the command prompt to begin with? Also can this method be used to block individual facebook profiles or pages? I heard that Facebook is famous for getting around filters by using multiple DNS and other methods. I don’t want to block all of Facebook, I just want to block certain facebook profiles from my children. I hope you respond. Thank you


  58. venkatesh
    January 23, 2014 at 12:04 PM

    am not getting bro.. it is asking the administrators permission


  59. Himansh
    January 23, 2014 at 5:43 PM

    What if other person knows about it
    He can remove the sites from hosts file

    Can we make hosts file password protected ?