How to block ads using a Raspberry Pi and Pi-hole

In this article, I will show you what Pi-hole is, how you install it on a Raspberry Pi, and ultimately, how you can block advertisements on all devices in your home network, including your smartphones, tablets, and Smart TVs.

Are you tired of seeing advertisements on virtually every website? Sure, you can install uBlock Origin or a similar browser extension, but that isn’t the best solution to this problem. Many websites can recognize when you’re using an ad blocker and won’t even let you view their site in case you do use one. Additionally, Google is well on its way to end the possibility to use ad blocker extensions in Google Chrome entirely. That’s why I’ve already ditched Google Chrome and now use Firefox instead. But still, a browser extension works only in your—wait for it—browser. The browser on your computer, at that. What about your smartphone? Or Smart TV? Or your partner’s smartphone? You could do the same work over and over again and install/configure a browser extension on all your different computers … or you could do it just once on the router level. Furthermore, you don’t see ads just in your browser. Even if you installed a browser extension on your computer/smartphone, how do you get rid of advertisements within an app? You need something better than an ad blocker as you know it.

Enter Pi-hole.

Why Pi-hole?

Pi-hole is a software that you typically install on a Raspberry Pi. A Raspberry Pi is a very affordable computer (that costs between 5 and 40 bucks) that consumes next to no energy, making it very cheap to run it 24/7. You plug this little device into your router and from then on all ads will already be filtered out when the data continues its way from your router to your laptop, smartphone, or TV. This way, ads are not only blocked for all devices in your home network, which means you don’t have to individually set up and configure every single device. It also means that ads will also be blocked on devices that simply cannot be configured to block ads, such as your Smart TV. Although there’s no way to block ads on these devices themselves, when you block them on the Raspberry Pi for your entire home network, this doesn’t matter. In this tutorial, I will show you what you need for that and how to achieve it.

By the way, you don’t specifically need a Raspberry Pi for this tutorial. You could also run Pi-hole in a Docker container on your NAS or any other server if it runs 24/7. However, this is much more costly with regard to energy consumption and, therefore, not recommended. Even if you had only two harddrives in your NAS, running the NAS itself plus several harddrives 7 days a week for 24 hours a day costs you at least 4 bucks per month for the energy consumption alone. A Raspberry Pi in contrast consumes next to no power at all. Seriously. The energy consumption of the Raspberry Pi Zero is between 0.5 and 0.7 W. While a Raspberry Pi sets you back 30–75 bucks upfront, depending on the model you choose, you will make up that money within the first year due to the energy savings. After all, you want your ads to be blocked 24/7, don’t you?

Which Model Do You Need?

As I already mentioned, there are multiple models of the Raspberry Pi available. The most affordable one will cost you about 35 bucks in total. A more powerful one will cost you about 75 bucks in total. Pi-hole needs almost no computing power, so the affordable option is perfectly fine. Since I built a Raspberry Pi not only for myself but also for non-savvy family members, I will explain both options. For my family members I built the affordable option as a set-it-and-forget-it solution that runs Pi-hole and nothing else. On my own Raspberry Pi, I want to run a few more other things as well, so I myself opted for the 75-bucks-solution. Those other things I want to run include a VPN server to which I can connect when I’m out and about, so that I don’t need to pay for a commercial VPN provider. These commercial VPN providers are not what you think they are and are mostly useless. At least they do not deliver what they promise to deliver.

What You Need to Buy

Anyway, here’s what you need to buy for the affordable option:

  • a Raspberry Pi Zero (~ $5)
  • a microSD card; 16 GB is more than enough (~ $5)
  • a power brick with 2 Ampere with a USB-A to Micro-USB cable; alternatively, the official Raspberry Pi wall charger (~ $10)
  • a case for the Raspberry Pi (ranges from $1 to $20; can also be 3D-printed by yourself)
  • a USB-A to Ethernet adapter (~ $15)
  • a Micro-USB to USB-A adapter (~ $2) for the aforementioned Ethernet adapter

Here’s what you need to buy for the more powerful option:

  • a Raspberry Pi with at least 512 MB RAM; the 2 GB model is sufficient (~ $40)
  • a microSD; 16 GB is more than enough (~ $5)
  • a 15 W power brick with a USB-A to USB-C cable; alternatively, the official Raspberry Pi wall charger (~ $10)
  • a case for the Raspberry Pi (~ $20)

Since the affordable option does not have an Ethernet port, it will have a few dongles attached to it. If you want a cleaner look and don’t mind spending the extra bucks, I would go for the more powerful option, i.e., the regular Raspberry Pi. If you’d rather safe some money and don’t care how your solution will look because it will be tucked away anyway, go for the more affordable option.

For the more powerful Raspberry Pi you definitely need cooling. You could use a fan (active cooling) in your case for that. I recommend using the excellent Flirc case which is made of aluminum and thus cools the Raspberry Pi passively, i.e., there’s no need for fans, which is much quieter.

If you decide to buy the affordable Raspberry Pi Zero, you will need an Ethernet dongle. Here’s some noteworthy information. Internally, all these USB-to-Ethernet adapters you can find on Amazon are the same. They are all produced by the same OEM manufacturer in Asia and then get their own branding. There really are only two different groups of dongles, depending on the chipset they use:

  • the ones using the older chipset ASIX AX88179
  • the ones using the newer chipset Realtek RTL8153

