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.

  • 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? Commented 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. Commented 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
    Commented 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. Commented May 19, 2022 at 15:51

4 Answers 4

  1. Update /etc/systemd/resolved.conf

  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:

             DNS Servers:
  • 8
    I did that, and that did not change the per-link DNS settings Commented May 26, 2020 at 9:06
  • @SergeFonville Is systemd-networkd active? What is the output networkctl status enp0s3
    – Tombart
    Commented 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? Commented 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.
    Commented Dec 20, 2020 at 19:01
  • 2
    Use resolvectl status instead. In systemd 239 systemd-resolve has been renamed to resolvectl.
    – AntonioK
    Commented Nov 24, 2023 at 8:36

This comment from @David R was very helpful:

rm -f /etc/resolv.conf
ln -sv /run/systemd/resolve/resolv.conf /etc/resolv.conf
  • 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
    Commented Feb 5 at 9:15

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
          DNS Domain: myDomain.lan
  • 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? Commented 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? Commented Sep 20, 2022 at 20:26

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:


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

Driver=ena ixgbevf vif


  • 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
    Commented Jan 15 at 13:46

You must log in to answer this question.

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