Skip to content

Block Advertisements in ANY Browser via /etc/hosts

I have been getting more and more tired of Firefox lately. Tired of the bloat. The unreliability. The gecko engine. I’ve been tinkering with alternate browsers such as Midori (which is *great*, assuming you can get >=0.1.6), Arora and Chromium. I think WebKit is the browser engine of the future, and with these browsers it is nearly a reality. They are still a little rough around the edges, but I can honestly see one of them absolutely taking off by the end of this year. Firefox, prepare to be dethroned!

The one missing link in most users transition to a new browser however is their reliability on extensions. I know I have a few extensions that I really don’t like to live without. I bet you do as well. Let me guess what your number one extension is? AdBlock Plus? So, if these new browsers don’t have an extension architecture (yet), how can I block ads? You can use your /etc/hosts file.

Let me tell you, quickly, about the /etc/hosts file for those that may not be familiar. The /etc/hosts file is the predecessor to the DNS system that we use now. It is a local mapping of IP address to hostname. At this point, as we now rely on DNS, the /etc/hosts file is generally pretty empty. You’ll likely just have entries that make sure your machine can find itself by localhost or hostname. Something along the lines of:

127.0.0.1    localhost  hostname

Now, you might be thinking “How am I going to block all the internets advertisements if my /etc/hosts file looks like that?” Well, you won’t, if it just looks like that. I have a solution for you though, and it requires very little work on your part.

In searching for a solution I have come across an /etc/hosts file that is (minus comments) 15,169 lines long. That’s right. Fifteen thousand one hundred sixty nine lines long. That is a lot of mapped IP addresses! What someone has done is collect every nasty thing he could find into the hosts file, and map it to 127.0.0.1.

What does that actually mean? It means that anytime your browser is told to display an ad it’ll need to look up the address. The /etc/hosts file is checked first before DNS, which then tells it to ask the local machine. The local machine, of course, does not have that information to display and therefore nothing is displayed. Bingo! No more ads.

But wait, there’s more! This not only applies to blocking ads, but also banners, 3rd party cookies, 3rd party page counters, web bugs, and even most hijackers. You’re not only blocking advertisements, you’re outright blocking thousands of known problematic and malicious websites. And all this without requiring a single Firefox extension. It works in ANY browser.

So, I hope you’re wondering where you can get a copy of this magical file that solves all of the worlds woes. Well you can get it here of course!

disclaimer: I am not the original author of this file, but it has been published under a CC-BY-SA license and under that license I am redistributing it. Attribution is contained within the file itself.

sudo mv /etc/hosts /etc/hosts.orig

sudo wget -c http://zelut.org/projects/misc/hosts -O /etc/hosts

These commands will move your original file as a backup and then pull the file from the web, putting it directly where it needs to go. You should be ready-set-protected after completion of the second command. Pull up a browser (hopefully you’ll try something other than Firefox), and give it a try.

If you have anything to add or subtract from the hosts file, you may edit it directly with a text editor. If you’d like to share your changes with the rest of us you may email me your update in the form of a patch. Please make sure your patch is created against the latest version.

I hope this solution works for many of you towards trying out and helping improve alternate browsers. Again, I highly suggest Midori or Arora as GTK or Qt (respectively) WebKit based browsers.

If this site has been helpful, please consider a donation.

Other Points of Interest

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

