Created Sat, Nov 21, 2009 by 少佐. Modified Thu, Mar 25, 2010. Cached Thu, Mar 25, 2010.
You are in: Root/Articles Section. vURL: /articles/block-ads.htm.

Introduction

Internet avertising is slowly bringing the Internet to its knees, and slowing your Web browser to a crawl. While not much can be done about the former thanks to "I want a piece of the money pie too" stupidity on the part of ad companies, much can be done to address the latter. In this webdoc, I'm going to show you how I kill ads.

CAVEAT: This article is provided to you on an AS-IS BASIS. This article is provided to you in the hope that it will be useful. You are, as a responsible adult, expected to confirm the veracity of my instructions before attempting to follow them. If you manage to bugger up your computer by following (or, more likely, by not correctly following) any instructions I give here, then don't complain to me about it.

CAVEAT: If you are using a corporate computer, ask permission from your sysadmin before making any changes to the computer's HOSTS file. If you are changing someone else's HOSTS, make sure you have their permission before doing so.

CAVEAT: BACK UP YOUR ORIGINAL HOSTS FILE BEFORE MAKING ANY CHANGES IN CASE YOU FUCK UP!

Table of Contents

  • 1.0: What Is the HOSTS File?
  • 1.2: History
  • 1.3: Where Is the HOSTS File?
  • 1.4: HOSTS File Format
  • 2.0: Using HOSTS To Block Ads
  • 2.1: Disabling Your DNS Client Service (Windows)
  • 2.2: Protecting Your HOSTS File
  • 3.0: Download My Personal 9.5 MB HOSTS File
  • 3.1: Caveats
  • 3.2: The Good News
  • 3.3: Download the File

  • 1.0: What Is the HOSTS File? [top]

    The HOSTS is where most computers with a TCP/IP stack look first when attempting to resolve a DNS name to an IP address, and it's meant to be somewhere the user can list frequently-visited sites which have static IPs. When faced with a DNS name, the computer checks HOSTS first to see if it's listed and, if it is, accepts the IP address listed in HOSTS instead of performing a DNS lookup to fetch an IP.

    The fact that HOSTS is always consulted first makes it a useful way of blocking ad servers and their ilk.

    1.2: History [top]

    The HOSTS file is a legacy thing, although can still be of great importance to anyone who needs to either block ad servers or quickly set up temporary redirections to servers on their local LAN. It dates back to the days of ARPANET∗, before the DNS system was invented. Back in those days, the internetwork - a network of networks, to use the original terminology - had no way of identifying nodes - computers - by name to other nodes (computers). There was no way to map a name that humans could read to a dotted quad IP address (what actually identifies a node on a network).

    To get around this problem, every node on the internetwork had a HOSTS file which contained mappings between human-readable names and IP addresses. These mappings were usually specific, referring only to servers that the particular computer needed to know about. While this approach worked, it was not without some glaring flaws - for one thing, there was no way for a computer to identify another computer by IP address given only its human-readable name if the target computer's name and IP address were not already present in the HOSTS file. A computer also couldn't readily obtain a HOSTS file from another computer - even if it could, how would it know what to do with it?

    Thus was the DNS resolution system born, and on the top of that and HTTP came today's Internet. But I digress. The HOSTS file was a workaround, a hack that was useful back in the day when the size of the network was small. Nowadays, global DNS resolution takes care of the problem for which the HOSTS file was originally conceived, but it still has its uses.

    Blocking ads, for example. :-)

    1.3: Where Is the HOSTS File? [top]

    The location of your HOSTS file will vary depending on the type of operating system your computer is using. On a Windows NT box, which is what mine is, the HOSTS file is located in

    %SystemRoot%\system32\drivers\etc\
    

    For boxes running Windows 95, 98 and Me, the file can be found at

    %WinDir%\
    

    On Linux, BSD, &c., it may be found in

    /etc/hosts
    

    1.4: HOSTS File Format [top]

    The format of the HOSTS file is simple. It consists of a bunch of lines, each of which can either represents a host, a comment, or nothing. A line which represents a host will contain an IP address, followed by some whitespace and then one or more DNS names. Blank lines are ignored, as is text following any pound symbol. Lines which begin with a pound symbol are ignored.

    Here is an example HOSTS file, to show you what I mean:

    #
    #  Sample HOSTS file
    #
    
    # System defaults
    127.0.0.1  localhost      # 'localhost' represents this machine *DO NOT REMOVE*
    ::1        localhost      # IPv6 'localhost' *DO NOT REMOVE*
    
    # Here are some anti-ad entries
    0.0.0.0  i.am.a.spammer
    0.0.0.0  canned.adverts # We hate these!
    

    2.0: Using HOSTS To Block Ads [top]

    This is simplicity itself. All you have to do in order to block one ad server, is to add one line to your HOSTS file with the IP listed as 0.0.0.0. What this does is to tell your computer that it needs to check an invalid address for content instead of checking the ad server for content. The end result of this is that your computer never performs a DNS lookup for the ad server, and will effectively ignore any request for content from that ad server.

    If you have more than one ad server to block, just add one line for each one, then save your HOSTS. It really doesn't get any more complex than this.

    CAVEAT: Always back up your HOSTS file so that you have it to return to if you mess up with your editing!

    See § 1.2 for details of the HOSTS file format.

    2.1: Disabling Your DNS Client Service (Windows) [top]

    If you're running Windows, you'll need to disable your DNS Client service before leveraging your HOSTS as an ad blocker, otherwise your computer will slow down every time it tries to access a blocked site. I don't know why these boxes will do that, but I can't be expected to know everything! Here's the workaround I use.

    Open your Run dialog (Start > Run), then run services.msc. (Note that you will need to run services.msc as an Administrator.) Scroll down the list to where "DNS Client" is showing. You'll need to stop the service first if it's already running; you can do this by right-clicking the service and selecting "Stop" from the context menu. If your DNS Client service isn't running, we're good to go! Right-click it and select "Properties". The Properties dialog for DNS Client will appear on your screen. Click the drop-down arrow for "Startup type" in the Properties dialog and select "Disabled". Now click the dialog's "OK" button and reboot your computer.

    What disabling DNS Client does is to disable the caching of DNS names in your computer. Without its cache, your computer will be forced to check its HOSTS file every time your Web browser or a Web server wants your computer to request some content from the Internet. DNS resolution will happen as normal (but uncached) if and only if there's no redirect line for the wanted DNS name in your HOSTS. In other words, disabling DNS Client makes your computer use HOSTS as it's supposed to be used, rather than attempting to skip HOSTS and use a previously-cached entry (which might be for an ad server that you've very recently blocked in your HOSTS).

    2.2: Protecting Your HOSTS File [top]

    It is always good policy to set your HOSTS file to be read-only. In recent years, it has become common to see malicious software such as trojans and virii attempting to add or overwrite entries in HOSTS files with their own, thereby giving them a way of transparently redirecting valid HTTP (and other) queries to sites of their own choosing. Some of the cleverer pieces of malware will delete any entries in HOSTS which redirect servers they need to use in the course of performing their malicious activities.

    By logging into an Administrator account on your computer and setting your HOSTS file to read-only, you will largely ameliorate this irritating problem. This has the added bonus of stopping any malware that's already on your computer dead in its tracks if it tries to alter your HOSTS file.


    3.0: Download My Personal 9.5 MB HOSTS File [top]

    I've decided to put my own HOSTS file online here so that you can take advantage of my ad-blocking attempts. My file has been compiled from a whole bunch of sources around the 'Net and includes many of my own finds. To the best of my knowledge, there are no duplicated entries anywhere in the file.

    3.1: Caveats [top]

    Before downloading and using my HOSTS file, please make sure that you've read and understand the following caveats.

    CAVEAT: My HOSTS file will break some popular sites. This is because they have stuff linked into their ads which they need in order to work (BAD!). In my opinion, any site that's designed to fail if its ads are blocked deserves to be broken.

    CAVEAT: My HOSTS file is huge. We're talking OVER NINE MEGABYTES kinda huge, here. Using it in concert with the instructions given above may slow your computer down slightly unless you have at least a 1 GHz dual-core processor in your machine. I personally have had no problem with any slowdown, but your mileage may vary.

    3.2: The Good News [top]

    Now you know the drawbacks of using my file as your own HOSTS, I'd better tell you what you'll get out of using it. Assuming you can stand a few of the more popular sites (such as eBay which tracks its users) going a little wonky, you'll receive the following benefits:

    • Many, many web bugs, email bugs, and tracking pages are blocked;
    • A whole set of unpleasant sites (e.g. rotten.com) are blocked;
    • Hundreds of annoying ad banner servers are blocked;
    • A bunch of sites which Google flagged as malware are blocked;
    • Also blocked are hundreds of sites containing trojans, virii and worms

    3.3: Download the File [top]

    Below you'll find a link to my HOSTS. If you have a slow connection, be prepared to wait a while for it to trickle into your download directory.

    http://m0rd3n.me.uk/downloads/hosts (contains over half a million unique ad servers).

    If you already have a HOSTS file, and you have custom settings in it, add my file (yes, all 9+ MB of it) onto the end of yours - don't overwrite your own file!