Block access to specified sites by modifying /etc/hosts
Mac OS X, Tips & Tricks, Command Line, How to
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.
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.
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
Mac Deals from the Apple Store:
Social bookmarks:
- del.icio.us | Digg it | Furl | ma.gnolia | Netscape | reddit | StumbleUpon | Yahoo MyWeb |
Comments:
Comments: 6
Comment from SnowTheInformer
Time: March 19, 2007, 1:03 pm
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/
Comment from GreenAlien
Time: March 19, 2007, 2:03 pm
Blocking MySpace, LOL!
Comment from pbhj
Time: March 19, 2007, 9:36 pm
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?).
Comment from web hosting
Time: March 19, 2007, 9:37 pm
that’s so last millenium
Comment from Paco
Time: March 20, 2007, 12:08 am
try
http://someonewhocares.org/hosts
you’ll find more information about hosts files and a very nice list of sites to block…
Comment from hmills
Time: March 26, 2007, 2:36 pm
simple enough


Write a comment