The DNS Advertisement blocker is an extension of blocking advertisements through the local hosts file, except using a real DNS server instead. There are various upsides to this approach such as the ability to blocking entire domains and sub-domains, and being able to block specific hosts on devices where editing the hosts file is not possible or practical (eg. a phone or tablet).

I do not recommend running this on a Raspberry Pi simply because a large list of zones will take a considerably long time to load and DNS queries may appear to be sluggish.

Deprecated: Use OpenWRT or Pi Hole
This is way too much work. Instead, put a copy of OpenWRT on your Pi and install the adblock package. From there, you can load a bunch of hosts from ad lists (such as adaway) to blackhole.

The benefit of using OpenWRT is that you can also manage DNS/DHCP for your network in addition to acting as a DNS ad blocker.

The Code[edit]

This blocker contains the following components:

  • BIND DNS server
  • A generator script that creates zones based on an ad list
  • An ad list containing all servers to be blocked
  • Optionally, a HTTP server to capture all redirected HTTP traffic


Install BIND with the following configuration in /etc/named.conf


Make sure your named.conf contains something similar to:

options {
	listen-on port 53 { any; };

        directory "/var/named";
	dump-file 	"/var/named/data/cache_dump.db";
        statistics-file "/var/named/data/named_stats.txt";
        memstatistics-file "/var/named/data/named_mem_stats.txt";
	allow-query { any; };
	recursion yes;

	dnssec-enable yes;
	dnssec-validation yes;
	dnssec-lookaside auto;

	/* Path to ISC DLV key */
	bindkeys-file "/etc/named.iscdlv.key";

	managed-keys-directory "/var/named/dynamic";

logging {
	channel "querylog" {
		file "/var/log/named-query.log";
		print-time yes;
	category queries { querylog; };

include "/etc/named.advertisement.conf";
include "/etc/named.rfc1912.zones";
include "/etc/named.root.key";

zone "." IN {
	type hint;
	file "";

# If you need to forward zones, add them here. For example:
zone "" IN {
	type forward;
	forwarders {;;; };

Zones that are not defined should be looked up via root hints.[edit]

Create a script named with the following:

# Normal advertisement zones
echo "" > /etc/named.advertisement.conf
for i in `cat adlist.txt` ; do
	test -z $i && continue;
	echo "zone \"$i\" {" >> /etc/named.advertisement.conf
	echo "type master; file \"\";" >> /etc/named.advertisement.conf
	echo "};" >> /etc/named.advertisement.conf
	echo "" >> /etc/named.advertisement.conf

# Or if using systemd: systemctl restart named.service
/etc/init.d/named restart

rndc status|grep OFF && rndc querylog
# systemctl restart named

sleep 4
rndc reload
rndc flush


Get the latest adlist from DNS Ad Blocker/Ad List and place it into the adlist.txt file. Zones will be generated for each domain in this list which will also block all subdomains.[edit]

Create this zone file in /var/named/ This zone will be referenced for every zone that is created.

Replace the IP address with if you do not plan on running a HTTP server to capture the ad requests.

$TTL 1h
@               IN SOA  ns1.server. ns2.server. (
                        44      ; serial
                        3600    ; refresh
                        15M     ; retry
                        1W      ; expiry
                        1D )    ; minimum
@               NS      ns1.server.
@               A
*               A



// Attempt to get a redirect URL for click tracking sites
// if a GET parameter begins with http, then it's probably a link we can redirect to.
$redirect = "";
foreach ($_GET as $key => $value) {
	$value = urldecode($value);
	if (strtolower(substr($value, 0, 4)) == "http") {
		$redirect = $value;

if ($redirect != "") {
	echo "$redirect";
	echo "<form action=\"$redirect\" method=\"post\"><input type=\"submit\" value=\"Go\" /></form>";

// log requests to a log file
$fp = fopen('log.txt', 'a');
fwrite($fp, date("M j - H:i:s") . " : " . $_SERVER['REMOTE_ADDR'] . " - " .  $_SERVER['HTTP_HOST'] . " - " . $_SERVER['REQUEST_URI'] . "\n");

echo "<script>if (opener){self.close();}</script>";



Place this with the index.php file so that all requests go to the same PHP script.

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]