20 Comments

  1. This is an interesting idea, but you should note that it does more than block those ads in browsers. It also blocks them in every other program, such as Thunderbird, Evolution, Pidgin, etc.

    Two problems occur to me:
    1. Is the program that reads /etc/hosts optimized so that it doesn't have to read the whole file every time? If not, this could put in a fairly significant performance hit.
    2. When these things are blocked, you have no idea that they were blocked. It might be an interesting idea to substitute your own page of some kind in place of these calls so that you at least know that something is missing. This could be done too, with a clever apache configuration, I'd wager.

    Posted on 15-May-09 at 5:45 pm | Permalink
  2. That is still way overkill. Get privoxy — it effectively blocks any ads regardless of host based on regular expressions that are common and blocks image sizes that are common banner ratios.

    Plus, of course, it's browser agnostic. And you can run it centralized on a server. And you can add user overrides. :)

    Posted on 15-May-09 at 6:07 pm | Permalink
  3. YaManicKill

    Now, I never one for monopolies, but having tried many many browsers (IE, firefox, safari, opera, midori, arora, chrome, chromium) I still find firefox to be the best. I have tried loading several different pages on the different browsers (today I did it with firefox, arora and midori) and firefox still came out best (and no, nothing to do with cache…I used websites I have NEVER been on in firefox)

    So, as far as moving away from the status quo, I can't seem to get off firefox. Yes, its a memory hog…but I have plenty to spare.

    Posted on 15-May-09 at 6:24 pm | Permalink
  4. ethana2

    Ubuntu 9.04 has Midori 0.1.2 and it's not stable enough to get on Facebook or gmail :(

    I would love to be able to switch from Firefox to Midori. Firefox doesn't work with gnome-globalmenu.

    Posted on 15-May-09 at 6:50 pm | Permalink
  5. rhesa

    Another vote for privoxy from me. It’s wonderful to have the same filtering across all browsers I use.

    I’ve been using Galeon as my main browser for a couple of years now (ever since FF 2 came out), It’s not as heavy on the system on FF, although it uses the gecko engine as well. Might be worth a quick try.

    Posted on 15-May-09 at 12:53 pm | Permalink
  6. jimcooncat

    Another option would be DNSMasq, which will give you faster response times, and you can filter other machines on your LAN too. Here's a howto that I've yet to try out:
    http://wiki.flexion.org/SettingUpDNSMasq.html
    Note this is for an older Debian, for Ubuntu or Lenny change

    apt-get -t testing install dnsmasq
    to
    apt-get install dnsmasq.

    Posted on 15-May-09 at 7:43 pm | Permalink
  7. jldugger

    Unfortunately, this technique comes from the era of advertising driven ISPs and programs, and tools have been written to detect clients who grab content but not ads. This is why AdBlock has an option to download but not display ads.

    Posted on 15-May-09 at 8:52 pm | Permalink
  8. Greg

    On windows SpyBot S&D uses this trick to "immunize" a system's host file for long time, and i think adaware does the same too, BUT, we tested not long ago, that a IE and FF are terribly slow becauseof this huge hosts file. So i give my 2 cent to either dnsmasq, or some kind of proxy like privoxy, or even OpenDNS, far better approach than overfilling a hosts file.

    Posted on 16-May-09 at 12:40 am | Permalink
  9. ethana2 : https://launchpad.net/~midori/+archive/ppa

    Posted on 16-May-09 at 3:10 am | Permalink
  10. motoplux

    I will suggest foolDNS!

    Posted on 16-May-09 at 8:17 am | Permalink
  11. My DNS servers are going to bleed? My (or any) DNS servers don't have anything to do with this at all. Perhaps you missed the part in the article where the hosts file was read before DNS lookups were attempted, and therefore unneeded.

    Performance impact is also zero. It's a plain text file on recent hardware. If your machine has problems reading a plain text file, well, perhaps its time to upgrade.

    Posted on 16-May-09 at 1:34 pm | Permalink
  12. 1. There is no performance hit that I have seen. I would argue that performance was in fact improved, considering anything in this list can be read from a local plain text file, as opposed to DNS queries which can take much longer. (and no, I don't believe any programs cache the hosts file)

    2. I guess I'm just not interested in what is being blocked. Unless I'm unable to find the content that I expect I assume all else is not worth seeing.

    Posted on 16-May-09 at 1:37 pm | Permalink
  13. It's also much slower, at least the last time I tried it.

    Posted on 16-May-09 at 1:38 pm | Permalink
  14. dfpoon

    Firefox is getting unstable? Maybe it is problem of "places.sqlite" storing too much info.

    Here is a possible way to do it:
    http://forum.moztw.org/viewtopic.php?p=146834 (It is a traditional chinese page)
    Google tranlated as following:
    http://translate.google.com/translate?js=n&pr...

    Posted on 16-May-09 at 3:10 pm | Permalink
  15. Mike

    Yeah, you're right the performance hit is pretty minimal. Just as an experiment, I made a file with 15,000 lines, and then did a grep for one of the lines towards the end. Using the time command, it took about 0.1s for grep to find the line. So I guess that's not a huge hit really, and you're right that's got to be faster than the DNS lookup that would happen otherwise.

    Sometimes it's useful to /know/ that something was blocked, like, for example on your site I had no idea until I was blocking it until I browsed around and saw your request to turn off ad-blocking. Which makes me wonder…how would one create an exception to a rule with this system? Did you just shoot your advertising in the foot?

    Posted on 16-May-09 at 4:57 pm | Permalink
  16. Arkadi

    Thanks for the information, but i really like Firefox and wish every body would use it, so please live Firefox alone!

    Posted on 19-May-09 at 7:23 pm | Permalink
  17. Jim

    Thanks for this Christer, it works great on my Jaunty system and I have not noticed any change in performance, just dont have the advertisements anymore and thats what this is about.

    Posted on 20-May-09 at 1:56 am | Permalink
  18. Thomas

    sudo mv /etc/hosts /etc/hosts.orig
    sudo wget -c http://zelut.org/projects/misc/hosts -O /etc/hosts

    is bad.
    - For a while there your system has not hosts file. This could be bad news if wget needs it to find your proxy :-)
    - Nothing from your original hosts file is retained.
    - wget is run as root.

    # get the file using a non-root account
    wget -c http://zelut.org/projects/misc/hosts -O ~/hosts
    # CHECK IT!
    less ~/hosts
    # backup your old hosts file
    sudo cp /etc/hosts /etc/hosts.orig
    # append the new one to your hosts file
    cat ~/hosts | sudo tee -a /etc/hosts

    Posted on 25-May-09 at 4:43 pm | Permalink
  19. Excellent Work. I can now use Arora browser full time!

    Posted on 10-Jun-09 at 9:10 pm | Permalink
  20. Paul

    Cool, guys. I'm kinda new to Linux – that is, I've gotten rather used to it, but I don't know enough about it to muck around with config files without guidance. I ALWAYS look for a how-to before diddling with stuff, lol

    I knew how HOSTS works in Windows, and I knew that hosts in Linux was supposed to do the same thing. But, I didn't have a clue where to look for it.

    I've got it working now. Unfortunately, zelut.org seems to be down, or having problems. All I get over there is a front page, and a bunch of 404 errors. No real problem, though, because I have a pretty good, updated HOSTS file on a Windows machine. I just copied it over to the Linux box.

    Thanks!

    Posted on 29-Jul-09 at 2:28 pm | Permalink

Post a Comment

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