I want to get fast dns resolution with dnsmasq and keep the default systemd-resolved.
Looking for an elegant way to do this
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 - With sudo
apt-get -y install dnsmasq
2 - With sudo
tee -a /etc/dnsmasq.conf << ENDdm interface=lo bind-interfaces listen-address=127.0.0.1 # DNS server from OpenDns. Use yours... server=126.96.36.199 server=188.8.131.52 ENDdm 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 127.0.0.1 # 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
netstat -antup Proto Recv-Q Send-Q Adresse locale Adresse distante Etat PID/Program name tcp 0 0 127.0.0.1:53 0.0.0.0:* LISTEN 1036/dnsmasq tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN 766/systemd-resolve cat /run/systemd/resolve/resolv.conf nameserver 127.0.0.1
I tried to find a reasonable solution and looks that there are different approaches.
I wanted to stay at most within the distribution layout while keeping all business requirements fulfilled. This is what I collected around and tested to work on clean Ubuntu 18.04 and KDE Neon flavour:
# Install required package and reconfigure service plans (i.e. disablesystemd-resolved, enable dnsmasq sudo apt-get install dnsmasq sudo systemctl disable systemd-resolved sudo systemctl stop systemd-resolved sudo systemctl enable dnsmasq # These two lines should work on most environments, but .. :-) - so I kept them commented out for less experienced users # Just add or change 'dns=dnsmasq' to your NetworkManager.conf to the section [main] # and yes, the sed expression can be better :-) #sudo cp /etc/NetworkManager/NetworkManager.conf /etc/NetworkManager/NetworkManager.conf.backup #sudo bash -c 'cat /etc/NetworkManager/NetworkManager.conf.backup |sed -e "s/^dns=.*//"| sed -e "s/\[main\]/\[main\]\ndns=dnsmasq/" >/etc/NetworkManager/NetworkManager.conf' # Restart NetworkManager to make the change above applied sudo systemctl restart NetworkManager # This removes the systemd resolv.conf link only if it has NetworkManager replacement :-) ls /var/run/NetworkManager/resolv.conf && sudo rm /etc/resolv.conf # And add NetworkManager's resolv.conf available for the system resolver sudo ln -s /var/run/NetworkManager/resolv.conf /etc/resolv.conf
(please note that the only general difference with the above answers is that the NetworkManager handle the dnsmasq DNS server assignments automatically
IMHO, if your going to be running dnsmasq, you should statically assign your ip address instead of getting it from dhcp. This way you can just disable systemd-resolved all together.
sudo apt-get install dnsmasq
sudo systemctl disable systemd-resolved
sudo systemctl stop systemd-resolved
Manually assign your ip address, gateway, and assign the ip address to your machine as DNS.
configure /etc/dnsmasq.conf (really...RTFM --> man dnsmasq.conf)
sudo systemctl enable dnsmasq
sudo systemctl status dnsmasq
point dhcp on your dhcp server to your shiny new dnsmasq server (..if yumpto)