Ubuntu – How to Add dnsmasq and keep systemd-resolved (18.04 to 20.04)


I want to get fast dns resolution with dnsmasq and keep the default systemd-resolved.

Looking for an elegant way to do this

Best Answer

I wanted to get fast dns resolution with dnsmasq and keep the default systemd-resolved/NetworkManager setup untouched for future use. Yes the huge dns caching of dnsmasq can improve browsing speed. Yes the goal was to keep the default featured dns setup of 18.04

  1. Install dnmasq
  2. Configure it (listen address and dns servers)
  3. Configure NetWorkManager for manual dns server address
  4. Check verify

1 - With sudo

apt-get -y install dnsmasq

2 - With sudo

tee -a /etc/dnsmasq.conf << ENDdm
# DNS server from OpenDns. Use yours...

systemctl restart dnsmasq
systemctl enable dnsmasq

3 - With USER, configure NetworkManager

# Get NM first active profile name
NetManProfile=$(nmcli -t  connection show --active | cut -f 01 -d ':')
# remove, if exists, current dns servers
nmcli con mod "$NetManProfile" ipv4.dns ""
# set 'manual' dns server
nmcli con mod "$NetManProfile" ipv4.ignore-auto-dns yes
# set dnsmasq as manually set dns server
nmcli con mod "$NetManProfile" ipv4.dns
# i also disabled ip6, do what u want
nmcli con mod "$NetManProfile" ipv6.method ignore
# reconnect to take effect
nmcli connection down "$NetManProfile"
nmcli connection up "$NetManProfile"

4 - Check verify

  • systemd-resolved listen on as should by default
  • dnsmasq listen on as set in /etc/dnsmasq
  • systemd-resolved took from NetworkManager
netstat -antup
Proto Recv-Q Send-Q Adresse locale          Adresse distante        Etat       PID/Program name    
tcp        0      0  *               LISTEN      1036/dnsmasq        
tcp        0      0 *               LISTEN      766/systemd-resolve
cat /run/systemd/resolve/resolv.conf 
Related Question