How to block a website from Safari, Firefox, or Chrome

Apr 21, 2010 - 37 Comments

Safari icon Do you want to keep yourself or your employees from wasting time on certain websites? Maybe you don’t want your child to see some of the virtual trashbins of the internet? By editing the /etc/hosts systems file, you can block any website, and here’s how to do it.

Easily block websites from being accessed in Safari, Firefox, or the Chrome browser

* Launch the Terminal and type the following command, you will need to enter a root password:
sudo pico /etc/hosts
* Using your arrow keys navigate down and create a new line in the file
* You can block any website by following the format of:
127.0.0.1 facebook.com
127.0.0.1 myspace.com
127.0.0.1 twitter.com
* Exit and save /etc/hosts by hitting Control+O and then the Return key

Next you will need to flush your DNS cache for the changes to take effect, this is done through the Terminal as well with the following command in 10.6:

sudo dscacheutil -flushcache

block websites mac safari firefox chrome Unblocking the sites is simply a matter of removing them from the /etc/hosts file and flushing your DNS cache again. This whole thing works by pointing the domains you want blocked to your machines localhost (127.0.0.1). If you wanted to be tricky you could point the blocked domain to a different IP address entirely, like 74.125.19.103 (Google.com in English). For this purpose, you can get any websites IP address by typing nslookup domain.com into the Terminal.

If you want a network wide solution to blocking websites or any other network services, you’d need to modify your routers settings.

Note: 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 the methodology is the same.

.

Related articles:

Posted by: Manish Patel in Mac OS, Tips & Tricks

37 Comments

