Saturday, September 21, 2024
Google search engine
HomeGuest BlogsHow To Set Up Raspberry Pi As A DNS Server

How To Set Up Raspberry Pi As A DNS Server

Introduction

A Domain Name System (DNS) translates human-readable domain names to IP addresses. DNS serves as a phone book of internet addresses for quicker access to pages. Setting up a Raspberry Pi as a DNS server improves DNS lookup time and connection speed.

This guide explains how to configure the Raspberry Pi as a DNS server.

How To Set Up Raspberry Pi As A DNS ServerHow To Set Up Raspberry Pi As A DNS Server

Prerequisites

  • Raspberry Pi 2, 3, or 4 with Raspbian OS using a static IP address
  • Ethernet cable connection or Wi-Fi dongle
  • Power supply
  • MicroSD card
  • Terminal access (directly or through SSH) with sudo privileges

Raspberry Pi DNS Server Setup Guide

There are several methods to set up a Raspberry Pi as a DNS server. However, the most straightforward way is using the lightweight DNSMasq utility.

Step 1: Update Packages

Before starting, open the terminal and update software packages on your Raspberry Pi using the apt package manager:

sudo apt update
sudo apt upgrade

Wait for the update and upgrade to complete before proceeding to the next step.

Step 2: Install DNS Software

Install DNSMasq on the Raspberry Pi:

sudo apt install dnsmasq
Installation of dnsmasq DNS Software through the terminalInstallation of dnsmasq DNS Software through the terminal

DNSMasq is an excellent choice for small-scale networks.

Step 3: Configure DNSMasq

Configuring DNSMasq ensures the best setup for the DNS server.

1. Modify the dnsmasq.conf file using the nano text editor by running:

sudo nano /etc/dnsmasq.conf

2. Locate (CTRL+W to search) and uncomment the following lines by removing the hash sign (#):

  • domain-neededConfigures the DNS server to not forward names without a dot (.) or a domain name to upstream servers. Any names without a dot or domain stay in the local network.
  • bogus-priv – Stop DNS server from forwarding local IP range reverse-lookup queries to upstream DNS servers. That prevents leaking of the local network to upstream servers.
  • no-resolv – Stops reading the upstream nameservers from the /etc/resolv.conf file, relying instead on the ones in the DNSMasq configuration.

3. Find (CTRL+W) and remove the following line:

#sever=/localnet/192.168.0.1

Replace with:

server=8.8.8.8
server=8.8.4.4

This step makes use of Google’s DNS servers for the upstream nameservers.

4. Find (CTRL+W) the following line:

#cache-size=150

Uncomment and change the cache size to 1000:

cache-size=1000

Increasing cache size saves a higher number of DNS requests to the DNSMasq cache. The network performance improves because the DNS lookup time reduces.

5. Save the file with CTRL+X, then press Y and hit Enter to keep the changes.

6. Restart DNSMasq to apply the changes:

sudo systemctl restart dnsmasq

7. Check the status of the DNS server with:

sudo systemctl status dnsmasq

The status shows as active (running), indicating the Raspberry Pi is running as a DNS server:

Terminal output of dnsmasq status as active (running)Terminal output of dnsmasq status as active (running)

Step 4: Test the DNS Server

1. Test the newly set up DNS server with the dig command. Check the DNS data by running:

dig <domain> @localhost

For example:

dig geeksforgeeks.org/kb @localhost

2. Check the query time in the response:

Output of the dig command first time useOutput of the dig command first time use

Note: If the system cannot find the command, run sudo apt install dnsutils.

3. Rerun the command. Notice the query time reduces because the address is cached:

Output of the dig command second time useOutput of the dig command second time use

Step 5: Configure Your Device to Use the Raspberry Pi as a DNS Server

The final step is to configure other devices to use the Raspberry Pi as a DNS server.

1. Find the IP address on the Raspberry Pi by running ifconfig from the terminal. Save the eth0 inet address if using an ethernet connection or the wlan0 inet address if using Wi-Fi:

Terminal output of IP address from the ifconfig commandTerminal output of IP address from the ifconfig command

2. Set the DNS server address as the Raspberry Pi address on the device you want to configure. The DNS server address is in the network settings and differs depending on the OS in use.

Every device must point to the Raspberry Pi address for the DNS server utilization.

Note: Learn about various DNS record types by referring to our post DNS Record Types Explained.

Conclusion

Using a Raspberry Pi as a DNS server improves network speed and connectivity by caching addresses. To learn more about DNS, check out our article on DNS best practices for security and performance.

Was this article helpful?
YesNo

RELATED ARTICLES

Most Popular

Recent Comments