Ubuntu

DNSMasq Ubuntu Tutorial

“The DNSMasq is a lightweight DHCP server and DNS forwarder capable of handling a few DHCP and DNS requests for a few clients on your network. Furthermore, it offers support for BOOTP, TFTP, IPv6, and PXE for network booting.

DNSMasq works for small network environments and will work perfectly in handling DNS queries and comes in handy to off-lift you the burden of configuring your DNS server. This guide will cover the installation, configuration, and use of DNSMasq on Ubuntu.”

How to Install and Configure DNSMasq on Ubuntu

Before installing DNSMasq, let’s understand the different subsystems it offers.

1. DNS subsystem – the subsystem handles caching of various record types, such as PTR, CNAME, and AAAA.

2. DHCP subsystem – offers support for PXE, BOTP, DHCPv6, and v4.

3. The advertisement subsystem is responsible for the autoconfiguration of IPv6 hosts.

By default, Ubuntu has systemd-resolved, which is enabled by default, but you need to disable it before installing DNSMasq to avoid conflict with each other.

Start by updating your repository

$ sudo apt update

Next, disable systemd-resolved using the commands below.

$ sudo systemctl disable systemd-resolved

$ sudo systemctl stop systemd-resolved

Also, you must remove the configuration file for systemd-resolved, and you can use the commands below.

$ sudo unlink /etc/resolv.conf

or

$ sudo rm -rf /etc/resolv.conf

With the configuration removed, you need to create a new one and add the Google DNS.

$ echo “nameserver 8.8.8.8” | sudo tee /etc/resolv.conf

Once you have the new configuration file, it’s time to install DNSMasq using the commands below. Start by updating the system.

$ sudo apt update

$ sudo apt install dnsmasq

The next thing is to modify the DNSMasq configuration file. Here, you need to open it using an editor like vim or vi, as shown below.

$ sudo vim /etc/dnsmasq.conf

Once you open the file, go ahead and modify the configuration.

Most lines are commented out with an explanation of what each line does. In your case, you need to uncomment some of these lines.

Here’s a quick cheat-sheet of what to uncomment.

port=53

domain-needed

bogus-priv

strict-order

expand-hosts

domain=linuxexample.com

listen-address=127.0.0.1, server-ip

cache-size=1000

Also, set your domain to be used by dnsmasq, listening address, and set the cache size as indicated above while replacing the name and IP to match yours.

Finally, save and close the file with everything in check.

Those are not the only configurations that you can make. If you see anything relevant, configure it, and once done, restart DNSMasq.

$ sudo systemctl restart dnsmasq

In the same configuration file we added the Google DNS, we need to add the DNSMasq IP address. The IP address will depend on your network, and in our case, we are using a Class C IP address 192.168.0.2.

Open the nano editor and add the line below.

nameserver 192.168.0.2
$ sudo nano /etc/resolv.conf

You must also add local DNS records to the DNSMasq server by creating their entries in the /etc/hosts file.

$ sudo nano /etc/hosts

The DNS records you add will be responsible for replying to the client’s queries. In the image below, we’ve added two records.

Restart the DNSMasq service for the records to get updated.

$ sudo systemctl restart dnsmasq

Verify the configuration by running the test command below.

$ dnsmasq --test

It should return an Ok status confirming everything is working fine.

Testing the DNSMasq DNS

The configured DNS records can be tested using the dig command, which returns the DNS information if it exists. For instance, let’s try server1.com. The command will be:

$ dig A server1.com

Replace server1.com with the DNS record you created.

In the image below, it returns an answer meaning everything is fine.

You can also verify the local DNS resolution to ensure the DNS records are getting responses.

$ dig your-dns.com +short

It should return the server IP.

Verify DNS Server Caching

You need to use the drill Linux command to check and verify the caching. If caching works, you should note a decrease in the time taken to query the DNS.

Query the first time.

$ drill linuxhint.com | grep "Query time"

The second time should return a shorter query time as proof that caching is working.

$ drill linuxhint.com | grep "Query time"

Conclusion

There are different ways to use DNSMasq in Linux. What we’ve covered here is a scratch to get you started. We’ve seen how you can install DNSMasq, configure it, and test its working. You can implement this knowledge on your small network and achieve great functionalities.

About the author

Denis Kariuki

Denis is a Computer Scientist with a passion for Networking and Cyber Security. I love the terminal, and using Linux is a hobby. I am passionate about sharing tips and ideas about Linux and computing.