Most modern operating systems use a 'hosts' file, or some varient. What this 'hosts' file does is explained in /etc/hosts.

Using the hosts file on your system to redirect the hostname of banner ad servers to (your computer), you can prevent many banner ads for ever getting a chance of loading. Put simply, you can use this to block a lot of ads from websites without extra software and minimal effort.

Step 1:

Open up your system's hosts file. Depending on your operating system, it may be any of the following.

Windows NT/2000 - c:\winnt\system32\drivers\etc\hosts

Windows XP - c:\windows\system32\drivers\etc\hosts

Linux/BSD/Solaris/*n?x - /etc/hosts

BeOS - /boot/beos/etc/hosts

(Some of these systems may not allow you to view or edit the hosts file unless you are root/administrator)

The file will probably be little more then localhost.

Step 2:

With that file open on your system, paste the following into it below the localhost bit.

Save the changes.

Now ads from any of those servers will be unable to load. (Some systems may require a reboot for changes to come into effect)

Step 3:

There is no step 3.

The list will always be incomplete; if you find an adserver hostname that is not on the list above, don't create a new writeup below, please /msg a God or Content Editor and ask them to add it.

Also, standard disclaimer: This is all at your own risk... blah blah etc. If using this makes your computer explode, don't blame me.

Have fun!

VirtualWolf: I didn't specify Mac OS because I couldn't test it myself. As far as I knew Classic Mac OS uses a different style of hosts file, and I just thought Mac OS X would be covered by the *n?x bit. It's not, though...

/me smacks Xenex

You forgot the Mac OS! :P

Under Mac OS X, being a *NIX variant, the hosts file is located at /etc/hosts. Under Mac OS X 10.2, lookupd is configured to look in /etc/hosts by default, however if you're running 10.0 or 10.1, /etc/hosts/ is completely ignored unless you're in single user mode. To get around this, you have to edit your /etc/hosts file as per normal, then type the command sudo niload < /etc/hosts. This reads /etc/hosts and puts it into the NetInfo database.

If you're running Mac OS 9, however, it's called "Hosts" and is located in the Preferences folder inside your System Folder. To use your Hosts file, open up the TCP/IP control panel, make sure that the User Mode is set to Advanced, click the Use Hosts File button, and select the Hosts file.

Actually, I just remembered something: when you select the Hosts file above, you can choose the file from anywhere (it doesn't have to been located in System Folder:Preferences). What it does is copies this file, renames it to "Hosts", and sticks it in Preferences.

Now you can enjoy Xenex's ad-blocking goodness from the comfort of your very own Mac!

A simple, 'no additional software' method of blocking adverts on the web is to alter the local 'hosts file', in order to redirect requests for the banner ads from the real server either to the local machine, or into oblivion, preventing the banner from being downloaded, and therefore displayed.

The hosts file is (for most operating systems) a text file, each line of which contains an ip address, followed by the hostnames it should override. Hosts files are normally used in small networks that are too small to justify a centralised DNS, to map hostnames to devices that don't participate in a peer-to-peer name resolution system. In commonly-used operating systems, the hosts file is found in the following places:

  • Windows 2000/XP: %windir%/system32/drivers/etc/hosts
  • Windows 95/98/ME: %windir%/hosts
  • Linux: /etc/hosts

This is not without its drawbacks. Quite a few web browsers (Netscape navigator especially) won't render the page until they have downloaded all the images and scripts the page needs to display. If one of these images or scripts is located on a server blocked using the hosts file, it will not render the page until it has timed out trying to contact all the blocked servers, often several times.

This problem can be circumvented, either by redirecting the requests to a nearby webserver, or by running a webserver on the local machine, and redirecting requests to it. The webserver on the local machine need not be particularly powerful (Personal Webserver will do; Apache is probably a bit overkill), or even a proper webserver (look elswhere in this node for a perl script that pretends to be a webserver).

Though it can be made to work acceptably, this method is nothing better than a hack. It is less useful on modern, multi-user operating systems, as the changes to the hosts file affect all users, and often cannot be effected without being root/administrator. In addition, there is no way to block adverts on the same host as wanted content (for example, there is no way to block without also blocking

A much better solution is to use an advert-blocking proxy - privoxy is Free, and available for both windows and unix. As it matches URLs according to a list of regular expressions, it can block with much greater granularity than simply banning hosts. It can block patterns of URLs, catching all of a large ad company's servers in one line that does not need to be updated as the company moves its servers about. It serves replacement images (either transparent 'web bugs' to remove the banner entirely, or an unobtrusive checkerboard pattern, so that it is obvious that the page has been interfered with) for banners, so there are none of the 'time out' issues associated with host-based blocking. Privoxy can even alter the text of web-pages in transit to remove the place where the blocked advert would have been. Blocking and web-page altering use Perl's regular expression library, allowing for very powerful and customisable ad blocking and web-page alteration.

The hosts file does have its uses, however - several 'spyware' programs (creative labs media manager, gozilla, realplayer etc.) will attempt to 'phone home', sending personal information back to the authors, and providing no way of disabling this behaviour. In this case, adding a bogus entry for the 'phone home server to the hosts file will prevent the application from phoning home. In my opinion, the privacy benefits greatly outweigh the inconvenience of the program popping up a 'cannot connect to server' warning every so often.

I wrote the following to help deal with browsers complaining about refused connections.

#!/usr/local/bin/perl -w
# simple HTTP server. Responds blindly with a 1x1 transparent PNG. Use with a hosts file for ad blocking

use IO::Socket;
use Net::hostent;

$server = IO::Socket::INET->new(
  Proto => 'tcp',
  LocalPort => 80,
  Listen    => SOMAXCONN,
  Reuse     => 1
) ;

print "*** fakehttp - listening on port 80\n";

while ($client = $server->accept()) {
  print "Recived a request, responding with 1x1 transparent png...\n";
  print $client "HTTP/1.1 200 OK\nContent-Length: 68\n".
"Content-Type: image/x-png\n\n\211\120\116\107\015\012".
  close $client;

Log in or register to write something here or to contact authors.