Block Ads For Your Entire Home Network With A Raspberry Pi

Jeff Morhous - Feb 18 - - Dev Community

Updated for 2024! Run a DNS sinkhole on a Raspberry Pi, including the Raspberry Pi 5.

I got a Raspberry Pi for my birthday last year and have been suffering analysis paralysis, trying to figure out what I want to do with it.

I recently came across some open source software called Pi-hole, that boasts the ability to block ads for your entire network. Perplexed, I had to see how this worked, so here’s how I set up my Raspberry Pi to act as a black hole for all things advertisement related. Not only will you experience a (mostly) ad-free internet, but things like Smart-TV’s and other “sketchy” analytics tools won’t be able to send their data home. Hurray privacy!

If you have any questions, leave a response below or feel free to tweet me!

Step 1: Aquire Hardware 🖥

The Raspberry Pi is an incredibly cheap computer, which makes it a great candidate for such a specific task.

I have tried this on both the Raspberry Pi Model 3 Model B and the new Raspberry Pi 5. It’s wireless, so I didn’t need an adapter. However, if you buy a model without WiFi, you will need to either wire it into your router with an ethernet cable or buy a WiFi adapter. You’ll need a power supply if you don’t have one already, as well as a usb mouse and keyboard. You’ll

You’ll also need a microSD cardto store the operating system (more on that later). You’ll need an HDMI cable for the Raspberry Pi 3, or a microHDMI to HDMI cable for the Raspberry Pi 5.

Step 2: Install a Headless Operating System

The Raspberry Pi is pretty bare-bones. You can choose your operating system from a number of interesting choices based on the task you’re trying to accomplish. First, install the official Raspberry Pi imager from the website.. Given that we’re just going to run the Pi-hole software, I went with Raspbian Lite, an open source port of the Linux operating system. The lite version comes headless (with no desktop), which is perfect since we will only connect to the Pi-hole remotely. When you launch the imager, it will have you select your hardware, the operating system you’d like, and where to install it (your SD card!).

You should also customize settings to enable SSH with a password, as we’ll use that to connect to the Raspberry Pi in the next step.

When you’re done installing the operating system, you can eject the SD card and insert into into the Raspberry Pi. You can run the Pi-hole wirelessly, but if you can connect it directly to your router via ethernet you can avoid any wireless connectivity problems.

Step 3: SSH to Raspberry Pi and Install Pi-hole

You’ll first need the IP address of the Raspberry Pi. You can find this in your network settings or by running:

arp -a
Enter fullscreen mode Exit fullscreen mode

If you haven’t already, now is a good time to allocate a static IP address to the Raspberry Pi in your routers settings.

To connect to the Raspberry Pi, run:

ssh YOUR.PI.IP.ADDRESS
Enter fullscreen mode Exit fullscreen mode

Here, substitute the IP addres you found for your Raspberry Pi. It will prompt you for the password you configured when you installed the operating system.

Either in your Pi’s terminal or your computer’s terminal connected to the Pi via SSH, run the following command from Pi-hole’s website to install it:

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

The installer will run and ask a bunch of questions. It’s okay to choose the defaults, but feel free to customize like I did.

When the installer finishes it’ll show you the password it selected. Note this and copy it, then change the web dashboard password by executing:

pihole -a -p
Enter fullscreen mode Exit fullscreen mode

Step 5: Configure Devices or Router

Now that your Pi-hole is acting as a DNS server, we need to make sure that your devices use it instead of your ISP. You can do this on a per-device basis, but I’d recommend changing it at your router. That way, every time a device on your network makes a DNS request, it will first go through the Pi-hole.

This step is very specific to the brand of router, so I won’t go into detail. Essentially you need to tell your router to treat your Pi-hole as a DNS server, rather than your ISP. You should also reserve a static IP address for your Pi to ensure it’s IP does not change because this would break your setup.

And Voila, enjoy your ad-free internet! 🙌

How Pi-hole Blocks Ads

Normal Ad-Blockers allow advertisements to load and just hide them from your view. Once you’ve gotten the Pi-hole up and running, advertisements will stop dead in their tracks, before they even begin to download.

Pi-hole is a very specific DNS sinkhole. DNS stands for Domain Name System, and it is the protocol for connecting names (like medium.com) to IP addresses, which is where websites/servers really live on the internet.

A DNS sinkhole acts in place of your usual DNS provider to provide false results. This pretty much breaks your internet — NEAT! With Pi-hole, everything that isn’t an Ad gets forwarded to your DNS provider, but if the domain name is in Pi-hole’s blacklist (which is customizable for the nerdiest among you), then the Pi-hole sends back a fake IP address.

So you visit a website and it loads. Next, the website asks its ad provider for ads, and Pi-hole says “What ad provider? That doesn’t exist.” And boom your ads are gone. This can actually speed up your network, as you won’t be using as much bandwidth.

An interesting note is that Pi-hole won’t kill youtube ads, as youtube serves its content and advertisements from the same DNS, so killing youtube ads would also kill youtube.

. . . . . . . . . . . . . . . . . . . .