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:

you will not be able to request websites (or anything, really) from - your operating system will connect you to 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:

  1. Download the hosts file.
  2. Get our original hosts file.
  3. Extract any content before a special "marker" comment (the manual configuration to be preserved).
  4. 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.
  5. Append the bad hosts from the downloaded file after the marker.

The Script

The script looks like this: (Source)


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

wget $src --output-document $dst

# The lines before the marker comment
# are the user-created entries to be preserved.
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 `   locahost`
#  allow only re-route to
unzip -Cp $dst | egrep -v '^#|^.*?localhost' | egrep '' >> /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 "" and removes the default entry for:   localhost

this entry must exist in your /etc/hosts 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 /tmp/hosts.

To start using the modified file (as root):

# cp /etc/hosts /etc/hosts.bak
# mv /tmp/hosts /etc/hosts
# chown root:root /etc/hosts

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.

Sources for Hosts files

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.

Docutils System Messages

System Message: ERROR/3 (<string>, line 128); backlink

Unknown target name: "".