Block Access to Websites on a Mac by Modifying /etc/hosts

Mar 19, 2007 - 68 Comments

The Finder icon of Mac OS X We’ve received several questions asking how to block specific sites from being accessed directly on the Mac. The frustration seems to lie in how easy it is to circumvent the typical measures, such as setting Parental Controls and blocking sites in Safari, only to have them accessed in Camino or Firefox. Well, a quick and efficient way to block access to specified sites is by editing the /etc/hosts file, which has the added bonus of providing system wide results. Whether it’s children or your roommate who you’re trying to stop from visiting a particular website, here’s how to do it.

Block Access to Websites on a Mac

Editing /etc/hosts

1) To edit the hosts file and add a new entry, open up your Terminal and type the following (You’ll be asked for your root password because this is a system file):
sudo nano /etc/hosts
2) This will bring up a screen that looks a lot like the one below, notice that yahoo.com has been added at the bottom as a site we want to block. To block any other site, simply type it in the same fashion. You can use the loopback IP of localhost and have as many sites map to 127.0.0.1 as you want, or specify other IP’s to redirect a URL to, like 0.0.0.0:

Blocking websites in ETC HOSTS File of a Mac

3) Now save the file in nano by hitting control-o and the return key.

Note that you may need to run the following command for changes to take effect:
sudo dscacheutil -flushcache
This flushes your DNS cache.

How this works: you are now telling your Mac to send requests for MySpace.com (or whatever site you listed) to 127.0.0.1, your local machine. This completely blocks the MySpace.com address from being loaded in any web browser. (Note that if someone is savvy enough though they could access the blocked site through a web proxy). If you really want to be crafty and have Personal Web Sharing enabled, you could put up a simple webpage for people to see when they try to access the blocked sites.

Confused? Want a visual walkthrough? Take a peak at our video guide showing how to edit /etc/hosts in Mac OS X

Enjoy this tip? Subscribe to the OSXDaily newsletter to get more of our great Apple tips, tricks, and important news delivered to your inbox! Enter your email address below:

Related articles:

Posted by: OSXDaily in Command Line, How to, Mac OS X, Tips & Tricks

68 Comments

