Block access to websites on a Mac by modifying /etc/hosts

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 myspace.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 have as many sites map to 127.0.0.1 as you want:

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 MurphyMac.com’s videocast showing how to edit /etc/hosts. MurphyMac also discusses other uses for the hosts file, such as setting shortnames to access local servers, check out the videocast here: MurphyMac’s videocast of hosts file usages

Related Posts with Thumbnails

28 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

Leave a Reply

 

Save big on Apple & Mac Deals from Amazon.com

Recent Comments

Shared on Facebook

Support Us

Mac Picks

iPhone & iPad Picks

Mac from PC Picks