Setting up a Raspberry Pi as a DNS server is an excellent way to improve your network’s performance and gain more control over your DNS queries. This comprehensive guide will walk you through the process of configuring your Raspberry Pi as a DNS server using Dnsmasq, a lightweight DNS forwarder and DHCP server ideal for small networks.
Prerequisites
Before you begin, ensure you have:
- A Raspberry Pi (Model 2, 3, or 4) running the latest version of Raspberry Pi OS (Debian 12 Bookworm as of February 2025)
- A stable network connection (Ethernet or Wi-Fi)
- A static IP address assigned to your Raspberry Pi
- Access to your Raspberry Pi via SSH or direct connection
Step 1: Update Your Raspberry Pi
First, update your Raspberry Pi’s software to ensure you have the latest security patches and updates:
sudo apt update
sudo apt upgrade -y
Step 2: Install Dnsmasq
Dnsmasq is a lightweight DNS server perfect for small networks. Install it using:
sudo apt install dnsmasq -y
Step 3: Configure Dnsmasq
Now, let’s configure Dnsmasq:
I. Backup the original configuration file:
sudo cp /etc/dnsmasq.conf /etc/dnsmasq.conf.backup
II. Open the configuration file:
sudo nano /etc/dnsmasq.conf
III. Modify the following settings:
- Uncomment and enable domain-needed and bogus-priv
- Uncomment dnssec for added security
- Uncomment no-resolv to prevent reading /etc/resolv.conf
- Set upstream DNS servers (e.g., Cloudflare’s 1.1.1.1):
server=1.1.1.1
server=1.0.0.1
Increase the cache size:
cache-size=2000
Add a local domain (optional):
domain=me.local
expand-hosts
IV. Save and exit the file (Ctrl+X, then Y, then Enter).
Step 4: Configure Network Manager for Static IP
With Raspberry Pi OS Bookworm, networking is managed by NetworkManager. To set a static IP:
I. List network interfaces:
nmcli connection show
II. Set a static IP (replace “CONNECTION_NAME” with your actual connection name):
sudo nmcli connection modify "CONNECTION_NAME" ipv4.addresses 192.168.1.100/24 ipv4.method manual
sudo nmcli connection modify "CONNECTION_NAME" ipv4.gateway 192.168.1.1
sudo nmcli connection modify "CONNECTION_NAME" ipv4.dns "192.168.1.1,8.8.8.8"
III. Apply changes:
sudo nmcli connection down "CONNECTION_NAME" && sudo nmcli connection up "CONNECTION_NAME"
Step 5: Restart and Enable Dnsmasq
Apply the changes by restarting the Dnsmasq service and enabling it to start on boot:
sudo systemctl restart dnsmasq
sudo systemctl enable dnsmasq
Step 6: Testing Your DNS Server
To verify that your Raspberry Pi is correctly resolving DNS queries:
I. Install DNS utilities:
sudo apt install dnsutils -y
II. Use the dig command to test a DNS lookup:
dig example.com @localhost
This should return the IP address for example.com, queried through your Raspberry Pi DNS server.
Step 7: Configure Client Devices
To use your Raspberry Pi as the DNS server:
I. Open your router’s configuration page
II. Find the DNS settings
III. Enter your Raspberry Pi’s static IP address as the primary DNS server
Alternatively, configure individual devices to use the Raspberry Pi’s IP as their DNS server.
#8 Optional: Adding Custom DNS Entries
You can add custom DNS entries by editing the /etc/hosts file on your Raspberry Pi:
sudo nano /etc/hosts
Add entries in the format:
192.168.1.100 myserver.local
Restart Dnsmasq after making changes:
sudo systemctl restart dnsmasq
By following these steps, you’ve successfully set up your any new or old Raspberry Pi PCs as a DNS server. This setup will cache DNS queries, potentially speeding up your browsing experience and giving you more control over your network’s DNS resolution. Remember to monitor logs in /var/log/syslog for any issues and consider implementing additional security measures like firewall rules to restrict access to your DNS server.