» Comments RSS Feed

  1. Your method works great for Mac’s and Linux machines, but you can do it on a Windows PC too, this link will show you how:

    http://www.allthingsmarked.com/2006/08/28/howto-block-websites-using-the-hosts-file/

  2. GreenAlien says:

    Blocking MySpace, LOL!

  3. pbhj says:

    Like hmmm how ancient is this technique!! I’ve been blocking internet hosts using /etc/hosts (or the windows equiv.) for about 8 years and it was already prolific back then.

    I seem to remember something about using 0.0.0.0 instead (or was it the other way?).

  4. web hosting says:

    that’s so last millenium

  5. Paco says:

    try
    http://someonewhocares.org/hosts

    you’ll find more information about hosts files and a very nice list of sites to block…

  6. hmills says:

    simple enough

  7. Will this work if you wanted to block a specific page on a site? without blocking the hole site.
    example: myspace/yoville.com, so the myspace page could be viewed but not the link to the application on myspace.

    sorry if that makes no sense :)

  8. […] how you can do it too: Windows | OSX | […]

  9. I was able to block the site but wanted to unblock it to make sure I could unblock if needed. How do you undo the block I deleted text and saved what now?

  10. Jill says:

    I also want to know how to unblock if needed…

  11. michael says:

    @Jill and girlfriend on myspace to much guy, to unblock all you have to do is go back into the command and delete the urls you put in and also the ip address to your computer and safe the file again and that worked for me, just double check if you remove access to a site or add it back to see if it worked i have a mac with file vault on and it did not work for me i tested it on another account with out it on and it worked fine.

  12. Jon says:

    Please help! Thanks for the step-by-step guide, unfortunately it doesn’t work. I am using OS 10.5.7, 2 GHz Intel Core 2 Duo, and don’t have fire vault on. After entering the Terminal, adding my password, the screen I get is similar to your example above, except that the title line reads Terminal – sudo – 80×24, and after the line that reads
    ::1 localhost
    I have another line that reads
    fe80::1%lo0 localhost
    Followed by the lines I added such as
    127.0.0.1 myspace.com
    127.0.0.1 facebook.com
    127.0.0.1 asx.com.au

    I then re-started my computer, only to find that safari version 4 public beta and firefox I can still enter the websites. I know my command lines are saved as when I re-enter the Terminal my added lines immediately appear. I would really appreciate it if you could help.

  13. Greg Anderson says:

    It’s easier to type
    sudo /Applications/TextEdit.app/Contents/MacOS/TextEdit /etc/hosts
    …into terminal and then you can edit the hosts file in TextEdit. WAY easier in fact! Terminal seems to act weird sometimes with the delete and backspace key.
    To remove a site, just delete that line and then save.

    • Alex says:

      Hi Greg, your way of doing it is indeed way easier. I have on problem though: it perfectly blocks certain pages but it displays some I put down on the list… can’t understand why… any idea? I had everything in “www.nam.e” form and certain pages did not work, changed that to http and still dd not work… any idea? Do i need to clean cache or? No clue… :(
      Thanks for reply..
      alex

    • Alex says:

      just one more thing that can give a bit more light on it… One website I tried to block was just a section of one. But as I wanted to try how it works I blocked the “main” web site as well. It worked great. But then I deleted the “main” one as it was newspapers and I wanted to read it and all of sudden nothing from those pages cannot be blocked… Other web sites can be blocked and it works okay….
      Cheers..
      alex

  14. […] Block access to specified sites by modifying /etc/hosts […]

  15. Gary says:

    I don’t have a password set on my system so when I type in nothing it just goes back to the command line again.
    What type of password does it asks for? System?

    Thanks

  16. Carlos says:

    How can I block the IP adress and block surfing using proxy?

  17. Susan says:

    I’m having the same problem that Jon did back in June. I did what the instructions said and saved the sites I wanted to block, but they’re still coming up on both firefox and safari. Need to get this done to keep kids off of sites where they shouldn’t be. Love my mac, but this site blocking stuff should be easier. Used Blocksites with Firefox and it works like a dream, but even when I try to set safari parental controls through the System Preference, nothing is blocking sites.

  18. Jen says:

    Ditto as Susan and Jon. Anyone?!

  19. […] Modifying the hosts file in your own computer. No need for fancy plugins here. Go minimalist by editing a simple text file that lives on your own […]

  20. Hillary says:

    Jon, Susan, and Jen:
    Make sure you flush your cache after you save the hosts file.
    For Mac OS X 10.5 and above, this can be done by opening Terminal and entering the following:
    sudo dscacheutil -flushcache

    If you are using Tiger, open Terminal and type the following:
    lookupd -flushcache

    I hope this solves your problem! Good luck!

  21. Elisabeth says:

    having the same problem as Jon, Susan and Jen!
    I have on my screen exactly as it is in the example box above – i have put in 127.0.0.1 facebook.com and then pressing cntrl-o and return, but all it does is say at the bottom File name to write: /etc/hosts with space to type. Then what?! Do i exit terminal without saving changes? I don’t understand as the process seems unfinished. Also the ‘sudo dscacheutil -flushcache’ thing doesnt seem to work. Im sitting my A-Levels and I REALLY need to block Facebook… please help!

  22. […] This tip was covered a few years back with instructions to block websites on a Mac. I’ve gotten enough messages about the topic that I felt it was worth repeating, even though […]

  23. Maya says:

    Yahoo! You’re the best! thanks so much

  24. Peter Wane says:

    Had configure SQUID for Windows. I was a linux guru back in the old days until such time when SQUID was released to support Windows and an office consult me of setting up their web proxy cache I tried using this and it’s awesome. In my page I’ve put step by step configuration on Windows with scripting on squid.conf on how to block sites like streaming,porn sites and block port.

  25. […] involves editing your /etc/hosts file. You may recall that you can edit your /etc/hosts file to block access to websites on your Mac, well that is exactly what we’re going to do, block Tynt’s Javascript from […]

  26. Brandon says:

    what if you need to block a specific address like:

    https://www.google.com/voice/#billing

    Thanks

  27. quicksilver says:

    Ive been using http://www.timedoctor.com/2 .
    It uses a better procedure than blocking social media sites because it only monitors websites during production hours. People/Employees still have the option to use it for a breather or during breaks really . Sometimes they use it for work too .

  28. atia says:

    ok what if i wannt to unblock them back?

  29. maria says:

    Hey I’ve managed to block websites in firefox but not safari ? I use both browsers and want both to block darn facebook !

  30. Avram says:

    Is there a way to revert this blocking?

  31. nermin says:

    please what can i do the facebook is blocked and this massege apper in terminal
    sudo: /private/etc/sudoers is mode 0644, should be 0440
    please help

    • maria says:

      I think I had this problem before and could solve this problem – the problem can be solved in two ways
      Please send a copy of the screen to this message

  32. nermin says:

    heeeeeeeeeeeeeeeelp please

  33. nermin says:

    please what can i do the facebook is blocked and this massege apper in terminal
    sudo: /private/etc/sudoers is mode 0644, should be 0440
    please help

  34. swellcat says:

    Editing the hosts file has been done and cache flushed, but Mac OS 10.4.11 seems to ignore the hosts file, and the undesirable pop-unders still occur. How do we force the OS to abide by our hosts file instructions?

  35. Pri says:

    Hello all,

    Problem : How to unblock the blocked website ?

    Details :
    I blocked one website using sudo command. I just wanted to check how this blocking works. It worked great for blocking websites according to the procedure stated above.
    and now I want to unblock it. But I could not.
    I tried using deleting and saving the host file using pico cmd. It gets saved but the website remains unblocked.

    Please help me out.

    Thanks in advance.

  36. Pri says:

    Sorry for inconvinience :

    Correction in above comment :

    ….

    I tried using deleting and saving the host file using pico cmd. It gets saved but the website remains BLOCKED.

    ….

  37. lukea79 says:

    when it asks me for password: i then cannot type anything in? the grey box just stays there…… how do i enter my password??

  38. AW says:

    Thanks for the assistance. Worked like a charm

  39. I do not see any screens, above.
    Even so, I tried ” sudo nano /etc/hosts”.
    It returned “sudo: nano: command not found”, which is zero help!
    I am using OSX 3.5.

    I am looking for something which will, when a browser or other software, makes a request for a commercial, ad, or some other annoyance, that something useless be returned to the requester.

    Comcast is too lousy to download as many things are requested. This means garbage is loaded, then nothing useful. I want to ignore the junk, given Comcast’s functioning-challenged state, and have only the content download.

    The difference between an ad and a commercial is functional: An ad minds its own business. A commercial interferes with your enjoyment or usage of your time.

  40. Mark Crane says:

    Facebook is delivered via many different servers:

    # Block Facebook

    127.0.0.1 http://www.facebook.com
    127.0.0.1 facebook.com
    127.0.0.1 static.ak.fbcdn.net
    127.0.0.1 http://www.static.ak.fbcdn.net
    127.0.0.1 login.facebook.com
    127.0.0.1 http://www.login.facebook.com
    127.0.0.1 fbcdn.net
    127.0.0.1 http://www.fbcdn.net
    127.0.0.1 fbcdn.com
    127.0.0.1 http://www.fbcdn.com
    127.0.0.1 static.ak.connect.facebook.com
    127.0.0.1 http://www.static.ak.connect.facebook.com

  41. Michael Tangstad says:

    Hi

    Works on the first 2 addresses but not on # 3 and 4 ???
    I’m on Lion.

    help please
    thanx

  42. […] spending too much time checking sports scores or gossip news or other unproductive content you can block it. You can always unblock the page but the extra step will make you think […]

  43. […] build a profile of your activity. It's for this reason that facebook.com and http://www.facebook.com are blocked in my /etc/hosts file.I have ten extra minutes every day.I won't ever remark out loud about someone's funny status or […]

  44. Travis says:

    What program do i need to save it? When i press ctrl o return it doesnt let me save i just brings up my folders. What program do i need etc? or where should it be stored?

  45. Conni says:

    Blocked perfectly, but need to unblock it now! What’s the procedure…pretty please???

  46. […] backups so you can quickly restore things to how they were before being edited, be it htaccess, /etc/hosts, or just a plist […]

  47. bla says:

    nano is just an editor. Any other editor works, eg ‘vi’ ‘emacs’ textedit or whatever you prefer. Save as plain text.

  48. bla says:

    Note that ‘facebook.com’ is a different address than ‘www.facebook.com’. So if it does not work for you, you may have blocked the wrong address

  49. Josh says:

    Hello productivity! Thanks for this…

  50. rupsta says:

    thanks for the tip

  51. jennifer says:

    this works for me on the admin account but how do I get it to filter down to other accounts on the same mac (parent with children accounts). thanks.

  52. rvbdar says:

    What about websites like twitter? I tried 127.0.0.1 http://www.twitter.com but I still can visit the site by just typing twitter.com on the browser. What now?

  53. rvbdar says:

    Never mind, got it.

  54. tobeornottobe says:

    Hey,

    they ask for my pasword, but I cannot type there? Why so? It’s show
    Password:
    But now matter what key I punch, it doesn’t type out!!! Help please

  55. […] can block websites through hosts by adding them to the file and sending them nowhere, preventing […]

  56. Adis says:

    I use Persistence on http://www.persistence-app.com

    It’s got that iOS and Mavericks look and feel.

Leave a Reply

 

Shop for Apple & Mac Deals on Amazon.com

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates