Raspberry Pi – Say Goodbye to Ads with Pi-hole


Hello everyone. Today I stumbled over a great way to get rid of nasty advertisements: The Pi-hole. I thought you might like this too so I wanted to share it. Here is how it works:

The Pi-hole uses a simple technique to get rid of advertisements: First you install a webserver on your Raspberry Pi which will serve an empty web page. Next you download the domain names of the advertisement servers you want to block and create DNS entries for them in the DNS server you install on your Raspberry Pi. And these DNS entries all point to your webserver which will serve you an empty page instead of advertisements. Pretty simple right? It´s practically DNS Spoofing to be specific. Now that we know how this works let´s get going.

Before you start with the steps below please have a look at this post (The IP addresses and configuration files I used in my current post are based on that post) and set up your Raspberry Pi as a wireless router. This way all your clients will be configured correctly by simply connecting to your Raspberry Pi´s wireless network.

The first step is to update your Raspberry Pi to avoid issues:

sudo apt-get update
sudo apt-get upgrade

Now install the webserver which will serve the empty html page for us. To do so run:

sudo apt-get install lighttpd

Now set the necessary permissions for the webserver.

sudo chown www-data:www-data /var/www
sudo chmod 755 /var/www

Next create a new directory called pihole and create and empty html page called index.html in it:

sudo mkdir -p /var/www/pihole
sudo touch /var/www/pihole/index.html

Next edit the lighttpd webservers configuration file.

sudo nano /etc/lighttpd/lighttpd.conf

Add the following line to the end of the file:

$HTTP["host"] =~ ".*" {
url.rewrite = (".*" => "pihole/index.html")
}

Next install the dnsutils and dnsmasq:

sudo apt-get install dnsutils dnsmasq

Stop the dnsmasq service till we finish our configuration.

sudo service dnsmasq stop

Next rename the dnsmasq.conf file (Do not delete it just to be safe):

sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.bak

Now create a new dnsmasq.conf file:

sudo nano /etc/dnsmasq.conf

Add the following code to the file:

domain-needed
interface=wlan0
min-port=4096
cache-size=10000
log-queries
bogus-priv

We use the wlan0 interface since this is the interface where our clients will connect to.

Next you need to edit the resolv.conf file:

sudo nano /etc/resolv.conf

Replace all contents of the file with the following code: (Besides our own DNS server we add Googles DNS servers to resolve domain names)

nameserver 127.0.0.1
nameserver 8.8.8.8
nameserver 8.8.4.4

Next create the script which will get the DNS domain names from the ad servers we want to block:

sudo nano /usr/local/bin/gravity.sh

Here is the script:

#!/bin/bash
adListURL="http://pgl.yoyo.org/adservers/serverlist.php?hostformat=dnsmasq&showintro=0&mimetype=plaintext"
# Address to send ads to (your Raspberry Pi's IP)
piholeIP="192.168.0.1"
adFile="/etc/dnsmasq.d/adList.conf"
eventHorizion="/etc/dnsmasq.d/adList.conf.tmp"
curl $adListURL | sed "s/127\.0\.0\.1/$piholeIP/" > $eventHorizion
if [ -f "$eventHorizion" ];then
mv -f $eventHorizion $adFile
else
echo "Error building the ad list, please try again."
exit 1
fi
service dnsmasq restart

Set the necessary permissions for the script by running:

sudo chmod 755 /usr/local/bin/gravity.sh

Now run the script to get the DNS domain entries of the ad servers into your DNS server:

sudo /usr/local/bin/gravity.sh

Next set up a cronjob to update your DNS entries to keep your network ad-free. To do so run:

sudo crontab -e

Now add the following line to the file to run the update cronjob weekly:

@weekly /usr/local/bin/gravity.sh

Now we need to update the dhcp.conf file to redirect all clients DNS queries to our DNS server on our Raspberry Pi. Edit the file by running:

sudo nano /etc/dhcp/dhcp.conf

Now edit “option domain-name-servers” and make sure to replace the IP addresses with the IP address of your Raspberry Pi (mine is 192.168.0.1):

subnet 192.168.0.0 netmask 255.255.255.0 {
range 192.168.0.10 192.168.0.250;
option broadcast-address 192.168.0.255;
option routers 192.168.0.1;
default-lease-time 600;
max-lease-time 7200;
option domain-name "local-network";
option domain-name-servers 192.168.0.1;
}

That´s it. Now reboot your Raspberry Pi to apply the changes.

sudo reboot

Ok. Your Raspberry Pi Pi-hole is now ready to use.

Here is a picture of a website (http://winfuture.de/) with advertisements without the use of Pi-hole:

winfuture_not_ad_free

And here is a picture of the same website without advertisements by using Pi-hole:

winfuture_ad_free

And all that´s necessary is that you are connected to the wireless network of your Raspberry Pi to surf advertisement free. Pretty cool right?

I hope you liked my post and I hope to see you again next time 🙂

Sources:

http://jacobsalmela.com/raspberry-pi-block-ads-adtrap/

Advertisements
Tagged

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: