ad blocking through /etc/hosts

There are several ad sites that you’ll see being used everywhere. They slow down page loading, even on fast connections, because sometimes there’ll be lag and they won’t load immediately and instead of being able to read that interesting news article, you’re stuck waiting for some Flash animation to show up so you can ignore it. A simple way to make those ads 1) not show up and 2) not cause delays in page loading is to edit /etc/hosts. Check out hostsfile.mine.nu for ways of doing this in Windows and Mac.

Here’s an example /etc/hosts file:

127.0.0.1       localhost

# The following lines are desirable for IPv6 capable hosts
# (added automatically by netbase upgrade)

::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts

We’re not interested in anything after the first line, really, because that first line is the key to our trick. What it’s doing is associating a host name (e.g. localhost) with an IP address (e.g. 127.0.0.1). What we’re going to do is assign some common ad host names to IP addresses beginning with 127.0.0, and ending with some number. The idea here is to point the host name, in our case some ad site, to an IP address that goes right back to localhost. This will cause the ad to load immediately because it’ll be looking on your local system, but since you’re not going to have /adi/N3271.RealCities/B1808805.8;sz=300×250;click=… on your computer, the ad will just show up with a ‘Not Found’ message:

Example of blocked ad

So as root, edit /etc/hosts with your favorite editor and add the following lines:

127.0.0.2 ad.doubleclick.net
127.0.0.3 servedby.advertising.com
127.0.0.4 ad.uk.doubleclick.net
127.0.0.5 ad.au.doubleclick.net
127.0.0.6 z1.adserver.com
127.0.0.7 red.as-eu.falkag.net
127.0.0.8 f.as-eu.falkag.net
127.0.0.9 data.as-eu.falkag.net
127.0.0.10 ads.guardianunlimited.co.uk
127.0.0.11 ads.guardian.co.uk
127.0.0.12 a.as-eu.falkag.net
127.0.0.13 as1.falkag.de
127.0.0.14 code.fastclick.net
127.0.0.15 adfarm.mediaplex.com
127.0.0.16 smarttargetting.co.uk
127.0.0.17 data.coremetrics.com

Now any site that tries to load an ad hosted on one of those sites will instead display a message akin to the one in the screenshot above. If you find any other sites that host ads that you don’t want to see, just add another line to /etc/hosts.

Thanks to my dad for showing me this trick.

This entry was posted in Linux | Current music Nine Inch Nails - We're in This Together. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

4 Comments

  1. grouch
    Posted 4 March 2006 at 4:23 PM | Permalink

    My lawyer will call your lawyer. You have stolen the precioussss IP (both Imaginary Property and Internet Protocol)! I was going to patent that and buy a small South Pacific island with the royalties. Now I’ll have to live out my miserable, ‘Intellectual Property’-challenged, peasant life huddled in my hut in the frozen woods. Hackers and bloggers are destroying modern civilization (R, TM, C, Pat. Pend., see EULA for applicable restrictions, void where prohibited until we can buy a new law there, not responsible for anything regardless of how much you pay or how many times).

  2. grouch
    Posted 4 March 2006 at 4:51 PM | Permalink

    BTW, that idea is not -mine-. No doubt somebody came up with using the localnet in such a manner right after 127.0.0.0 was implemented.

  3. Posted 5 March 2006 at 3:28 AM | Permalink

    Ohhhhh… I like this already! *starts editing*

  4. Lebowski
    Posted 26 April 2007 at 7:59 AM | Permalink

    I find all the 404 errors annoying when trying to view web pages, so i install lighthttpd (on a seperate port if you already have a webserver installed) and change the 404 error page to a blank document in lighthttpd’s config file.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>