Blocking ads with a server, using dnsmasq

Remove bind9:

apt-get remove bind9

Install dnsmasq:

apt-get install dnsmasq
service dnsmasq start

Now, run a script to auto download list of ad servers:


### short script that downloads a list of ad servers for use with
### dnsmasq to block ads.

# the ipaddress where we want to send the requests to, instead of the
# bannerservers


# the args to add to the request to the yoyo server, to tell it that we want
# a hosts file and that we want to redirect to the addcatcher

# URL of the ad server list to download

# location of a file where hostnames not listed can be added

## command to reload dnsmasq - change according to your system
## not sure if we need this for dnsmasq
reloadcmd='/etc/init.d/dnsmasq restart'

# temp files to use

# command to fetch the list (alternatives commented out)
fetchcmd="/usr/bin/wget -q -O $tmpfile $listurl"


# add the extras
 [ -f "$extrasfile" ]  && cat $extrasfile >> $tmpfile

# check the temp file exists OK before overwriting the existing list
if  [ ! -s $tmpfile ]
echo "temp file '$tmpfile' either doesn't exist or is empty; quitting"

# get a fresh list of ad server addresses for dnsmasq to refuse
cat $configfile | grep -v "address=" > $tmpconffile

while read line; do
    echo "address=\"${ADDRESS}\"" >> $tmpconffile
done < $tmpfile

mv $tmpconffile $configfile
rm $tmpfile

Name it as update_bannerhosts

Now, copy it to /usr/local/bin/update_bannerhosts

cp update_bannerhosts /usr/local/bin/
chmod 755 /usr/local/bin/update_bannerhosts

Add it to cron:

0 1 * * * /bin/sh /usr/local/bin/update_bannerhosts

Run it for the first time:


You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.