44

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.

4
  • What is your end goal here? Do you want to point the machine to your own DNS server on your local network? Are you on a home network you control or in an enterprise environment? May 24, 2020 at 16:06
  • I want this system to have different DNS servers from those that are provided over DHCP. While still using the default tooling that is used for configuring DNS. May 24, 2020 at 16:47
  • I want this system to have different DNS servers from those that are provided over DHCP In the network, config dialogs you can configure your system to ignore the DHCP-provided DNS and use specific ones.
    – xenoid
    May 24, 2020 at 17:25
  • I disable and mask systemd-resolved and then hand code the /etc/resolv.conf to point to my local unbound . You have to delete the resolv.conf link to create a static file. I have had no problems. May 19, 2022 at 15:51

4 Answers 4

47
  1. Update /etc/systemd/resolved.conf

    [Resolve]
    DNS=1.1.1.1 8.8.8.8
    FallbackDNS=8.8.4.4
    
    
  2. Restart system resolved: service systemd-resolved restart

  3. Run systemd-resolve --status (or resolvectl status in newer versions of systemd). The output should look like this:

    Global
             DNS Servers: 1.1.1.1
                          8.8.8.8
    ...
    
6
  • 8
    I did that, and that did not change the per-link DNS settings May 26, 2020 at 9:06
  • @SergeFonville Is systemd-networkd active? What is the output networkctl status enp0s3
    – Tombart
    May 27, 2020 at 10:35
  • 2
    systemd-networkd is not active and networkctl status enp0s3 outputs the settings of the interface, I noticed specifically State: n/a (unmanaged). Do you want to know something specific? May 28, 2020 at 22:59
  • 11
    You need to change the file the /etc/resolv.conf link points to. Point it at /run/systemd/resolve/resolv.conf. See this bug: bugs.launchpad.net/ubuntu/+source/systemd/+bug/1774632
    – David R.
    Dec 20, 2020 at 19:01
  • 2
    Use resolvectl status instead. In systemd 239 systemd-resolve has been renamed to resolvectl.
    – AntonioK
    Nov 24, 2023 at 8:36
8

This comment from @David R was very helpful:

rm -f /etc/resolv.conf
ln -sv /run/systemd/resolve/resolv.conf /etc/resolv.conf
1
  • Why would that work? N.B. that files in /run are in memory so something has to generate /run/systemd/resolve/resolv.conf for something else to use /etc/resolv.conf.
    – Matthieu
    Feb 5 at 9:15
5

Let the iface variable contain your main interface name, then you can set your DNS addresses this way :

$ sudo resolvectl dns $iface x.y.z.t1 x.y.z.t2
$ sudo resolvectl domain $iface "myDomain.lan"
$ resolvectl dns $iface
Link 2 (ensX): x.y.z.t1 x.y.z.t2
$ resolvectl status $iface
Link 2 (ensX)
      Current Scopes: DNS
DefaultRoute setting: yes
       LLMNR setting: yes
MulticastDNS setting: no
  DNSOverTLS setting: no
      DNSSEC setting: no
    DNSSEC supported: no
  Current DNS Server: x.y.z.t1
         DNS Servers: x.y.z.t1
                      x.y.z.t2
          DNS Domain: myDomain.lan
2
  • All right (whew) that certainly worked but... will it be made persistent? And, if no, where/what has to be changed to make the change persistent — short of having a script manually setting it up on boot? Sep 20, 2022 at 20:16
  • Note: I can confirm that things were changed in /run/systemd/resolve/resolv.conf but... /run is a tmpfs in memory... so where does it get stored? Or, rather, where does the non-global configuration come from? Sep 20, 2022 at 20:26
1

you should explicitly specify in this file /etc/systemd/network/enp0s3.conf to ignore DNS from DHCP. I tend to ignore everything from dhcp in most situations, since I prefer to control this via config-management tools. For example, the DHCPv4 section in the above file would look like:

[DHCPv4]
UseHostname=no
UseDNS=no
UseNTP=no
UseDomains=no

Also note that you should be using .network extension for the file. Also the [Match] section can help you so that you dont need to care about what the device name actually is. in my case I dont have any individual device files and instead do overrides when in aws:

section of /etc/systemd/network/01-ec2-overrides.network

[Match]
Driver=ena ixgbevf vif

[DHCPv4]
UseHostname=no
UseDNS=no
UseNTP=no
UseDomains=no

[DHCPv6]
UseHostname=no
UseDNS=no
UseNTP=no
1
  • Maybe I got something wrong, but first you ask to enter something in .../enp0s3.conf and in the next section you ask to use .network extension for the file. Do you really mean that same file with just another extension or do you refer to some other file? And then which file are you referring to?
    – Gerd
    Jan 15 at 13:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .