What Exactly Does Pi-Hole For Raspberry Pi Do And Will It Block YouTube Ads?

There's nothing more annoying than seeing ads all over the web page you're reading. Not only are they irrelevant to the content of the page, but they're also intrusive and sometimes get in the way of what you really want to see. Sure, you can stop ads on your Android, iPhone, Mac, Windows, and Linux with the help of browser settings, third-party browser extensions, and ad blocker apps. But what about the TV, fridge, and other smart home devices on your home network? And then, there's the hassle of having to install an ad blocker app or browser extension on every device you own, which can mean spending at least a couple of minutes on each machine. And if you rely solely on browser settings and browser extensions, all they really do is block ads on your browser, not in your other apps.

Advertisement

This is where Pi-hole comes to the rescue. Instead of individually configuring all your phones and computers to block ads, you can allow Pi-hole to automatically block ads for all the devices connected to your home network, from your mobile to your smart toaster. We'll walk you through how it does so and what you need to do to install it on a Raspberry Pi. 

How do ads get embedded on a website?

Before we get into what Pi-hole is and how it works, you need to understand how ads get delivered to a website in the first place. At first glance, it may seem like they're natively part of the site you're visiting. However, the reality is that they are actually just embedded. Their data doesn't usually come from the same domain (website address) as the content, images, and other media appearing on the page, meaning the ads don't belong to the original website. Instead, they're typically hosted on a different domain (or subdomain).

Advertisement

So how do they become embedded onto the website? Whenever the browser on your phone, computer, or what-have-you makes a request to access a website address, a process called DNS lookup happens behind the scenes. In this process, your browser essentially reaches out to a so-called Domain Name System (DNS) server to ask for the website's IP address. This DNS server acts like a phonebook — translating URLs into IP addresses that your browser can better understand and process. Once you receive the IP address of the URL you're after, your browser proceeds to use this to connect to the website and load its contents. Among the content that gets loaded are the ads that the website pulls from the ad company's different domain.

Advertisement

Try going to your favorite blog (one you know has ads) and hover your mouse over any ad on the page. You'll see the ad's website address momentarily appear on the bottom left corner of the screen. Notice how it's different from the URL address of the page you're currently on. Now, with this knowledge in mind, let's move on to how Pi-hole works to remove such ads.

What is Pi-hole and what can it do?

At its core, Pi-hole is an ad blocker that works by coming in between your device and the DNS server. Instead of having your browser directly ask the DNS server for the IP address of the website you want, the request first goes through the Pi-hole. It checks whether your website is on its blocklist of ad domains and known malicious and malware websites. If it is, then the request is dropped and doesn't proceed to the DNS server. You're instead provided with an essentially dead IP address that makes your device think the website is nonexistent. 

Advertisement

If, on the other hand, the website you want isn't included in the blocklist, Pi-hole connects you to the DNS server, which, in turn, sends you the website's IP address. Using this IP address, your browser is now able to pull the data you need to display the site. However, this data passes through the Pi-hole and gets analyzed again. If it contains ad domains, they're immediately filtered to leave you with only ad-free content. 

Unfortunately, this technique doesn't reliably work on YouTube since YouTube delivers the content and its ads on essentially the same domain but using different subdomains. If you block the domain, you'll be blocking the ads but also end up blocking the videos. While you can  go through the query log on Pi-hole's admin page and block the ones you think contain YouTube ads, this isn't a surefire way to remove the ads for all of the YouTube videos you'll watch. It may work one time and fail the next. The best way to remove ads on YouTube is to subscribe to Youtube Premium. If you prefer a free way of doing so, you can also uBlock Origin, which works well with FireFox on Android phones and other platforms. 

Advertisement

How to install Pi-hole on Raspberry Pi

