Ubuntu – Override Ubuntu 20.04 DNS using systemd-resolved

dnssystemd-resolvedUbuntu

How do I configure an Ubuntu 20.04 system so it overrides the default DNS?

It seems that by default there is a global and per-link DNS setting.

I tried a couple of things that did not work:

  • Edit /etc/systemd/resolved.conf with the DNS Servers
  • Created /etc/systemd/network/enp0s3.conf with the DNS servers
    configured
  • Removed all DNS related parameters from the DHCP request by editing
    /etc/dhcp/dhclient.conf

All these changes (and the combinations) result in the DNS servers being prepended to the list of global DNS servers.

Most 'solutions' are to either install resolvconf or replace the /etc/resolv.conf symbolic link with a file and set the DNS servers there. Both of these seem like a workaround.

I would like to use the existing tooling (systemd-resolved) to override the DNS Servers.

As suggested by @xenoid in the comments:
Setting the DNS for the interface through the GUI resulted in a file /etc/NetworkManager/system-connections/enp0s3.nmconnection that contains the correct DNS servers, the output of resolvectl status includes the correct DNS servers, this however is not what I had in mind.
I am looking for a solution that does the configuration using systemd-resolved, which is possible from what I can find, but it is unclear how. Since this requires a GUI installation.

Best Answer

  1. Update /etc/systemd/resolved.conf
[Resolve]
DNS=1.1.1.1 8.8.8.8
FallbackDNS=8.8.4.4

  1. Restart system resolved: service systemd-resolved restart
  2. Run systemd-resolve --status

the output should look like this:

Global
         DNS Servers: 1.1.1.1
                      8.8.8.8
...
Related Question