Setting up my Raspberry Pi to run Pi-hole was fairly straightforward while following the beginner's guide linked below.
The basic steps were:
- Installing Rapsberry Pi OS, the Raspberry Pi Imager software makes this process very easy.
- I installed Rapsberry Pi OS Lite, so I used these instructions to make the terminal font larger using
sudo dpkg-reconfigure console-setup
- I installed Rapsberry Pi OS Lite, so I used these instructions to make the terminal font larger using
- Assigning a static IP address to the Raspberry Pi on my router.
- Installing Pi-hole on the Raspberry Pi, and configuring it to run as a DHCP server as well.
- On some Netgear routers, you might get the following error when trying to set the Raspberry Pi as your DNS Server:
To fix this you can simply keep your DNS settings at the default which is to let your ISP set your DNS, then reserve any static IP addresses for your Pi and disable DHCP on your router. After that you can point your DNS settings back to the Raspberry Pi. This post from Adam Ayala was helpful in solving that error.
The IP address conflicts with the WAN IP subnet. Please eneter a different IP address
- On some Netgear routers, you might get the following error when trying to set the Raspberry Pi as your DNS Server:
- Beginner's guide to building a Pi-hole
- Official Pi-hole Documentation
- Using pihole for time of day based per-client site blocking
- Change terminal font size
Using the helpful information on Thomas Mayfield's blog post, I created a custom Adlist for sites I wanted to block during certain times of the day.
Custom Adlist - https://raw.githubusercontent.com/artwilton/pi-hole-setup/main/social-media-blocklist.hosts
It's also possible to store an adlist file locally with the file:///file-location
syntax. For example: file:///home/pi/adlist.list
Shell Script to Enable and Disable custom Adlist:
#!/bin/bash
#
#block_social_media.sh
sqlite3 /etc/pi/gravity.db "update adlist set enabled = $1 where id = 2;"
pihole restartdns reload-lists
-
Troubleshooting Note: After trying a few different combinations of commands, I eventually found that
pihole restartdns reload-lists
worked best for consistently enabling and disabling the adlist, without having to restart any services like thepihole restartdns
command by itself does.pihole -g
also seemed to work well but I didn't want to have to rebuild the gravity database outside of its normal schedule, and when looking at the code for enabling and disabling adlists in the Web interface I saw that the Pi-hole devs usepihole restartdns reload-lists
as well.What I believe also helped was making sure to include
bash -lc
when runningsudo crontab -e
, which was the syntax used in Thomas Mayfield's blog post above. According to this post, this forces "cron execution to use your login environment."
Example syntax for crontab which passes a "true" or "false" parameter to the shell script. This would un-block social media websites from 8:00 AM to 11:00 AM.
# m h dom mon dow command
00 8 * * * bash -lc "/home/pi/block_social_media.sh false"
0 11 * * * bash -lc "/home/pi/block_social_media.sh true"