ThisCalculator

Everyday

Block Ads at DNS Level with Pi-hole and OpenVPN

Updated · Standard formula, shown below

This guide shows you how to set up Pi-hole as a network-wide ad blocker and securely connect to it from anywhere using OpenVPN. You’ll enter your router’s DNS settings, install Pi-hole on a Linux server, configure OpenVPN, and then route all your devices’ DNS queries through the Pi-hole—even when you’re away from home. By the end, ads, trackers, and malicious domains will be blocked on every device that uses your VPN.

What You’ll Need

  • A dedicated Linux server (Ubuntu 20.04 or 22.04 recommended) with a static IP address, either on your local network or a cloud VPS.
  • Root or sudo access to that server.
  • A domain name or a dynamic DNS service if your home IP changes.
  • OpenVPN installed on the server (we’ll cover the setup).
  • OpenVPN client software on your devices (Windows, macOS, Android, iOS, Linux).
  • Basic comfort with the command line.

Step-by-Step

  1. Update your server: Log in via SSH and run sudo apt update && sudo apt upgrade -y to ensure all packages are current.
  2. Install Pi-hole: Run the official installer with curl -sSL https://install.pi-hole.net | bash. Follow the prompts: choose your upstream DNS provider (e.g., Cloudflare or Google), set a static IP if prompted, and note the web interface password it generates.
  3. Set Pi-hole’s IP as your router’s DNS: In your router’s admin panel, find the DHCP or LAN settings, and set the DNS server to your Pi-hole server’s IP address. This makes every device on your network use Pi-hole automatically.
  4. Install OpenVPN: The easiest way is to use the PiVPN script, which integrates with Pi-hole. Run curl -sSL https://install.pivpn.io | bash. Follow the prompts: choose OpenVPN as the protocol, select your Pi-hole IP as the DNS server, and set a static IP for the VPN interface if asked.
  5. Generate client profiles: After PiVPN finishes, run pivpn add to create a profile for each device. Give it a name (e.g., phone). The script will create a .ovpn file in ~/ovpns/.
  6. Copy profiles to your devices: Transfer the .ovpn files to your devices (via USB, email, or a secure file transfer). On Android/iOS, use the OpenVPN Connect app and import the file. On Windows/macOS, install the OpenVPN client and import the profile.
  7. Connect and test: Connect to your VPN from a device. Then visit a site like pi-hole.net or check your Pi-hole dashboard (http://pi.hole/admin) to see the query log. You should see queries coming from the VPN interface.
  8. Verify ad blocking: Visit a site known for ads (like a news site) and confirm ads are missing. Also check the Pi-hole dashboard’s “Query Log” to see blocked domains.

Common Mistakes

  • Forgetting to set the DNS in the OpenVPN config: If you don’t specify Pi-hole as the DNS server in the VPN config, clients will use their own DNS, and ad blocking won’t work while on VPN.
  • Not using a static IP for the server: If your server’s IP changes, Pi-hole and OpenVPN will break. Set a static IP in your router or use DHCP reservation.
  • Blocking all traffic on port 53: Some routers or ISPs block DNS queries. Ensure your VPN uses port 1194 (UDP) and that DNS queries are allowed.
  • Ignoring firewall rules: If you have a firewall, you must allow incoming connections on port 1194 (UDP) and possibly port 80/443 for the admin interface.

Tips

  • Use a dynamic DNS service if your home IP is not static. PiVPN will ask for a hostname; use your DDNS address.
  • Update your blocklists: Pi-hole comes with default lists, but you can add more from sources like Firebog (search for “firebog” to find the list). Add them in the dashboard under “Group Management” > “Adlists”.
  • Set a strong password for the Pi-hole admin: Use pihole -a -p to change it.
  • Use the VPN only when needed: If you’re on your home Wi-Fi, you don’t need the VPN; your router already uses Pi-hole.

Troubleshooting

  • VPN connects but no ad blocking: Check that the client’s DNS is set to the Pi-hole IP. On Windows, run ipconfig /all and look at the DNS servers. If it shows a different IP, edit the .ovpn file and add dhcp-option DNS 10.8.0.1 (or your Pi-hole IP) and reconnect.
  • Pi-hole dashboard not loading: Ensure the web server is running: sudo systemctl status pihole-FTL. If it’s not, restart it with sudo systemctl restart pihole-FTL.
  • VPN won’t connect: Check the server firewall: sudo ufw status. If enabled, allow OpenVPN: sudo ufw allow 1194/udp. Also verify the server’s public IP is reachable from the internet.
  • Some ads still appear: Some ads are served from the same domain as content (e.g., YouTube). Pi-hole can’t block those without breaking the site. You can add custom blacklists for specific domains.
  • Slow DNS resolution: If Pi-hole is slow, try a different upstream DNS provider. You can change it in the dashboard under “Settings” > “DNS”.

Frequently Asked Questions

Do I need a separate server for this?

Yes, ideally. You can run Pi-hole on a Raspberry Pi or an old PC, but it must be always on. A cloud VPS is also an option, but then you’d need to route all your traffic through it, which can be slower.

Will this block ads on my phone when I’m on cellular data?

Only if you connect to your VPN while on cellular. If you don’t connect, your phone uses your carrier’s DNS, so ads will appear. You can set up the VPN to auto-connect on your phone for continuous blocking.

Can I use Pi-hole with OpenVPN on the same machine?

Yes, that’s exactly what this guide does. PiVPN makes it easy to install OpenVPN alongside Pi-hole.

Does this work with Windows, macOS, Android, and iOS?

Yes, OpenVPN has clients for all major platforms. The .ovpn profile works on all of them.

Will this slow down my internet?

Minimally. DNS queries are tiny, and Pi-hole caches responses. The VPN adds a small overhead, but for most activities you won’t notice a difference.

Can I unblock a domain that’s accidentally blocked?

Yes, go to the Pi-hole dashboard, click “Query Log”, find the blocked domain, and click the “Whitelist” button. You can also manage whitelists under “Group Management”.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.