User Agents

Web browsers and everyday web tech, explained

Raspberry Pi DNS Server: Setup Guide

· 8 min read · How the Web Works

In this article
  1. What a DNS Server Does
  2. Why Run a DNS Server on a Raspberry Pi?
  3. Hardware and Software Requirements
  4. Setting Up Pi-hole (Step-by-Step)
  5. Adding Unbound for Recursive DNS
  6. Common Problems and How to Fix Them
  7. Alternatives to Pi-hole
  8. Costs and Power Consumption
  9. Security and Maintenance
  10. Frequently Asked Questions

Yes, a Raspberry Pi can run a DNS server, and it is one of the most popular and practical projects for the device. You can turn a $35 Raspberry Pi into a fast, private, and ad-blocking DNS resolver for your entire home network using software like Pi-hole or dnsmasq. This guide covers how it works, what you need, how to set it up, and common pitfalls.

What a DNS Server Does

The Domain Name System (DNS) is the phonebook of the internet. When you type example.com into your browser, your device asks a DNS server to translate that human-friendly name into an IP address like 93.184.216.34. Your router usually provides this service automatically, forwarding queries to your Internet Service Provider's DNS servers. Running your own DNS server on a Raspberry Pi replaces that default with a system you control.

There are two main roles a Pi can fill:

  • Recursive resolver – it queries root servers, TLD servers, and authoritative servers itself to find answers. Software like Unbound does this.
  • Forwarding proxy – it forwards queries to an upstream DNS (like Cloudflare or Google) but caches results and can filter them. Pi-hole and dnsmasq work this way.

Most home users run a combination: Pi-hole for filtering and caching, with Unbound as the recursive backend to avoid relying on third-party resolvers.

Why Run a DNS Server on a Raspberry Pi?

The main reasons people build this are privacy, speed, and ad blocking.

  • Ad blocking – Pi-hole maintains a blocklist of known advertising and tracking domains. When a device requests ads.example.com, the Pi responds with a null IP (0.0.0.0), so the ad never loads. This works network-wide, including on smart TVs and phones where you cannot install an ad blocker.
  • Local caching – A Pi with a decent SD card can cache DNS responses. Repeated requests for the same domain are answered instantly, which can feel snappier than querying a remote server each time.
  • Privacy – By running your own recursive resolver, you stop your ISP from seeing every domain you visit. Your queries go directly to authoritative servers, not through a third-party logging service.
  • Learning – It is a great introduction to Linux system administration, networking, and DNS concepts. The project is well-documented and forgiving.

Hardware and Software Requirements

You do not need a powerful Pi. Any model from the Raspberry Pi 2 onward works fine for a home network. The Pi 4 or Pi 5 with 2GB RAM is overkill but gives you headroom. What you need:

  • A Raspberry Pi board
  • A microSD card (8GB minimum, Class 10 recommended)
  • A power supply (official one for your model)
  • An Ethernet cable or a stable Wi-Fi connection
  • Raspberry Pi OS Lite (64-bit) – the headless version without a desktop

You will also need to know your router's IP address (often 192.168.1.1) and the IP you want to assign to the Pi. It is best to set a static IP on the Pi itself or reserve one in your router's DHCP settings.

Setting Up Pi-hole (Step-by-Step)

