Once you've done all the preparation, setting your Pi up as an ad blocking AP is actually rather simple. The first thing we need to do is install the necessary software, and set it all up.

To start with, uninstall isc-dhcp-server and install dnsmasq. We'll be using dnsmasq as your dhcp server instead of isc-dhcp-server. You may want to do a sudo apt-get update first, and then execute the following:
sudo apt-get autoremove isc-dhcp-server
sudo apt-get install -y dnsmasq dnsutils
You can test that it installed properly by checking the status:
sudo service dnsmasq status
Create and edit a new file for the dhcp server settings:
sudo nano /etc/dnsmasq.d/dnsmasq.custom.conf
Add the following to that file:
interface=wlan0
dhcp-range=wlan0,192.168.42.10,192.168.42.50,2h
# Gateway
dhcp-option=3,192.168.42.1
# DNS
dhcp-option=6,192.168.42.1

dhcp-authoritative
Save the file by typing in Control-X then Y then return

These are the minimum settings required to get the dhcp server setup properly. There are a lot more settings available as examples in /etc/dnsmasq.conf if you want to configure it further.

Any files added to the directory /etc/dnsmasq.d are automatically loaded by dnsmasq after a restart. This is a convenient way to override or add new configuration files in Debian.
Next, let's update the dns nameservers to route to our pixelserv IP address first. We'll also go ahead and use the google nameservers.

Open the file with sudo nano /etc/resolv.conf and replace the contents with the following:
nameserver 192.168.42.1
nameserver 8.8.8.8
nameserver 8.8.4.4
After saving the resolv.conf file, let's restart dnsmasq to have the settings take effect:
sudo service dnsmasq restart
Try testing out the dns by using the dig command.
dig adafruit.com
Your results should be similar to the following:
Ok, now that we have all of that setup, let's create a script that will redirect known ad servers to an internal IP address. This will essentially cause any requests to generate an HTTP 404.

Open nano and create a file with the following command:
sudo nano /usr/local/bin/dnsmasq_ad_list.sh
Copy and paste the following into the file, and save and exit:
#!/bin/bash

ad_list_url="http://pgl.yoyo.org/adservers/serverlist.php?hostformat=dnsmasq&showintro=0&mimetype=plaintext"
pixelserv_ip="192.168.42.49"
ad_file="/etc/dnsmasq.d/dnsmasq.adlist.conf"
temp_ad_file="/etc/dnsmasq.d/dnsmasq.adlist.conf.tmp"

curl $ad_list_url | sed "s/127\.0\.0\.1/$pixelserv_ip/" > $temp_ad_file

if [ -f "$temp_ad_file" ]
then
	#sed -i -e '/www\.favoritesite\.com/d' $temp_ad_file
	mv $temp_ad_file $ad_file
else
	echo "Error building the ad list, please try again."
	exit
fi

service dnsmasq restart
Notice, in the above script there is a line starting with "#sed ". You can uncomment that, and modify it to remove your favorite sites from the ad blocking list so you can continue to support them. You can add as many of those lines as you'd like. One example would be:

sed -i -e '/ads\.stackoverflow\.com/d' $temp_ad_file

Next allow the file to be executed:
sudo chmod +x /usr/local/bin/dnsmasq_ad_list.sh
Manually run the script to test that it works:
sudo /usr/local/bin/dnsmasq_ad_list.sh
You should see the following output:
The above script will need to be run as root in order to properly update the configuration file. Basically, what it does it grab a pre-generated list of known ad server domain names from http://pgl.yoyo.org/adservers and save them to a file in /etc/dnsmasq.d/dnsmasq.adlist.conf. It then restarts dnsmasq.

You could manually do this every once in a while, but it's easier if we setup a weekly cron job with the root user.

Open crontab with the following command:
sudo crontab -e
Add the following line to the end of the file and save it:
@weekly /usr/local/bin/dnsmasq_ad_list.sh
Congratulations, you should now be blocking ads with your Raspberry Pi. You can test that it's working by executing the following:
dig doubleclick.com
And you should see that it gets routed to the 192.168.42.49 IP address:
One drawback to just stopping here is that we're now timing out on requests that are going to return an HTTP code of 404 for any ad servers. This won't be terribly fast. Another drawback is that any areas with ads will potentially still take up space in the page. We can fix this in the next section.

This guide was first published on Sep 13, 2013. It was last updated on Mar 08, 2024.

This page (Install Software) was last updated on Sep 05, 2013.

Text editor powered by tinymce.