Press "Enter" to skip to content

How to Create an Ad Blocker with Raspberry Pi and Pi-hole

Friendly warning: you should only install Pi-hole on your Raspberry Pi if you plan on dedicating it to this task. Expect to have issues if you move your Pi to another network or try to run other server functionality on it alongside Pi-hole.

Remember when ad blockers worked?

The internet was such a lovely place.

The ads were gone. Nobody bugged me. It was bliss.

And then this happened:

ad blocker popup

Site after site, you’re bombarded with these popups that ask (and sometimes force) you to disable your ad blocker.

Now you have to disable your ad blocker to view a site that has even more ads than it did a few years ago.

To me, this is the best reason to run Pi-hole on your home network.

With Pi-hole, none of these sites even know you’re blocking their ads, so not only are there zero ads, but also, you won’t see any of the “disable your ad blocker” popups.

But that’s not the end of the story. Pi-hole has many other advantages too.

The benefits of using Pi-hole

Browser-based ad blockers hide the ads on the page, but your browser still downloads them. Since Pi-hole operates at the DNS level (I’ll explain later), your Raspberry Pi ad blocker will stop the ads from ever getting downloaded. This leads to a noticable performance increase.

Better still, Pi-hole’s network-level operation means it blocks ads for all internet-connected devices in your home (yes, even your TV).

no ad roku tv
There’s an empty box on the right where an ad would normally show

Pi-hole also blocks more ads in general than most browser-based ad blockers. For instance, those content suggestions that tend to show up after articles get blocked too:

content suggestion ads

And like I mentioned before, you don’t see any of those popups asking you to disable your ad blocker.

The main drawback to running Pi-hole is that you will want to dedicate a Pi for it to avoid causing any network errors or performance issues.

As long as you’re fine with that, let’s get on with the ad blocking!

What you’ll learn

In this beginner-friendly tutorial, you’ll learn how to install a free tool called Pi-hole on your Raspberry Pi that turns it into a DNS server.

You’ll route all requests through the Pi so that Pi-hole can check if the request is for an advertisement and block it if it is. This will work for all your devices without installing any other software.

You’ll even get a cool dashboard where you can review your stats and ad blocking performance.

pi hole dashboard

And when we’re finished, we’ll put this ad blocking technology to the ultimate test: The Daily Mail 🙀

daily mail

Let’s get started!

Introducing Pi-hole

Pi-hole is a popular (and free) program that will let you block ads with your Raspberry Pi.

e

Pi-hole works at the DNS level which means that it intercepts the ads before they’re even loaded. It checks every request against a list of known advertisers and if it finds a match, it blocks the request.

The end result is that you get to browse the internet, minus all the ads.

Before we get to the installation, let’s address a few common questions.

What if you’re not home?

Since Pi-hole is tied to your home network, it means you won’t have ad blocking when you’re not connected to your router.

However, you can use Pi-hole with OpenVPN to get remote access to your home network. This will allow you to use your home Wifi on your mobile device even when you’re away.

I won’t be covering VPN usage in this tutorial, but now you know it’s an option.

Can it block Youtube ads?

Everyone (myself included) immediately wants to know if Pi-hole can block Youtube ads.

There are lots of tutorials that make it sound like Pi-hole can do this.

It can’t.

The fundamental issue is that when you’re on ExampleNews.com, they display ads from ad networks like Google AdSense. This means that with Pi-hole, you can block the requests going to the Google AdSense servers and effectively block all the ads.

Youtube is different because it is an ad network in and of itself. This means that you can’t easily separate the ad requests from requests for the videos because it’s all coming from the same domain.

There is a massive thread on the Pi-hole forums where people discussed this and tried their hardest to find workarounds and solutions, and after two years, it ended like this:

pi hole thread closed

In other words, don’t waste your time.

You can block Youtube ads if you watch Youtube in a browser by using a browser-based ad blocker like Ad Block Plus.

If you’re watching in the Youtube app on your phone or Smart TV, then the only reliable option is to purchase Youtube Premium.

If this changes at any point in the future, I’ll make sure to update this post.

With the pros & cons out of the way, let’s dive into the technical details.

How Pi-hole works

Understanding the underlying tech will help you a lot if you need to debug along the way.

The quick explanation

All of your internet traffic is filtered through Pi-hole. It keeps a blacklist of domains that serve ads. When it sees a request for one of these blacklisted domains, it blocks it so no request is made.

The detailed explanation

As I mentioned already, Pi-hole works at the DNS level which allows it to block requests before their resources are loaded. To understand this, you need to know a bit about how DNS servers work.

Every website and internet-enabled device has an IP address. For instance, this website’s IP address is 104.18.54.224. Obviously, the internet would be a cognitive load nightmare if we had to use IP addresses to visit sites instead of simply typing “Youtube.com.”

This is where DNS servers come in.

