Block Ads with /etc/hosts
The Hosts File (
/etc/hosts on Linux) is used to map hostnames to
IP-addresses when no DNS is available.
Although it is not intended for this, it can can be used to block network traffic
by re-routing requests for some.domain to a different address: localhost.
If you add the following line to the hosts file:
you will not be able to request websites (or anything, really) from google.com - your operating system will connect you to 127.0.0.1 instead. Since there is (normally) no web server to answer the request, the browser will display an error message.
If you create entries like this for all adservers, you will definitively not be able to see ads.
- Update 2014-07-06
Added some links to hosts files
Since there are a lot of adservers, we cannot create these entries manually. Instead, we will download a list of adservers to include in our hosts file. More specifically, we obtain a pre-made hosts file to append to our original hosts file.
A small script will do this for us and can also be used to update the file. The script assumes that the existing hosts file contains manually created configuration entries which should be preserved.
This is done in the following steps:
Download the hosts file.
Get our original hosts file.
Extract any content before a special "marker" comment (the manual configuration to be preserved).
Append the "marker comment" in the preserved part of the hosts file so that the "marker comment" is the last line of the original file.
Append the bad hosts from the downloaded file after the marker.
The script looks like this:
#!/bin/sh # block servers based on /etc/hosts # Downloads a hosts-file with hostnames to be blocked. # Takes the current hosts file and # appends the downloaded hosts to the original. src='http://hosts-file.net/download/hosts.zip' dst='/tmp/downloaded-hosts-file' wget $src --output-document $dst # The lines before the marker comment # are the user-created entries to be preserved. marker='#---generated-content---' sed -e '/'$marker'/,$d' < /etc/hosts > /tmp/hosts echo $marker >> /tmp/hosts echo "# Generated: `date`" >> /tmp/hosts echo '' >> /tmp/hosts # unzip options: # -C case-insensitive (match "HOSTS" or "hosts") # -p output to pipe # # egrep-filters # exclude comments # exclude standard entry `127.0.0.1 locahost` # allow only re-route to 127.0.0.1 unzip -Cp $dst | egrep -v '^#|^.*?localhost' | egrep '127.0.0.1' >> /tmp/hosts rm $dst echo "Complete. Updated hosts file is /tmp/hosts" exit 0
The script does two other things: If filters the downloaded "bad hosts" file to contain only entries that start with "127.0.0.1" and removes the default entry for:
this entry must exist in your
but should already be contained in your original file.
A correct /etc/hosts is essential for the networking functionality of your computer. You should absolutely create a backup of the original file.
The script will not do that for you.
It will not modify your /etc/hosts but instead
write the modified file to
To start using the modified file (as root):
Should You Do This?
Using ''/etc/hosts`` is really reliable, there will definitively no networking with the blocked hostnames. It works for all applications.
This is an all or nothing solution. There will be no outgoing requests for the blocked hosts as long as the hosts file is in place. Period.
It is not realistically possible to manually control the list of blocked hosts. You have to trust whoever compiled the list.