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.
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.
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
In this configuration:
interface=lospecifies that dnsmasq should only listen on the loopback interface.
bind-interfacesmakes dnsmasq bind only to the interfaces it is listening on.
listen-address=127.0.0.1tells dnsmasq to listen for requests on the loopback address.
server=18.104.22.168are 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:
nmcli -t connection show --active | cut -f 1 -d ':'command is used to get the name of the first active network connection.
ipv4.dns ""command removes any existing DNS servers.
ipv4.ignore-auto-dns yescommand disables automatic DNS server configuration.
ipv4.dns 127.0.0.1command 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:
If everything is configured correctly, you should now be using dnsmasq with systemd-resolved in Ubuntu 20.04.
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.
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.
To install dnsmasq in Ubuntu 20.04, you can use the command
sudo apt-get install dnsmasq.
The configuration file for dnsmasq is located at
To restart dnsmasq and apply configuration changes, you can use the command
sudo systemctl restart dnsmasq.
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.
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.