DNS servers enable us to use common language names for sites, like “Twitter.com.” When you request a site via its domain name, a DNS server looks up the associated IP address and returns it to your browser to then contact the server with that IP address.

Every webpage also has resources it needs to function. Some of these resources include image files and CSS stylesheets loaded from the same domain. For example, all the images in this article are loaded from raspberrydiy.com. These images do not require an additional DNS lookup since they’re served from this domain.

Sometimes, webpages also need to request resources from an external site and that requires an additional DNS lookup. This means that a webpage that loads ads will need to make a request to an external domain like ads.adnetwork.com in order to load the ads. This gives Pi-hole an opportunity to block the request without ever loading the ad.

Now let’s put it all together.

You’re going to tell your router to use your Raspberry Pi as the DNS server. Every request gets sent to the Pi and Pi-hole checks the domain against its blacklist. Matches are blocked while all other requests are passed along to a DNS resolver. The DNS resolver returns the IP address to the Raspberry Pi which sends it back to your browser which then contacts the server located at that IP address.

pi hole how it works

A request to fonts.google.com? That’s for a web font, pass it through. A request to doubleclickbygoogle.com? That’s an ad network, block it.

This makes websites run faster because you’re skipping the DNS lookup and you’re not downloading the resulting ads. Plus, you are most likely not using the ultra-fast public DNS resolver you’re about to find in this tutorial.

But I think that’s enough background for now.

Let’s move onto the steps required to install Pi-hole and configure your Raspberry Pi to block ads.

How to install Pi-hole

Pi-hole has an automated installer that will walk you through the entire process.

It’s always a good idea to update your Pi’s packages before installing new programs. Run the following two commands to do that:

sudo apt update
sudo apt upgrrade

Next, run the following command to install the Pi-hole package:

curl -sSL https://install.pi-hole.net | bash

You’ll see a window in the terminal like this show up:

Pi Hole installer
This shows up in the terminal so you can complete these steps via SSH

Hit Enter to continue.

Pi-hole will let you know that it is a server so it requires your Raspberry Pi to have a static IP address. Normally, devices that connect to your router will routinely be assigned a new IP address which is a problem. You’ll give your Pi a static IP later in the process when configuring your router, so hit Enter now to continue.

Set a static IP

Next, you’ll choose whether your Pi will be connected to your router through an ethernet connection (eth0) or a Wifi connection (wlan0).

Choose an interface

The next step asks you to select an upstream DNS provider. I referred to this earlier as a “DNS resolver,” which is the more common term. This is the DNS service that will actually look up IP addresses for domains and return that IP address to Pi-hole.

Select an upstream provider

While Google is a solid choice, if you arrow down the screen you’ll find Cloudflare which is what I recommend and use for my configuration.

Select Cloudflare for DNS provider

Cloudflare is best known for their CDN which offers DDoS protection and a web application firewall. I’m actually using it on this site right now.

Recently, they launched their own “privacy-first” DNS resolver called 1.1.1.1. Cloudflare is usually a B2B company, but this their first foray into public consumer products. They even released a mobile app.

As a fan of Cloudflare, I was already leaning towards their service, and then I found that it performs much faster than competitors (including Google) which is why I ended up using it. If you choose a different upstream provider, the following steps will be mostly the same and I will point out where it differs.

Continuing on, Pi-hole will ask you which lists you’d like to use to block ads.

Choose ad block lists

These are simple text files that contain a list of domains, and combined they create your “blacklist.” Pi-hole checks the blacklist to know which requests to block.

The two that are selected work well, and I can’t think of any reason to deselect either. You can easily add more lists later through the admin dashboard if you find you’re not getting enough coverage.

Next, you’ll select which protocols to block ads over.

Select IP protocols

Most websites still run over IPv4, but IPv6 is becoming more prevalent. Unless you’ve got a good reason to do otherwise, keep both selected.

Next up, Pi-hole will ask you if you’d like to use your Pi’s current IP address.

Set a static IP address

You can modify your IP now if you’d like, but I continued with the IP address already given to my Pi as I didn’t see any need to change it.

Pi-hole will then warn you that your router could potentially assign the IP address to another device. This is easy to get around by manually setting your address in your router’s settings, and we’ll do that soon.

Warning about IP conflicts

Then Pi-hole will ask you if you’d like to install the web admin interface. That was the colorful dashboard featured in the intro of the tutorial, and I recommend enabling it.

Install the web admin interface

Pi-hole will then ask if you’d like to install the web server. Unless you already have a web server installed that you’d like to integrate with Pi-hole, leave this on as well.

Install lighttpd server

Next, you can decide if you’d like to keep a log of all the queries. I would leave this on so you can review your logs while you get Pi-hole working, and you can always disable it later.

Enable query logging