Assuming you've already installed Raspberry Pi OS on your microSD card, here's how to get Pi-hole installed on your Raspberry Pi (or any machine with Linux or WSL): 

  1. Assign a static IP address to the Pi:
    1. With your Raspberry Pi powered on, type hostname -I (that's a capital I as in igloo) in the command line to get your current IP address. Take note of this IP address. You can use this as is or change the last number to anything up to 254.
    2. Run sudo nmcli -p connection show to get the connection name of your network. This is the value on the left of the table that appears.
    3. Run the following commands one at a time:
      1. sudo nmcli con mod "connection name" ipv4.address X.X.X.X/24 – For instance, if your connection name is Wired connection 1 and your IP address is 192.168.200.78, then your command should be sudo nmcli con mod "Wired connection 1" ipv4.address 192.168.200.78/24.
      2. sudo nmcli con mod "connection name" ipv4.gateway X.X.X.1 – Your gateway IP address is your Pi's IP address but with the last digit replaced by 1.
      3. sudo nmcli mod "connection name" ipv4.dns X.X.X.X – Use the IP address of your preferred DNS. For instance, Cloudflare is 1.1.1.1 and Google is 8.8.8.8.
    4. Reboot your Pi with sudo reboot for the changes to take effect. 
  2. Back on the Pi's command line interface, run sudo apt update && sudo apt full-upgrade to update your software. 
  3. Reboot your Pi again. 
  4. Install Curl by running sudo apt install -y curl
  5. Install Pi-hole. Type curl -sSL https://install.pi-hole.net | bash into the command line.
  6. Go through the Pi-hole installation wizard:
    1. On the Pi-hole Automated Installer and Open Source Software screens, hit OK. 
    2. Press Continue on the Static IP Needed screen. 
    3. On the next page, select eth0 if you're using an Ethernet or wlan0 if you're using Wi-Fi to connect to your home network.
    4. On the Static IP Address screen, choose Yes and press Continue. 
    5. Hit Continue on the IP Conflict message. 
    6. Select your preferred DNS as your upstream DNS provider. 
    7. Click Yes on including StevenBlack's Unified Hosts List to your blocklists. 
    8. Press Yes to installing the Admin Web Interface, lighttpd, and required PHP modules. 
    9. Enable query logging. 
    10. In the privacy mode page, pick Show everything. 
  7. Wait for the command script to finish running. 

Once you see the Installation complete page, take note of your web interface URL and password that appears. The web interface is accessible at http://pi.hole/admin. You'll be using this interface later. Then, hit OK to finish.

Advertisement

How to integrate Pi-hole into your network

With Pi-hole installed on your Raspberry Pi, you can proceed with setting up your network to use Pi-hole as your new DNS server instead of the default DNS server in your router. This means any device connected to your network would go through Pi-hole first, effectively eliminating the ads on the content you visit online. 

Advertisement

To set up Pi-hole as your new DNS server, log in to your router's admin interface and look for the DHCP or DNS settings for LAN. Then, under DNS Server, enter the Pi-hole's static IP address. This is the static IP address you assigned in Step 1. If you forgot what it is, look at the connected devices in your router's admin interface and find your Raspberry Pi from the list. It should say its IP address. Once you've entered this address as your DNS server, make sure this is the only DNS server used, or else your devices' connection may be diverted to the other servers and bypass the Pi-hole completely. 

Most routers let you add a custom DNS server, so you'll have no problem setting this up. However, some don't come with the option to do so. If this is the case for you, another way you can integrate Pi-hole into your network is by configuring each and every one of your devices to use it as their DNS server. Changing the DNS server settings on your Windows, Mac, Linux, iPhone, Android, TV, and other electronics can be time-consuming though, and the process varies per device. The general idea, though, is to go into your home network's settings/properties on your phone/computer/TV and add the Pi-hole's address as a new DNS server. Once that's done, you should be able to browse your favorite websites without getting bothered by ads.

Advertisement

Use the admin interface to manually block websites and add other blocklists

Although Pi-hole's default blocklist is already quite extensive, blocking over 100,000 domains, there may still be certain ads that sneak past Pi-hole. If you want to up the ad-blocking capability of your Pi-hole, you can manually include new blocklists and even block certain domains via the Pi-hole admin interface. Follow these steps to do so:

Advertisement
  1. Open your browser.
  2. Type the Pi-hole's admin interface URL that you noted earlier.
  3. Enter the login password to proceed.
  4. Press Log in.
  5. To add a new blocklist:
    1. Select Group Management from the left side panel.
    2. Click on Adlists.
    3. In the Address field at the top, enter your blocklist. Most Pi-hole users refer to The Firebog for blocklists to add.
    4. Check the List of configured adlists (still in the Adlist group management page) to see whether you already have your chosen blocklist from The Firebog.
    5. If your chosen blocklist isn't already configured in your Pi-hole, press Add. Otherwise, pick a different blocklist.
    6. After seeing the new blocklist under the List of configured adlists, open the Terminal app.
    7. Run the command pihole -g to update your configured adlists and include the newly added one.
  6. To manually block domains:
    1. Go to Query Log on the side panel. This will show you all the domains Pi-hole blocked and allowed.
    2. Investigate which domain is delivering unwanted ads. There are several ways to go about this:
    3. Check the timestamp for when you visited the website that displayed ads.
    4. Use adam:ONE assist Chrome extension and load the page again. The extension will show you all the domains used to load that page.
    5. Once you have a hint of the domain causing issues, return to the Query Log.
    6. Find the problematic domain.
    7. Press Blacklist to the right of the domain to block it.
    8. If you accidentally blacklisted a domain, simply click on the Whitelist button next to it in the Query Log to unblock it.

The downsides of Pi-hole

While getting rid of ads on all your devices is a dream, there is a price to pay for such convenience. Two of the known downsides of Pi-hole are connectivity and privacy issues. You may experience connectivity issues with Pi-hole when the Raspberry Pi or Linux machine it's installed on is powered off. Because you're using the Pi-hole as your DNS server, any DNS requests made by the devices on your network now have nowhere to go. Sure, you might still be able to load websites you've previously visited as they have already been cached to your browser/device. However, for brand new websites that still need their IP addresses resolved, they would need the Pi-hole to load on your end. As a workaround, you can go into your router settings and add a different DNS server for your devices to use. But note that this will mean ads, ads, and more ads appearing on your websites.

Advertisement

The second downside of Pi-hole is the lack of privacy for the other network users. There's a Network section in the Pi-hole admin interface where you can see the IP addresses of all the devices using Pi-hole. When you click on one, you will see its query log that lists the domains the device requests. It's best to let your household members know about this privacy issue, so they can opt out of using Pi-hole if they're concerned about you snooping on their online activities.

Recommended

Advertisement