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