Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Use /etc/hosts to block ads at the local level Network
When I'm surfing the web at home, I am easily able to use my DNS server to block content that I don't want to see on web sites. Though I'm using a Gentoo box (can't quite afford the nice Mac server I want), this hint covers this process on an OS X box.

Since I have a PowerBook and often surf the web at all sorts of places, I got to thinking of a way to do this locally. With DNS, adding a simple 'deny' entry is easy, but using someone else's DNS server usually means I don't have access to their config files.

Another way to do this is to redirect the DNS to point to a false IP address. The easy way to do this is to use /etc/hosts. By aliasing the DNS name of a site I want to block to 127.0.0.1, I can block all traffic to that site. Using sudo nano -w /etc/hosts, add the DNS names of the sites you want to block after the IPv4 localhost entry, separated by spaces.
127.0.0.1       localhost ads.site1.com ads.site2.com
255.255.255.255 broadcasthost
::1             localhost 
No logout or restart is needed, just a refresh of the page. Since this blocks the site at the DNS level, this should work with any web browser.

Of course, I do not recommend using this on all ads, especially on free sites you support, since those sites most likely make a good portion of their money off of ads, thus keeping their site free! I do recommend this solution on sites with an excessive amount of ads (including pop-ups and/or pop-unders), or sites that have ads not particularly appropriate to a school or workplace environment.

[robg adds: Yes, it's easy to block ads. And yes, there are many ways to do it either within a given browser or system-wide, as shown here. And there are sites out there that take ads to the extreme, to the point where it's impossible to read the pages due to the amount of distraction one sees. But please, heed the author's advice about the sites you enjoy reading each day -- if everyone blocks ads all the time, then the future of the free web isn't necessarily good. Here on macosxhints, I don't directly make money off the ads on the site, but the ad revenue helps to pay my salary, and keeps the site (including the huge archive) free to readers. That's a good thing...]
    •    
  • Currently 2.50 / 5
  You rated: 5 / 5 (8 votes cast)
 
[38,764 views]  

Use /etc/hosts to block ads at the local level | 21 comments | Create New Account
Click here to return to the 'Use /etc/hosts to block ads at the local level' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
lookupd
Authored by: mchadwick on Nov 15, '05 07:10:08AM
Tiger uses lookupd to cache lookups. That means if you hit the ad before editing /etc/hosts and haven't logged out or restarted, chances are the ip is cached. This can be solved by running:
lookupd -flushcache
I make a habit of doing so after each edit of the hosts file, that way I remember, and since I edit the hosts file regularly I created a script called vihosts (named after visudo). I put it in ~/bin, which is in my $PATH. It's very simple:
#!/bin/sh
sudo vi /etc/hosts
lookupd -flushcache
You can replace vi with nano or whatever your favorite editor is. Just make sure it's in your path and executable and you can type vihosts to edit your hosts file now.

