Block ads with /etc/hosts
Author: | akeil |
---|---|
Date: | 2013-10-23 |
Updated: | 2014-07-06 |
Version: | 3 |
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:
127.0.0.1 google.com
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.
Note
- 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
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:
127.0.0.1 localhost
this entry must exist in your /etc/hosts but should already be contained in your original file.
Note
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 /tmp/hosts.
To start using the modified file (as root):
Should You Do This?
Advantages: |
Using ''/etc/hosts`` is really reliable, there will definitively no networking with the blocked hostnames. It works for all applications. |
---|---|
Disadvantages: |
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. |
Sources for Hosts files
- hostsfile.org_ (text)
- hpHosts (zipped)
- MVPS (text)
There is a similar post on this topic. There is also a cron script that does this.
hosts-update is program to automate the process. It uses the MVPS hosts file. For Arch Linux, a package is available in the AUR.