Setting Up a DNS Server on Raspberry Pi with DNSmasq
Ohidur Rahman Bappy
MAR 22, 2025
Introduction
A DNS server handles translating domain names, such as pimylifeup.com
, into their respective IP addresses. By setting up a DNS server on your Raspberry Pi, you can enhance the speed of DNS requests on your network by caching them locally.
Why Use DNSmasq?
DNSmasq is a lightweight and easy-to-configure DNS server, ideal for small networks, making it a perfect fit for Raspberry Pi.
Prerequisites
Ensure your Raspberry Pi is up to date by running:
sudo apt update
sudo apt upgrade
Installing DNSmasq
Install DNSmasq on your Raspberry Pi with the following command:
sudo apt install dnsmasq
Configuring DNSmasq
Modify the DNSmasq configuration by editing its config file:
sudo nano /etc/dnsmasq.conf
Key Configuration Changes
-
Enable domain-needed to avoid forwarding plain names:
domain-needed
-
Enable bogus-priv to stop local IP leakage:
bogus-priv
-
Disable reading of the
/etc/resolv.conf
file:no-resolv
-
Set Google DNS as the upstream servers:
server=8.8.8.8 server=8.8.4.4
-
Increase cache size for improved performance:
cache-size=1000
-
Set a local domain:
domain=me.local
Save and exit the file by pressing <kbd>CTRL</kbd> + <kbd>X</kbd>, <kbd>Y</kbd>, and <kbd>ENTER</kbd>.
Adding Local Hosts
To add a local host such as kodi.me.local
, edit the hosts file:
sudo nano /etc/hosts
Add this line:
192.168.1.17 kodi
Save and exit the file.
Restarting DNSmasq
Apply changes by restarting DNSmasq:
sudo systemctl restart dnsmasq
Check the status with:
sudo systemctl status dnsmasq
Testing Your DNS Server
Install dnsutils
to test the DNS server with dig
:
sudo apt install dnsutils
Run a DNS query:
dig pimylifeup.com @localhost
Repeated queries should return faster responses due to caching.
Troubleshooting: Port 53 Used by systemd-resolved
-
Prevent
dnsmasq
from starting automatically:servicectl disable dnsmasq
-
Create and make executable
rc.local
:#!/bin/bash service systemd-resolved stop service dnsmasq start
-
Check port 53 usage:
sudo ss -lp "sport = :domain"
-
Change DNSmasq's listening port if necessary:
listen-address=127.0.0.1#5300
By following these steps, you should have a fully functioning DNS server on your Raspberry Pi, offering quicker and more efficient DNS lookups for your network.