J. R. Swab

Two Ways To Block Ads On Your Entire Network

Categories: [Technology]
Tags: [open-source], [linux], [software], [privacy], [dns]

Many people use an ad blocker like uBlockOrigin to block ads from tracking us around the web but this only works on the devices it is installed on. We can do one better and prevent them all across any network in our control.

Method 1: Hosts File

This method has the least overhead of the two and is great if you just want to get something up and running. Ideally, you would want a separate device to, but you can do this with any computer.

Every computer has a hosts file that holds data for converting domain names to their IP addresses. When you open one for the first time, it is pretty bare, and all requests get directed to your router for lookup.

  1. Log into the computer or device you want to use as your new DNS server. I am using a Raspberry Pi 2.
  2. Open Steven Black's Hosts Git Repo, scroll down, and find the file that works best for you. I am using the Unified Hosts + Porn. Click the link to the "Raw Hosts File," and you will see an enormous list of domains.
  3. Open your hosts file. (Linux & Mac OS X: /etc/hosts, Windows \system32\drivers\etc\hosts)
  4. Copy and paste the raw text from step two into your hosts file. You can overwrite the file or create a symlink if you want. The file hosted on Github has all the bits required it the hosts file.
  5. Log into your home router.
  6. Find the IP address of the device you just added the hosts file too
  7. Copy this address and set is as your new DNS server for your router. (I can't walk you through this because they are all different.)

That's it! Now you have a network level ad blocker!

If you want to have this file auto update every day to have the freshest hosts, just clone the repo, create a symbolic link, and then set up a Cron job to git pull that repo once a day.

Method 2: Pi-Hole

If you want a fancy interface to log into and see all the stats; check out Pi-Hole. I set this up as my project to test out Balena.io. They have a guide on how to set up a Pi-Hole with their site and your device.

One of the steps there is to set service variables, but this can not be done until Pi-Hole is installed on the device via the Balena-CLI.

Because of this, you won't have the password to log into the Pi-Hole admin page. To update this password navigate to your device on the Balena dashboard and run the terminal for the Pi-Hole service. Once in run pihole -a -p pass where pass is the password you want to use.

Once you have Pi-Hole running and can log in and add the Steven Black's Hosts Git Repo of your choice to the list of domains. Copy the URL to the raw file, then click settings in the Pi-Hole admin dashboard. Click the blocklists tab, scroll down to the bottom, and paste the URL to the list you chose.

If you don't want to block anything other than ads and malicious sites than there is no need to add anything to the Pi-Hole.

After the Pi-Hole is set up all you need to do change your router's DNS server to that of the Pi-Hole, and you are all set!


Ways to support the blog.