Block Advertisements in ANY Browser via /etc/hosts

By | 2009/05/15

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.

25 thoughts on “Block Advertisements in ANY Browser via /etc/hosts

  1. Mike

    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.

  2. Steve Dibb

    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. 🙂

  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.

  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.

  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.

  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.

  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.

  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.

  9. Christer Edwards Post author

    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.

  10. 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?

  11. Arkadi

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

  12. 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.

  13. 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

  14. 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!

  15. The Cog

    The trouble with using 127.0.0.1 is that your browser queries your own PC (it’s the “loopback” address, after all). If you’re running a webserver on your PC then it will query that webserver. Even if you don’t run a webserver, it will try to connect to port 80 every time, and get refused.

    If you use address 127.0.0.0 instead, then the connect attempt from the browser fails immediately without ever sending a connect request.

  16. Agena 21

    ######################################

    This will make LIFE simple, for UBUNTU or LINUX MINT users:

    Open a terminal, then copy and paste this below:

    sudo gedit /etc/hosts

    [] Now got to this website below and copy and paste the text into your “HOSTS” file after deleting the existing text from gedit in your existing hosts file:

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

    [] Do this once a month as they update the info…

    [] Now press SAVE in gedit, and reboot… you will be protected once you reboot from the bad sites in your now updated HOSTS file… simple no? Enjoy, I do.

    ########################################

  17. Marius

    Agena 21, thanx, it works perfect, indeed is very simple.

  18. Tomas

    how would I undo this if I don’t like the outcome?

Comments are closed.