How to setup DNS Server in Raspberry Pi| LinuxGist
This article will provide details on how DNS Server can be setup in Raspberry Pi and other Linux systems.
Introduction
A DNS (Domain Name System) server translates human-readable domain names into IP addresses that computers use to communicate over a network.
Components of a DNS Server
- Authoritative DNS Servers: These are servers that store the most up-to-date and accurate information about domain name mappings. They respond directly to queries for specific domains.
- Recursive DNS Servers: These forward requests from client machines to authoritative servers to resolve domain names, then return the results to the client.
Setting Up Your Own DNS Server on a Raspberry Pi
Setting up your own DNS server can be useful for various reasons, such as privacy (using a local DNS resolver), caching frequently accessed domains, or customizing name resolution. Below are the steps to set up a simple recursive DNS server using dnsmasq
, which is a lightweight and powerful DNS forwarder.
Prerequisites
- A Raspberry Pi running a Debian-based operating system (e.g., Raspbian).
- An internet connection.
Step-by-Step Guide
Update Your System
1
sudo apt update && sudo apt upgrade -y
Install
dnsmasq
1
sudo apt install dnsmasq
Configure
dnsmasq
Edit the configuration file
/etc/dnsmasq.conf
using a text editor likenano
.1
sudo nano /etc/dnsmasq.conf
Add or modify the following lines to configure your DNS server:
1 2 3 4 5 6 7 8 9 10 11 12
# Set this to the IP address of your Raspberry Pi (or any other preferred upstream DNS server) server=8.8.8.8 # Optionally, you can add custom domain mappings here local=/home.mydomain/ address=/home.mydomain/192.168.1.100 # Enable recursion to allow the Raspberry Pi to forward queries it doesn't know how to answer recursive=yes # Optionally, enable logging for debugging purposes (uncomment if needed) log-queries
Configure Network Interface
Edit the network interface configuration file for your Ethernet or Wi-Fi interface. For example, edit
/etc/network/interfaces
:1
sudo nano /etc/network/interfaces
Add or modify the following lines to set your Raspberry Pi as a DNS server for your local network:
1 2 3 4 5
auto eth0 iface eth0 inet static address 192.168.1.1 netmask 255.255.255.0 dns-nameservers 127.0.0.1
This configuration sets your Raspberry Pi’s IP address to
192.168.1.1
and configures it as the DNS server for your local network.Restart
dnsmasq
Apply the changes by restarting the
dnsmasq
service:1
sudo systemctl restart dnsmasq
Verify Configuration
Ensure that
dnsmasq
is running correctly and listening on the correct port (53):1
sudo netstat -tuln | grep 53
You should see an entry for
dnsmasq
listening on port 53.Configure DHCP (Optional)
If you want your Raspberry Pi to act as a DHCP server along with the DNS server, install and configure
isc-dhcp-server
.1
sudo apt install isc-dhcp-server
Edit the configuration file
/etc/dhcp/dhcpd.conf
:1
sudo nano /etc/dhcp/dhcpd.conf
Add or modify the following lines to specify your network range and provide the IP address of your Raspberry Pi as the DNS server:
1 2 3 4 5
subnet 192.168.1.0 netmask 255.255.255.0 { range 192.168.1.10 192.168.1.100; option routers 192.168.1.1; option domain-name-servers 192.168.1.1; }
Edit the
dhcpcd
configuration file to use the DHCP server:1
sudo nano /etc/dhcpcd.conf
Add or modify the following line:
1
static domain_name_servers=192.168.1.1
Restart
isc-dhcp-server
anddhcpcd
to apply changes:1 2
sudo systemctl restart isc-dhcp-server sudo service dhcpcd restart
Testing Your DNS Server
You can test your DNS server by querying it using the nslookup
or dig
command from another device on your network.
1
nslookup example.com 192.168.1.1
Or:
1
dig @192.168.1.1 example.com
Conclusion
You have successfully set up a simple recursive DNS server on your Raspberry Pi. This setup can be useful for caching domain queries, providing custom name resolution, or enhancing the privacy of devices in your local network by using a local resolver instead of querying public DNS servers directly.