When shopping for a Ethernet adapter, don’t be deterred by the 1-star reviews. These people complaining that their dongle does not work just could’t figure out how/where to get the necessary drivers. This brief sidenote is not a necessary step for setting up your Raspberry Pi—it’s just a noteworthy piece of information if you plan to re-purpose that Ethernet adapter in the future. The dongles with the Realtek chipset work out of the box on your Windows/Linux/macOS computer. If you wanted to use a dongle using the ASIX chipset with your main computer, you’d need to first download and install the drivers yourself. On macOS, if you installed Homebrew, you can install those drivers like so (in case you want to use such a third-party adapter with your Mac computer rather than the official Apple one):

brew install --cask homebrew/cask-drivers/asix-ax88179

Many of the 1-star reviews can be explained by people not knowing they had to install drivers or not finding them. In case you also want to buy an USB-to-Ethernet adapter for your Nintendo Switch, the Nintendo Switch supports only adapters with the ASIX AX88179 chipset, not adapters with the technically superior Realtek chipset. But I digress.

Installing the Operating System

The good thing about the Raspberry Pi is that there are many tutorials out there, including how to install the operating system Raspberry Pi OS (previously called Raspbian) on your Raspberry Pi. What you need to do is image your microSD card with the image of the Raspberry Pi operating system. There are many different ways to achieve that, including:

  • the easy-to-use tool NOOBS
  • downloading the image onto your computer and then use balenaEtcher
  • downloading the image onto your computer and then manually doing the imaging the hard way using Unix command-line tools like dd

Probably the easiest method is using the new Raspberry Pi Imager, introduced in March 2020, which you can get here. If you’re using macOS, you can also get the Raspberry Pi Imager using Homebrew like so:

brew install --cask raspberry-pi-imager

Then, follow the instructions given by the Raspberry Pi Imager, which are

  1. Insert the MicroSD card into your computer
  2. Open Raspberry Pi Imager and choose the OS from the list presented
  3. Select the MicroSD card
  4. Click “Write”

Enabling SSH

Before you can configure your Raspberry Pi in the next step, you need to first enable the SSH server on your Raspberry Pi so that you can connect to it. To do so, all you need to do is create a file named ssh on the microSD card. Your microSD card was probably ejected after the last step had finished, so pull the microSD card out of your card reader and plug it back in. The quickest way to then create a new file on your microSD card would be entering these two commands into the terminal:

cd /Volumes/boot
touch ssh

Configuring the Raspberry Pi

After you’ve installed the operating system, you should make several configurations. But first, log in to your router’s admin site and assign the Raspberry Pi a static IP address. You can then log in to your Raspberry Pi either by connecting a display and a keyboard to your Raspberry Pi, or by connecting from another computer via SSH. Since the affordable Raspberry Pi Zero lacks a lot of common ports, connecting a display and keyboard to it proves to be much more difficult than it is with the larger Raspberry Pi. I therefore recommend following the SSH approach. (Not being comfortable with connecting via SSH is also one reason why you might prefer the more expensive/powerful Raspberry Pi.) Anyway, to connect via SSH use this command with your Rasapberry Pi’s own IP address:

ssh pi@192.168.x.x

The default user name is pi. You’re now prompted to enter the default password which is raspberry. You’re also asked to change that default password. Simply enter the command passwd, then the current password (raspberry) and then provide your new password twice.

In case you followed my guide on how to use SSH keys and don’t want to type your password every time you want to log in to your Raspberry Pi, you can copy your SSH key to your Raspberry Pi like so:

ssh-copy-id -i ~/.ssh/ pi@192.168.x.x

Next, you should update the packages on your Raspberry Pi:

sudo apt update && sudo apt upgrade -y

If you want to reboot your Raspberry Pi, do it like so:

sudo reboot

Installing Pi-hole

After you’ve installed the Raspberry Pi OS and configured your Raspberry Pi, it’s finally time to install Pi-hole. On your Raspberry Pi, open the terminal. Then, enter this command in the terminal:

curl -sSL | bash

This will download and install Pi-hole which may take a while. First, you will see some warning telling you something about Root user check and Script called with non-root privileges. You can safely ignore these warnings, since the installer will automatically try again—this time with the necessary rights—and succeed. From there, follow the instructions you’re given. Essentially, you have to choose the networking interface (your Ethernet interface) and a DNS provider. Personally, I’d choose a DNS provider that respects your privacy. Since your goal is partly to block Google ads so that you are no longer tracked by the Google surveillance machinery, it’s of no use to then opt for Google DNS. But that’s just my opinion. Feel free to use whichever provider you like. At the end of the installation, you will be presented an “Web interface password”. Save it somewhere, for example, in your password manager. Then, change this password using the following terminal command:

pihole -a -p

Settings in the Router

After you’ve installed Pi-hole, you need to go to your router again. Go to your router’s DNS settings and set your Raspberry Pi as the new DNS server. That is, enter your Raspberry Pi’s IP address in the DNS section of your router. Enter both the IPv4 and the IPv6 adress.

Configuring Pi-hole

You can make changes to Pi-hole by entering the IP adress of your Raspberry Pi followed by /admin into your web browser’s adress bar. For example, Here, you need to enter the password you were presented earlier when you installed Pi-hole. In this control panel you can add websites to your whitelist (or blacklist), import blocklists, and tweak other settings. How exactly to configure Pi-hole, how to automate the import of blocklists, how to use Pi-hole instead of your router as your DHCP server, or how to use Unbound as your DNS resolver rather than, say, Google or Quad9 deserve a blog post of their own. But you can also call it a day after following only the article you’re reading right now and already enjoy a less insufferable web.

Final Words

Most advertisements should now be blocked on all your devices. How cool is that? I hope you find this article helpful. If so, please consider leaving a comment in the box below or say Hi on Twitter. Thanks, and be well.

As an Amazon Associate I earn from qualifying purchases.
Built with Hugo
Theme Stack designed by Jimmy