Categories
Hardware Networking Software

Pi-Hole Ad-blocking DNS Server.

When you visit websites like speedtest.net, forbes.com, or any local news agency, you’ll probably notice the huge number of ads vying for your attention. What if I told you there’s a way to block most if not all of these annoying ads, so you can keep your focus? Well, you can, and I will tell you how in this post!

There are a few different ways of going about this, most people use a plugin such as uBlock origin since it’s simple and effective, however, this only works on the client that it’s installed on and doesn’t work for applications other than the web browser. This is where DNS comes into play.

To understand DNS we need to know how your computer accesses websites. All nodes on the internet access services through an IP address, so when you enter an address like www.google.co.uk, your computer needs to find the IP address associated with www.google.co.uk. This is done through a DNS (domain name system) which provides a phonebook like directory, linking domain names to IP addresses. So when your computer needs to reach www.google.co.uk a few things will happen:

  • The client will send a request to a DNS server, asking for the correct IP address.
  • The DNS server will proccess the request and send back one or more addresses that the client can use.
  • Now that the client has the right IP information, it can contact the website server directly and establish a connection.

So how does this help block ads? Well luckily for us, websites pull adverts from dedicated ad servers dynamically, allowing them to change the ads they show you on the fly. This means that we could block these ad servers, preventing the ads from being shown. The best way to do this is through a network-wide DNS sink such as Pi-hole which runs on a raspberry pi.

How to install Pi-hole

For this project we need the following items:

  1. A Raspberry Pi running raspbian with an ethernet port and sd card.
  2. A spare ethernet port on your router.

The first step is to power up the raspberry pi and either plug in a monitor, keyboard and mouse or fire up an SSH session to access the terminal.

SSH can be enabled by adding an ssh.txt file to your raspberry pi’s root directory, this will be deleted when the pi is powered on and SSH will be enabled.

We need to ensure the raspberry pi’s software is up to date, this can be done with this command.

sudo apt-get update

Before the installation of the software, we need to set a static IP address for the raspberry pi. We can do this by editing the following file:

sudo nano /etc/dhcpcd.conf

Go to the end of the document and add an IP address of your choice, and enter the IP address of your router.

# Example static IP configuration:
interface eth0
static ip_address=192.168.1.200/24

static routers=192.168.1.1
static domain_name_servers=192.168.1.1

It’s a good idea to also change the hostname of the raspberry pi to avoid confusion. This can be done by editing the raspberry pi config.

sudo raspi-config

Next, we need to start the installation of Pi-hole. For this project the installation is automated with a script.

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

The installer will ask a series of questions to configure the DNS server, if in doubt go with the recommended option.

Lastly, reboot the raspberry pi.

sudo reboot

To monitor and change the system we can access the dashboard with the raspberry pi’s IP, in our case it’s http://192.168.1.200/admin

And now that Pi-hole has been installed and is running, we can start using it as our DNS middleman. The DNS server can be configured on individual clients but if you want to use Pi-hole for all clients on your network, you can change the DNS server on your router, this proccess is different depending on your router but most will allow you to advertise a local DNS server in some way.

Using Pi-hole

Now when your device requests a domain name like www.google.co.uk, it will ask Pi-hole which will either send back the IP address if it already knows it, block the request if it’s on the block-list, or it will forward it on to your selected external DNS server.

So overall, Pi-hole helps to clean up messy web pages and can even block malicious sites and services improving the security of your network. And if you want to use Pi-hole to block other types of websites you can do so by changing the ad-lists that it uses in the dashboard.

Leave a Reply

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