Pi-hole
Pi-hole
Guide for setting up Pi-hole
What you will need
This project assumes that you have already followed our Raspberry Pi installation project, and have the resulting configured Raspberry Pi. If you are installing on different hardware or a different OS, these steps may not all be the same for you.
System Preparation
Before continuing with the project, make sure your system is up to date.
sudo apt updatesudo apt upgrade -yWhat is Pi-hole?
Pi-hole is a DNS sinkhole/network-level blocker. It can block requests to known advertising/tracking domains before they resolve.
This guide installs Pi-hole and configures the Pi itself to use Pi-hole for DNS.
Initial Setup
Installing Pi-Hole
The Pi-hole project has a one-line command that can be used to install a Pi-hole on a machine.
curl -sSL https://install.pi-hole.net | bashConfiguration
The install script that runs will ask if you want custom configurations. We are going to stick to the defaults here.
For this temporary demo, we won’t configure a static IP, so you can disregard the warning it gives you.. For a permanent Pi-hole installation, you should give the Pi a consistent IP address, either by configuring a static address or creating a DHCP reservation on your router.
Starting Commands
Before we do anything, we should verify the installation by checking Pi-hole’s status.
sudo pihole statusIf your installation isn’t working as expected you can rerun the install script or run:
sudo pihole repairPasswords
Now that we know Pi-hole is working properly, let’s change the password it gave us.
sudo pihole setpasswordor
sudo pihole setpassword <your_password>Our Pi-hole should now be set up, but it’s not currently in use. Let’s fix that.
Using Pi-hole
Listing Connections
NetworkManager stores network configuration in connection profiles. We need the name of the active connection so we can modify its DNS settings. Look for the active Ethernet/Wi-Fi connection (depending on what you are using) and use its name in the following commands.
nmcli conn showSetting DNS
ipv4.ignore-auto-dns yes tells NetworkManager to ignore the DNS settings provided by DHCP and use the manually specified DNS servers instead.
ipv4.dns "127.0.0.1" tells NetworkManager to use the specified DNS server (in this case, the local Pi-hole / localhost) for DNS resolution.
nmcli conn mod "<connection_name>" ipv4.ignore-auto-dns yesnmcli conn mod "<connection_name>" ipv4.dns "127.0.0.1"Applying Changes
nmcli conn down "<connection_name>"nmcli conn up "<connection_name>"Testing
Now, let’s test if our Pi-hole is working. In a terminal:
sudo pihole tailThen, open up a web browser and visit a website. You should see queries being logged in the terminal where sudo pihole tail is running, indicating that Pi-hole is actively filtering DNS requests.
Pi-hole blocks domains at the DNS level. It does not inspect or modify webpage content, so it cannot block every type of advertisement. Additionally, you may want to further customize Pi-hole by adding more block lists or using regex patterns to block specific types of domains.
Further Customizations
Manually Blocking Domains1/2
Pi-hole is nice, but the default block list it comes with doesn’t block everything.
So let’s tell Pi-hole to block more domains.
sudo pihole deny <domain_1> <domain_2> <domain_3> ...For testing purposes, you can try blocking a domain and then attempt to access it in a web browser. You should see that the site is blocked by Pi-hole.
Manually Blocking Domains2/2
You can also see the queries being blocked by Pi-hole in real-time by running the following command again:
sudo pihole tailReverting Changes
If you ever block a domain you didn’t mean to, you can always remove it from Pi-hole’s deny list.
sudo pihole deny remove <domain_1> ...Let’s allow the domain that you previously blocked in Pi-hole again.
Regex Blocking
You can also use a regular expression (regex) to block any domain that matches:
sudo pihole --regex <your_regex>For example:
sudo pihole --regex ".*\.doubleclick\.net"Testing (Again)
Try visiting https://fuzzthepiguy.tech/adtest/ after blocking “doubleclick.net”. You should not see any ads.
Accessing the Web Interface
Pi-hole’s web interface provides some extended capabilities, such as viewing query logs, managing block lists, and configuring settings.
You can access it at http://pi.hole:80/admin, or directly at http://<your_pi-hole_ip>:80/admin.
Block Lists
One of the things you can do from the web interface is add block lists.
In the sidebar, navigate to Group Management and select Lists.
Now try adding the list https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts. You should now be able to block a lot more ads.
Up next
Docker & Portainer