‍

In today’s digital world, establishing a local DNS Server for your environment is essential. It optimizes network performance, reduces latency, and provides a layer of security. One of the most popular tools for this purpose is Dnsmasq. This guide will walk you through the process of installing and configuring Dnsmasq on Rocky Linux.

The Power of Dnsmasq

Dnsmasq is a lightweight, easy-to-use DNS server. Its versatility allows it to function as a DNS Server, DHCP Server, and TFTP Server. As a DNS Server, Dnsmasq can act as a forwarder, recursive DNS Server, and DNS caching system. It can also read DNS contents from the /etc/hosts file, enabling you to set up domain names for local hostnames.

Why Choose Dnsmasq

Designed with a small footprint, Dnsmasq is perfect for low-resource devices such as routers and firewalls. With low system requirements and minimal resource consumption, it can run on multiple operating systems including Linux, BSDs, Android, and macOS.

Getting Started

Before we delve into the installation process, ensure you have the necessary prerequisites:

  • A Rocky Linux 9 server with a hostname (in this example, ‘dnsmasq-rocky’) and an IP address (for instance, ‘192.168.5.50’).
  • A non-root user with sudo/root administrator privileges.
  • Ensure SELinux is running in ‘permissive’ mode.

For client machines, any Linux distribution can be used, be it Debian-based or RHEL-based.

Preparing the System

In RHEL-based operating systems, the default DNS resolver ‘/etc/resolv.conf’ is generated by the NetworkManager service. Before installing Dnsmasq, we need to set up a static DNS resolver via the /etc/resolv.conf file and disable the DNS resolver from the NetworkManager service.

Open the NetworkManager config file by executing:

sudo nano/etc/NetworkManager/NetworkManager.conf

Add the line ‘dns=none’ within the section ‘[section]’.

[main]
dns=none

Save and exit the file. Next, open the DNS resolver config file ‘/etc/resolv.conf’ and replace all available lines with the following:

sudo nano /etc/resolv.conf
nameserver 1.1.1.1
nameserver 8.8.8.8

Restart the NetworkManager service to apply the changes:

sudo systemctl restart NetworkManager

With these settings, your DNS resolver will not be changed by the NetworkManager service, and you can modify the DNS resolver at any time.

Installing Dnsmasq on Rocky Linux

The ‘dnsmasq’ package is available on the Rocky Linux AppStream repository. Install it by running:

sudo dnf install dnsmasq dnsmasq-utils

Start and enable the ‘dnsmasq’ service to run upon system boot:

sudo systemctl start dnsmasq
sudo systemctl enable dnsmasq

To confirm that the ‘dnsmasq’ service is running, use:

sudo systemctl status dnsmasq

Configuring Dnsmasq

Next, we’ll set up Dnsmasq as the local DNS Server. This will include enabling features such as cache DNS and DHCP server. It will also allow for the configuration of domain names and sub-domains for local applications.

Begin by copying the default Dnsmasq config file to ‘/etc/dnsmasq.conf.orig’, then open the original Dnsmasq configuration file ‘/etc/dnsmasq.conf’:

sudo cp /etc/dnsmasq.conf{,.orig}
sudo nano /etc/dnsmasq.conf

Add the following configuration to the file:

# dnsmasq run onUDP port53
# withIP address localhost and 192.168.5.50
# and networkinterfaceeth1
port=53
listen-address=127.0.0.1,192.168.5.50
interface=eth1
# disable forwardingof non-routed address
# disable forwarding names without the main domain.com
# automatically append the domain part to simple names
# disable dnsmasq to read/etc/resolv.conf file
domain-needed
bogus-priv
expand-hosts
no-resolv
# upstreamDNS serverfor non-local domains
# using Cloudflare and googlepublicDNS
server=1.1.1.1
server=8.8.8.8
# define the domainfor dnsmasq
domain=testdomain.com
address=/testdomain.com/192.168.5.50
# enableDNS Cache and adjust cache-size
cache-size=10000
# enable dhcp via dnsmasq
# define lease db file
# make the dhcp serveras an authoritative
dhcp-range=192.168.5.100,192.168.5.150,12h
dhcp-leasefile=/var/lib/dnsmasq/dnsmasq.leases
dhcp-authoritative

Save and exit the file.

Next, open the ‘/etc/hosts’ file and define some sub-domains for applications in your local environment:

sudo nano /etc/hosts
192.168.5.10 web
192.168.5.25 mysql
192.168.5.30 files

Open the DNS resolver config file ‘/etc/resolv.conf’ and add the following lines:

sudo nano /etc/resolv.conf
nameserver127.0.0.1
nameserver192.168.5.50

Use the following command to verify the Dnsmasq configuration:

sudo dnsmasq--test

Finally, restart the ‘dnsmasq’ service to apply the changes:

sudo systemctl restart dnsmasq

Verifying Dnsmasq Installation

Now that we have finished the Dnsmasq configuration, it’s time to verify that everything is working as expected. Start by confirming that Dnsmasq is running on the default port 53:

ss-tulpn | grep 53
sudo systemctl status dnsmas

Next, install the ‘bind-utils’ package to your Rocky Linux server:

sudo dnf install bind-utils

Use thedig command to verify the domain name for the Dnsmasq server:

dig testdomain.com

Verify the sub-domains you defined via the ‘/etc/hosts’ file:

dig web.testdomain.com+short
dig mysql.testdomain.com+short
dig files.testdomain.com+short

Securing the DNS Port with Firewalld

To open the DNS service port and add the internal network IP addresses to Firewalld, run:

sudo firewall-cmd--add-service=dns
sudo firewall-cmd--add-source=192.168.5.0/24
sudo firewall-cmd--runtime-to-permanent
sudo firewall-cmd--reload

Verify the Firewalld configuration with:

sudo firewall-cmd--list-all

Setting Up a Client to Use Dnsmasq

Finally, let’s set up a client machine to use the local DNS Server created with Dnsmasq. For RHEL-Based distributions, use:

sudo nano /etc/NetworkManager/conf.d/dns-servers.conf
[global-dns-domain-*]
servers=192.168.5.50
sudo systemctl restart NetworkManager
sudo dnf install bind-utils

For Debian-Based distributions, use:

sudo systemctl disable--now systemd-resolved
unlink /etc/resolv.conf
sudo nano /etc/resolv.conf
nameserver 192.168.5.50
sudo apt install dnsutils

Conclusion

By now, you should have a local DNS Server with Dnsmasq running on your Rocky Linux system. You’ve learned how to set up a DNS Server, configure local domain names and sub-domains, enable DNS cache, and set up a DHCP Server. Lastly, we’ve covered how to configure client machines to utilize your local DNS Server.