» Comments RSS Feed

  1. Martha T. Ortez says:

    thanks man! great recipe that really worked!

  2. Jason says:

    In case anyone reads this old page:

    This appears to work for me, but I found it necessary to include the “www.” for some websites. It seems to vary by website: For some of them, just listing “website.com” works, for some just listing “www.website.com” works, and for some, you need both. Also, if the site has any other subdomains such as “forums.website.com” or “mail.website.com”, you also have to list those if you want them blocked too. Tedious, but it seems to work eventually.

  3. Omeg says:

    This doesn’t block Omegle. Why?

  4. S Theys says:

    Thank you for posting this.
    I just found a charge on my PayPal account from a Disney website that allows kids to play on it, but then DOESN’T require adult permission when clicking their link to activate a membership. The link only asks for an email address and a user made username. It then goes to Paypal to process payment.
    Luckily I caught it and filed a complaint with paypal.
    But your posting has shown me how to block them from even reaching the Disney site in question.
    I did as the instructions stated, and then tried to access their site.
    “Safari cannot connect to http://www.(BLOCKEDNAME).com because Safari cannot connect to the server for (BLOCKEDNAME).com”

    THANK YOU so much !!

  5. Johannes says:

    thanks man! great recipe that really worked!

  6. yumyumshisha says:

    thank you for saving my marriage

  7. Alex says:

    Hey I’ve been trying to block a site, but after I enter that first sudo command and it asks for the password I cannot enter it- it won’t allow me to type anything.
    Help please!

    • Helper says:

      The password doesn’t show up, but it is being entered. Just type it and hit return.

      • Tito says:

        what’s the password -_-“

        • S Theys says:

          usually its the one you use to deactivate the screensaver, or when you first start up your computer, the log in password you use.
          Also, if you make any system changes (add a new program or delete a program) its the password you type in to allow the computer to proceed.

  8. Cody says:

    I would recommend using 123BlockMe. It’s free, easy to use, and will block websites on all browsers. Just search for it on Google.

  9. Bob says:

    Hello!

  10. I am using Aobo filter for Mac and it works fine

  11. xyz says:

    how to clear cache ? what does this mean-sudo dscacheutil -flushcache? and do i have to download any software to run this type of security on my laptop?

  12. macdental says:

    by the way, I am using Google Chrome on SnowLeopard 10.6.5.

  13. macdental says:

    The tip of modifying /etc/hosts works well. Thanks a lot.

  14. Jeff says:

    Hi David,

    I believe I have figured out why blocking through the /etc/hosts did not work. I decided to take one of the computers from school home. I hooked it up and blocked twitter and youtube via Parental Control. It worked just as it is supposed to. Same for /etc/hosts. I then took my personal home computer to school and tried to access websites I had blocked at home, but they were not blocked. I was able to access them at school. /etc/hosts also did not block websites.

    So I believe the issue is somewhere in the school’s server. It is not allowing the Macs to block websites via parental control. Our school uses mainly Windows computers, except our department, so I am thinking the server has been set up for Windows software. I do not know if this would have any effect on our Macs, but what should I look for on the university’s server to allow Parental Control to block websites? Unfortunately, the websites can not be blocked for the entire university, which is why we would like to block them through parental control or the Mac Air Port antenna in the Mac computer room.

    Thanks,
    Jeff

  15. Lol says:

    I cannot flush my cache? Why is that?

  16. crazy says:

    yo ppl..ur post was of great help.but now that i am done with the job how shall i unblock the sites back??..its pretty confusing in the post.so required help..:) :)

  17. Jeff says:

    We have 20 desktop Macs in one room for our students to use. We have tried to block certain websites, using both parental control and the advice from this website, but both were unsuccessful. When we try to block the sites, clear the cache, then enter the website we can access it no problem. Any ideas why both parental control and going through the terminal will not work when trying to block websites? Any other suggestions?

    Jeff

    • David says:

      Jeff,

      This article may be of interest to you:

      https://osxdaily.com/2010/11/21/block-facebook/

      Make sure you are trying to edit /etc/hosts as Administrator

      • Jeff says:

        Hey David,

        Thanks for the link. I did try it and that does not work either. We are running Mac OS X 10.6.5 with snow leopard. When I tried to block the site, and cleared the cache and restarted my computer I was still able to view the website with no problems.

        Thanks for the info.
        Jeff

        • David says:

          Jeff,

          Blocking sites through /etc/hosts does work, it’s possible your changes are not saving if the file is not edited as Administrator. Flush the DNS and then you can check that the domain block worked by trying to ping it from the Terminal, if successful you will be pinging the local host (127.0.0.1) rather than the blocked domain.

          If you’re still having difficulties, you may want to try one of the other techniques to block a site. Through the router or DNS is a very effective way, or as another user suggested, through an application called LittleSnitch.

          David

  18. Aeradore says:

    This does not work in Snow Leopard (10.6.x) as the hosts file is now ignored.

  19. Sandor says:

    There is another one tool to block a website, very simple and secure and have even more options.

  20. Garry Bunt says:

    Hola, Your post made sense and will help me in my business. thanks

  21. SK says:

    The hosts file approach actually doesn’t work (OS 10.6.3 in use here). I don’t know why, but the file seems to be simply ignored.
    Tried to search for info about why the hosts file doesn’t work any more but couldn’t find anything. Does anyone know something about this topic?

    I installed Glimmerblocker now, as Alex suggested. It’s good and it has great configuration-options, but unfortunately doesn’t work system wide. It’s a simple local proxy server and every browser on your machine must therefore be configured to use it.

  22. Alex says:

    Use Glimmerblocker instead.
    It’s fantastic.
    And free.

    Alex ;-]

    http://glimmerblocker.org/

  23. Sepp says:

    Why use the hosts file for that? Mac OS X ships with ipfw. You can easily block any site via:
    sudo ipfw add 70000 deny tcp from any to facebook.com

    If the site has more than one DNS A record, you could add a rule for each A record.

  24. James says:

    FYI, you can also sign up for the free OpenDNS service and it will block many things. So long as the user cannot change the DNS entries they will not be able to bypass the lockdown.

    http://opendns.com

    I used it to block apps.facebook.com to avoid the online games on FaceBook, etc. There are categories to block large sets of sites plus you can manually unblock/block specific sites.

Leave a Reply

 

Shop on Amazon.com and help support OSXDaily!

Subscribe to OSXDaily

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

Tips & Tricks

News

iPhone / iPad

Mac

Troubleshooting

Shop on Amazon to help support this site