Block access to websites on a Mac by modifying /etc/hosts
March 19th, 2007 - Command Line, How to, Mac OS X, Tips & Tricks
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
Posted by: Editor

Order Mac OS X 10.6 Snow Leopard today from Amazon.com - $25!

Write a comment