Software & AppsOperating SystemLinux

How To Use dnsmasq with systemd-resolved in Ubuntu 20.04

Ubuntu 11

In this article, we will guide you through the process of using dnsmasq with systemd-resolved in Ubuntu 20.04. This can be beneficial for those who wish to leverage the caching capabilities of dnsmasq alongside the DNSStubListener of systemd-resolved.

Quick Answer

To use dnsmasq with systemd-resolved in Ubuntu 20.04, you need to install dnsmasq, configure it to listen on the loopback interface and forward requests to your preferred DNS servers, configure NetworkManager to use dnsmasq as the DNS server, and verify the configuration. This allows you to leverage dnsmasq’s caching capabilities alongside systemd-resolved’s DNSStubListener for improved performance and DNS resolution.

What is dnsmasq?

dnsmasq is a lightweight, easy-to-configure DNS forwarder and DHCP server. It’s designed to provide DNS and, optionally, DHCP, to a small network. It can serve the names of local machines which are not in the global DNS and it can integrate with the ISC DHCP server to provide network boot services.

What is systemd-resolved?

systemd-resolved is a system service that provides network name resolution to local applications. It implements a caching and validating DNS/DNSSEC stub resolver, as well as an LLMNR resolver and responder.

Prerequisites

Before we start, ensure that you have sudo or root access to the Ubuntu 20.04 system.

Step 1: Installing dnsmasq

First, we need to install dnsmasq. You can do this by running the following command:

sudo apt-get install dnsmasq

Step 2: Configuring dnsmasq

Next, we need to configure dnsmasq. The configuration file for dnsmasq is located at /etc/dnsmasq.conf. Here is an example of how you can add configurations to this file:

sudo tee -a /etc/dnsmasq.conf << ENDdm
interface=lo
bind-interfaces
listen-address=127.0.0.1
server=208.67.222.222
server=208.67.220.220
ENDdm

In this configuration:

  • interface=lo specifies that dnsmasq should only listen on the loopback interface.
  • bind-interfaces makes dnsmasq bind only to the interfaces it is listening on.
  • listen-address=127.0.0.1 tells dnsmasq to listen for requests on the loopback address.
  • server=208.67.222.222 and server=208.67.220.220 are the DNS servers that dnsmasq will forward requests to. You can replace these with your preferred DNS servers.

After adding the configurations, restart dnsmasq to apply the changes:

sudo systemctl restart dnsmasq

To ensure dnsmasq starts at boot, enable it:

sudo systemctl enable dnsmasq

Step 3: Configuring NetworkManager to use dnsmasq

Next, we need to configure NetworkManager to use dnsmasq. This can be done by modifying the active network connection’s settings. Here is an example of how you can do this:

NetManProfile=$(nmcli -t connection show --active | cut -f 1 -d ':')
nmcli con mod "$NetManProfile" ipv4.dns ""
nmcli con mod "$NetManProfile" ipv4.ignore-auto-dns yes
nmcli con mod "$NetManProfile" ipv4.dns 127.0.0.1
nmcli connection down "$NetManProfile"
nmcli connection up "$NetManProfile"

In this example:

  • The nmcli -t connection show --active | cut -f 1 -d ':' command is used to get the name of the first active network connection.
  • The ipv4.dns "" command removes any existing DNS servers.
  • The ipv4.ignore-auto-dns yes command disables automatic DNS server configuration.
  • The ipv4.dns 127.0.0.1 command sets the DNS server to the loopback address, where dnsmasq is listening.

Step 4: Verifying the Configuration

Finally, we need to verify that the configuration is correct. Here are some commands you can use to do this:

  • To check that systemd-resolved is listening on 127.0.0.53:
    netstat -antup | grep 127.0.0.53
  • To check that dnsmasq is listening on 127.0.0.1:
    netstat -antup | grep 127.0.0.1:53
  • To check that systemd-resolved is using the DNS server set in NetworkManager:
    cat /run/systemd/resolve/resolv.conf

If everything is configured correctly, you should now be using dnsmasq with systemd-resolved in Ubuntu 20.04.

Conclusion

In this article, we’ve covered how to use dnsmasq with systemd-resolved in Ubuntu 20.04. By following these steps, you can take advantage of the caching capabilities of dnsmasq and the DNSStubListener of systemd-resolved. This can be especially useful in environments where DNS queries are common and caching can help improve performance.

Remember, while this guide focuses on Ubuntu 20.04, the same steps should apply to other versions of Ubuntu and similar distributions that use systemd-resolved and NetworkManager. Always make sure to test changes in a controlled environment before applying them to production systems.

For more information about dnsmasq and systemd-resolved, check out their official documentation at the dnsmasq homepage and the systemd-resolved man page.

What is the purpose of using dnsmasq with systemd-resolved?

Using dnsmasq with systemd-resolved allows you to take advantage of dnsmasq’s caching capabilities alongside the DNSStubListener of systemd-resolved. This can improve DNS query performance by caching frequently accessed DNS information.

How do I install dnsmasq in Ubuntu 20.04?

To install dnsmasq in Ubuntu 20.04, you can use the command sudo apt-get install dnsmasq.

Where is the configuration file for dnsmasq located?

The configuration file for dnsmasq is located at /etc/dnsmasq.conf.

How can I restart dnsmasq to apply configuration changes?

To restart dnsmasq and apply configuration changes, you can use the command sudo systemctl restart dnsmasq.

How can I verify that dnsmasq and systemd-resolved are functioning correctly?

You can use the netstat -antup command to check if dnsmasq is listening on 127.0.0.1 and if systemd-resolved is listening on 127.0.0.53. Additionally, you can check the /run/systemd/resolve/resolv.conf file to see if systemd-resolved is using the DNS server set in NetworkManager.

Can I use dnsmasq with systemd-resolved in other versions of Ubuntu or similar distributions?

Yes, the steps outlined in this article should apply to other versions of Ubuntu and similar distributions that use systemd-resolved and NetworkManager. However, it’s always recommended to test changes in a controlled environment before applying them to production systems.

Where can I find more information about dnsmasq and systemd-resolved?

You can find more information about dnsmasq at the dnsmasq homepage and about systemd-resolved in the systemd-resolved man page.

Leave a Comment

Your email address will not be published. Required fields are marked *