[ Reply to This | # ]
Good idea!
Authored by: zs on Nov 16, '05 08:08:37PM
Though I like to use the $EDITOR environment variable and full paths to make my scripts a little more robust, like so:
if [ $EDITOR ]; then
  /usr/bin/sudo $EDITOR /etc/hosts
else
  /usr/bin/sudo /usr/bin/vi /etc/hosts
fi
/usr/sbin/lookupd -flushcache

---
zs

[ Reply to This | # ]

Whoops...
Authored by: zs on Nov 16, '05 08:16:31PM
Don't forget the
#!/bin/sh
at the top of the script...

---
zs

[ Reply to This | # ]

Good idea!
Authored by: zs on Nov 17, '05 10:52:14AM
Looking back on this, it could be simplified further:
#!/bin/sh
if [ ! $EDITOR ]; then EDITOR=/usr/bin/vi; fi
/usr/bin/sudo $EDITOR /etc/hosts
/usr/sbin/lookupd -flushcache

---
zs

[ Reply to This | # ]

Use /etc/hosts to block ads at the local level
Authored by: eagle on Nov 15, '05 07:11:54AM

Yes, I've been using this trick for months. If you're slick about it, you can use it to protect against those nasty JavaScript ads too - you know, the ones that pop on top of a web page, which you have to close before you can even see the web page? Yup - you can rid your browsing life of those too.

It's very cool.



[ Reply to This | # ]
Use /etc/hosts to block ads at the local level
Authored by: dimwell on Nov 15, '05 07:41:42AM
Here's a pretty extensive HOSTS file. It's what I use on all my machines.

http://www.mvps.org/winhelp2002/hosts.txt

Hope this helps.

[ Reply to This | # ]
Use /etc/hosts to block ads at the local level
Authored by: jonesy on Nov 15, '05 10:49:53AM

And a couple more.

http://someonewhocares.org/hosts/

and

http://www.everythingisnt.com/hosts.html

Both sites are reputable and update the files regularly to reflect the shifting adwhore landscape.

Another item of interest, if you use Firefox.

Adblock - an extention to block ads on a per ad basis rather than the all-or-nothing of a host file.

NoScript - Blocks javascript, java, flash, and a whole host of other plugins . Like adblock, banned sites can be on a per incident basis.

Go to the Firefox extention page and search for them.

---
Grrrr!!!!!



[ Reply to This | # ]
possible security risk
Authored by: hayne on Nov 15, '05 09:03:55AM

I would like to point out that it is a possible security risk to install an /etc/hosts file that you download from somewhere unless you are careful to look at the contents of that file to be sure that it is only doing what it advertises.
For example, a malicious person could provide an /etc/hosts file that effectively redirected your banking inquiries to a different site of their making. Or that made Software Update connect to their site instead of Apple. Etc.

I would recommend using something like "Privoxy" (http://www.privoxy.org) instead of munging /etc/hosts. It is a more precise tool.



[ Reply to This | # ]
possible security risk
Authored by: Code Masseur on Nov 16, '05 01:11:02PM
Hayne wrote:
I would like to point out that it is a possible security risk to install an /etc/hosts file that you download from somewhere unless you are careful to look at the contents of that file to be sure that it is only doing what it advertises.

Excellent point! But you can use the shell to easily confirm that no rogue entries exist in /etc/hosts.

The following shell command will display any lines in /etc/hosts which do not start with sites which point to either 127.0.0.1, 0.0.0.0 or start with a comment.

grep -v "^127.0.0.1|^0.0.0.0|^#.*" /etc/hosts | more

If any lines are output, you can easily check to see if they are valid or rogue entries.



[ Reply to This | # ]
possible security risk
Authored by: markoz on Sep 01, '12 10:08:29PM
This worked for me:

grep -v -e "^127.0.0.1" -e "^0.0.0.0" -e "^#" -e "^\s*$"

[ Reply to This | # ]
Use /etc/hosts to block ads at the local level
Authored by: nvdingo on Nov 15, '05 10:49:21AM

My one exception to ad blocking on sites i visit frequently, is sites that use ad hosts that take FOREVER to load.
Thankfully, macosxhints does not have any of these ads, therefore, i don't block any of them.

But, i like to read gpf-comics and superosity every morning. keenspot (which hosts both) unfortunately has one of these super slow to load ad setups.

when they load, you can look in the activity viewer in safari to find out what is taking so long. those are the hosts that i block. Their loading of javascript ads has taken upwards of 5 minutes.

sorry, but you got blocked.



[ Reply to This | # ]
Use /etc/hosts to block ads at the local level
Authored by: auricgoldfinger on Nov 16, '05 12:23:12PM

Make sure to keep a backup. I did that once too, added some own redirects that I needed, but the hosts file was replaced by a system update...



[ Reply to This | # ]
Use /etc/hosts to block ads at the local level
Authored by: tomhannen on Nov 28, '05 06:32:17AM

Just a quick note... I needed to format my hosts file like this:

127.0.0.1 localhost
127.0.0.1 siteiwanttoblock.com

Not as shown in the tip:

127.0.0.1 localhost siteiwanttoblock.com

It's probably obvious, but I just thought I'd mention it. Also this tip blocks sites to all users of the machine, not just to one user (you need to be an admin to have access to the hosts file).



[ Reply to This | # ]
Where is my pico, vi, or nano?
Authored by: pdxdude on Dec 05, '05 02:19:01PM

I've hit a roadblock on this one, and could use any help, please.

I've successfully installed nano on one of my machines, no problem.

But on another machine, while trying to use pico or vi, I get command not found. So, I tried to do a manual nano install, and I get a couple of errors in the make. So i installed xcode, thinking some editors would be installed with that, and no luck. Any ideas? I'm just looking to do a basic edit of my hosts file, but can't access it with pico, vi, or nano.

Many thanks.



[ Reply to This | # ]
Use /etc/hosts to block ads at the local level
Authored by: errolbert on Apr 24, '06 01:09:14PM
The best of both worlds... Take a look at FloppyMoose. It's a css file that you use with your browser to "hide" advertisements. In this way, you still download them and your favorite site still gets the ad revenue, but you don't have to look at them... plus pages render faster.

[ Reply to This | # ]
Use /etc/hosts to block ads at the local level
Authored by: luomat on Apr 25, '06 12:58:48AM

@ errolbert:

Using CSS is a nice idea, but pages will NOT render faster if you use CSS vs /etc/hosts.

/etc/hosts blocks the call ever being made to the 3rd party ad server. There's no way in the world that "Contacting a 3rd party ad server" will EVER be faster than "Not contacting a 3rd party server".

In some case it might be almost the same length of time, assuming that the DNS information is cached already and the ad server is responsive. Plus "Not Downloading" the ads is a part of the feature many of us are looking for.

Personally I disable plugins (mostly to stop Flash) using Opera. If I find a site that I have to see using Plugins, I simply press alt+F12 to bring up the context menu and re-enable plugins. Easy as pie.



[ Reply to This | # ]
Under Leopard Lookupd has disappeared
Authored by: alec kinnear on Apr 01, '08 07:37:26AM

sudo lookupd -flushcache was the way to update hosts cache before Leopard. Lookupd is gone now.

In Leopard, lookupd no longer exists. Instead force a DNS cache refresh with:

sudo dscacheutil -flushcache

I hope this helps.

---
WordPress SEO Secrets - foliovision.com/weblog



[ Reply to This | # ]
Instructions for Snow Leopard?
Authored by: magnamous on Sep 14, '09 12:49:53AM

I've been fiddling with this on 10.6, and the dscacheutil -flushcache command doesn't seem to do the trick anymore. I even tried rebooting afterward, and that didn't seem to do it either. I tried visiting one of the blocked sites and the browser went right to it.

I've done this before on 10.4 and 10.5, so I'm pretty sure I'm not doing anything wrong as far as modifying the hosts file is concerned. I'm using OpenDNS, but the computer is supposed to check the local file first, so what gives? I can't imagine that Snow Leopard doesn't use a local DNS database, so has it given up on using the hosts file? I thought they just gave up NetInfo in 10.5, so why would they switch away from /etc/hosts now?

Any suggestions are greatly appreciated, as always.



[ Reply to This | # ]
Instructions for Snow Leopard
Authored by: magnamous on Sep 14, '09 01:05:21AM

Okaaaaaaay. So apparently it does still work. It seems that my problem had something to do with the specific server I picked (ad2games.com), which somehow redirected to www.ad2games.com, which then loaded. Once I added www.ad2games.com to the hosts file, it stopped working.

So the directions for 10.6 are apparently the same as they are for 10.5. Nice.



[ Reply to This | # ]
/etc/hosts is working... when I don't want it to {Snow Leopard}
Authored by: bruddalarry on Oct 01, '09 10:35:42AM

So, before I discovered Floppymoose.com I edited the /etc/hosts file to block ads (since 64 bit Safari doesn't support the adblocker I was using in Leopard... yet). I went back to the hosts file and deleted all of the sites I'd added, but it still appears to be blocking some websites... I tried to refresh using the dscache command in terminal (described above), and rebooted the computer, all to no avail. Unfortunately, I didn't think to backup the hosts file either. Any ideas?



[ Reply to This | # ]
/etc/hosts is working... when I don't want it to {Snow Leopard}
Authored by: vlad2k on Nov 09, '09 03:46:32PM

Try clearing browser cache



[ Reply to This | # ]