Pi-hole is the most common choice because it is a single, well-maintained package that combines a DNS forwarder, a DHCP server (optional), and a web dashboard. Here is the standard installation process.

  1. Install Raspberry Pi OS Lite on the SD card using the Raspberry Pi Imager. Enable SSH during the imaging process by clicking the gear icon and setting a username and password.
  2. Boot the Pi, find its IP address (check your router's client list or use nmap), and SSH into it.
  3. Update the system: sudo apt update && sudo apt upgrade -y
  4. Run the Pi-hole installer: curl -sSL https://install.pi-hole.net | bash
  5. Follow the prompts. Choose your upstream DNS provider (Cloudflare, Google, or a custom one). If you plan to use Unbound later, select a placeholder like Cloudflare.
  6. When asked, enable the web interface and the admin password. Note the IPv4 address it tells you to use.
  7. After installation, set a static IP using sudo raspi-config or by editing /etc/dhcpcd.conf. Add lines like:
    interface eth0
    static ip_address=192.168.1.50/24
    static routers=192.168.1.1
    static domain_name_servers=127.0.0.1
  8. Reboot: sudo reboot

Once rebooted, visit http://192.168.1.50/admin (using your Pi's IP) to see the dashboard. You can now point your router's DNS settings to this IP, or set it manually on each device.

Adding Unbound for Recursive DNS

Pi-hole by default forwards queries to a public DNS server. If you want to eliminate that third party, install Unbound on the same Pi. Unbound acts as a recursive resolver, meaning it asks the root servers directly. This is more private but slightly slower on the first query for a new domain.

  1. Install Unbound: sudo apt install unbound -y
  2. Create a configuration file at /etc/unbound/unbound.conf.d/pi-hole.conf with the following content (adjust the interface to your Pi's IP):
    server:
        verbosity: 1
        interface: 127.0.0.1
        port: 5335
        do-ip4: yes
        do-ip6: no
        do-udp: yes
        do-tcp: yes
        access-control: 127.0.0.1 allow
        access-control: 192.168.1.0/24 allow
        hide-identity: yes
        hide-version: yes
  3. Restart Unbound: sudo systemctl restart unbound
  4. In the Pi-hole web interface, go to Settings → DNS and set the custom upstream to 127.0.0.1#5335.
  5. Test with dig @127.0.0.1 -p 5335 example.com. You should get an answer.

Now your Pi is fully recursive. It no longer sends your DNS queries to any third party.

Common Problems and How to Fix Them

Even with a smooth setup, you may hit a few issues. Here are the most frequent ones and the usual fixes.

  • No internet after changing router DNS – If you set the router's DNS to the Pi and the Pi goes down, all devices lose name resolution. Fix: give the Pi a static IP and make sure it is always on. You can also set a secondary DNS on the router, but that will bypass Pi-hole for some queries.
  • Pi-hole shows 0 queries – This usually means devices are not using the Pi as their DNS. Check your router's DHCP settings: it must hand out the Pi's IP as the DNS server, not the router's own IP.
  • Slow first queries – If you use Unbound, the first lookup for a domain can take a second because it has to walk the root servers. This is normal. Subsequent lookups are fast due to caching.
  • Blocked legitimate sites – Some sites use ad-serving domains for actual content. You can whitelist a domain in Pi-hole's dashboard under “Query Log” or “Domains”.
  • SD card corruption – Power cuts can corrupt the SD card. Use a high-quality card, and consider enabling read-only mode or moving logs to RAM to reduce writes.

Alternatives to Pi-hole

Pi-hole is not the only option. Depending on your needs, you might consider:

  • dnsmasq – A lightweight forwarder and DHCP server. It is already built into Raspberry Pi OS. It is fast and simple, but has no web interface or ad-blocking lists by default.
  • AdGuard Home – A Pi-hole alternative with a modern web UI, parental controls, and built-in DNS-over-HTTPS support. It is available as a single binary and is easy to install.
  • Unbound alone – If you only want a recursive resolver without filtering, Unbound is sufficient. It is secure and widely used.
  • Cloudflare or NextDNS on your router – Some routers let you set DNS-over-HTTPS directly, giving you filtering without any extra hardware. However, you lose local caching and control.

Costs and Power Consumption

The cost of running a Pi DNS server is minimal. The Pi itself costs between $35 and $75 depending on the model. An SD card is about $10. The power draw of a Pi 4 under light load is around 3–4 watts. At an average electricity price of $0.15 per kWh, that is roughly $4 per year. In contrast, a dedicated DNS appliance can cost hundreds of dollars and use far more power.

If you already own a Pi, the only cost is your time. The software is free and open-source.

Security and Maintenance

Running your own DNS server has security implications. You are now responsible for keeping the system patched. Set up automatic updates:

sudo apt install unattended-upgrades -y
sudo dpkg-reconfigure --priority=low unattended-upgrades

Also, do not expose the DNS server to the internet. If you need remote access, use a VPN. By default, Pi-hole listens on all interfaces, so make sure your firewall blocks port 53 from outside. You can restrict access in the Pi-hole settings by entering your local subnet in the “Interface Settings” section.

Frequently Asked Questions

Will a Raspberry Pi DNS server slow down my internet? No. In fact, it can speed up repeated lookups due to caching. The Pi has more than enough CPU power to handle hundreds of queries per second, far more than a typical home network generates.

Can I use a Raspberry Pi Zero for this? Yes, but with caveats. The Pi Zero's single-core CPU and slower SD card can handle a small network, but the web interface may feel sluggish. For a reliable setup, a Pi 2 or newer is recommended.

Do I need to change settings on every device? No. If you set your router's DHCP to hand out the Pi's IP as the DNS server, every device on the network will use it automatically. You only need to change per-device settings if your router does not allow custom DNS.

What is the difference between Pi-hole and a VPN? A VPN encrypts all your traffic and routes it through a remote server. Pi-hole only handles DNS queries, so it does not hide your IP address or encrypt your browsing. They serve different purposes and can be used together.

Can I run Pi-hole in Docker on a Raspberry Pi? Yes, Pi-hole has an official Docker image. This is a good option if you want to keep the host system clean or run multiple services. The setup is slightly more complex but well-documented.

Will blocking ads break websites? Occasionally, yes. Some sites rely on the same domain for ads and content. If a page looks broken, check Pi-hole's query log and whitelist the domain. This is rare and usually easy to fix.