And the very last thing you need to decide is the privacy mode. This essentially controls how much information about the sites you visit is saved in the Pi-hole dashboard. I think it helps to see everything at first, so I’d recommend leaving it on for now as it is easily changed later via the dashboard.

Select a privacy level

Once you complete this step, you should see a success message with the device’s IP address, log directory, admin interface URL, and password for the admin login.

Installation complete
Make sure to save this password before hitting “Ok”

On a fresh Pi, the installation should run smoothly and you will be able to view the Pi-hole dashboard by visiting your Pi’s IP address followed by “/admin.”

That said, the dashboard is going to be empty because none of your devices are using Pi-hole yet. Now that Pi-hole is installed, you just have to tell your router to use it as the DNS server to complete the setup.

But first, I have a few debugging tips for an error I encountered the first time I set this up.

Debugging the lighttpd error

If you already have lighttpd installed for running a web server with your Pi, you might encounter this error during the Pi-hole installation:

pi hole installation frozen

This could be patched by the time you read this guide, but it’s due to a minor compatibility issue with Pi-hole and the newest version of Pi OS (Buster).

There is a chance that a permission issue stopped some files from being copied correctly.

Running these two commands in order should fix the error:

sudo apt purge lighttpd 
pihole -r

If you still have any trouble, try running this command as well which copies over one file in particular that often gets dropped.

sudo ln -s /usr/share/lighttpd/create-mime.conf.pl /usr/share/lighttpd/create-mime.assign.pl

After this is complete, you should be able to visit the Pi-hole dashboard at your Pi’s IP address followed by /admin.

Configure your router

The odds that we have the same router are slim, so the steps I show here won’t perfectly match those required for your router. However, the basics are the same, and I’ll do my best to be helpful.

Before you get started, I have a few tips to make this process smoother and less annoying.

Making your life easier

You will likely make a mistake that “breaks” your internet. You might need to reset your router.

To make things easier for yourself, have your phone at the ready so you can Google things using mobile data. Additionally, if you have access to a public Wifi network from your home, that will help too, and you can save this page as a PDF to keep access if your connection goes down.

Okay with that out of the way, lets get to the configuration.

Configuring your router

In the official instructions, they recommend using your Raspberry Pi as the only DNS server and so that’s what we’ll be doing.

Start by visiting your router’s settings page. You can normally access it at http://192.168.1.1. You can also look up your router’s brand to see if they have an alternate settings page URL. For instance, ASUS routers can use router.asus.com.

And if you’re a genius like me, you’ve forgotten your password and get to reset the router first 😉

From your router’s admin screen, locate and visit the LAN settings page.

lan settings
Located here for any other ASUS users

Next, look for your DHCP settings page. For ASUS routers, it’s a tab within the LAN settings, but it might be a top-level menu item for your router.

On this page, there’s a few things you need to do.

set pi as dns

First, enable manual assignment of IP addresses. This is so you can set a static IP address for the Pi that never changes.

Next, select your Pi to manually assign its current IP address. For ASUS users, the dropdown at the bottom will let you select the “raspberrypi” by name. There will be two instances of “raspberrypi.” Select the one with the IP address that Pi-hole used (the IP address from the installation success screen). Also, make sure to press the plus button to actually add it.

Lastly, enter the Pi’s IP address as the DNS server and apply your settings.

Your computer may not use these new network settings until it’s restarted, so now is a good time to double-check and make sure you’ve got everything right. Then reboot your computer, and Pi-hole should be blocking ads at long last.

If you visit http://pi.hole/admin you should see the Pi-hole dashboard already live with data on the blocked advertisements.

pi hole dashboard
It also reports the Pi’s temperature at the top left, which is handy

This dashboard gives you a nice overview of Pi-hole’s performance, but if you click the Login link on the left, you can get a much more complete dashboard. Use the password that showed up on the installation success page to login.

pi hole complete dashboard

There’s a lot of interesting stuff to pour through from the most commonly blocked ad networks to the whitelist and blacklist management, but I’ll leave that to you to enjoy on your own time.

Enjoy your ad-free home network

With Pi-hole installed and the router configured, it’s time to put your Raspberry Pi ad blocker to the ultimate test.

Cue the boss fight music.

daily mail ads blocked

Be honest, do you even recognize it?

With Pi-hole running on your network, you can finally find out what Kim Kardashian had for breakfast in peace.

Kidding aside, thanks for sticking with me through this guide – it was a long one – but I had a lot of fun putting this together.

If you want to learn more cool ways to use your Pi, you might finding something of interest in my full list of Raspberry Pi projects here.

And if you have any questions or tips to share with other Pi lovers, please post in the comments section below.

Thanks for reading this guide on how to create a Raspberry Pi ad blocker!

My Favorite RPi Products

Get new post notifications

Subscribe to get new projects & tutorials in your inbox

    Unsubscribe at any time in one click.

    Be First to Comment

    Leave a Reply

    Your email address will not be published. Required fields are marked *