Adding a new DNS server with systemd-resolved

domain-name-systeminternal-dnsnetworkingsystemd

To access other machines on my network by their name, I have to add the following two lines to my /etc/resolv.conf:

search foo.local
nameserver 192.168.X.Y

But any changes I make do not persist across reboots.

This is the content of my /etc/resolv.conf, which is actually a symlink to /run/systemd/resolve/stub-resolv.conf:

$ cat /etc/resolv.conf
# This file is managed by man:systemd-resolved(8). Do not edit.
#
# This is a dynamic resolv.conf file for connecting local clients to the
# internal DNS stub resolver of systemd-resolved. This file lists all
# configured search domains.
#
# Run "systemd-resolve --status" to see details about the uplink DNS servers
# currently in use.
#
# Third party programs must not access this file directly, but only through the
# symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,
# replace this symlink by a static file or a different symlink.
#
# See man:systemd-resolved.service(8) for details about the supported modes of
# operation for /etc/resolv.conf.

nameserver 127.0.0.53
options edns0

I read the manuals mentioned above as well as the ArchWiki on systemd-resolved, but I could not figure out what needs to be done.

This is a Ubuntu 18.04 VM (on a Ubuntu 18.04 host).

$ systemd-resolve --status
Global
          DNSSEC NTA: 10.in-addr.arpa
                      16.172.in-addr.arpa
                      168.192.in-addr.arpa
                      17.172.in-addr.arpa
                      18.172.in-addr.arpa
                      19.172.in-addr.arpa
                      20.172.in-addr.arpa
                      21.172.in-addr.arpa
                      22.172.in-addr.arpa
                      23.172.in-addr.arpa
                      24.172.in-addr.arpa
                      25.172.in-addr.arpa
                      26.172.in-addr.arpa
                      27.172.in-addr.arpa
                      28.172.in-addr.arpa
                      29.172.in-addr.arpa
                      30.172.in-addr.arpa
                      31.172.in-addr.arpa
                      corp
                      d.f.ip6.arpa
                      home
                      internal
                      intranet
                      lan
                      local
                      private
                      test

Link 2 (ens2)
      Current Scopes: DNS
       LLMNR setting: yes
MulticastDNS setting: no
      DNSSEC setting: no
    DNSSEC supported: no
         DNS Servers: 192.168.122.1

Best Answer

In case you are using systemd-resolved instead of NetworkManager, add the following line to the very beginning to /etc/resolv.conf.head:

/etc/resolv.conf.head

nameserver 127.0.0.1

Prevent NetworkManager from modifying /etc/resolv.conf and leave everything to systemd-resolved by adding the following line under the [main] section of /etc/NetworkManager/NetworkManager.conf

/etc/NetworkManager/NetworkManager.conf

dns=none

As a last step you will have to restart systemd-resolved.

host> sudo systemctl stop systemd-resolved
host> sudo systemctl start systemd-resolved

Once done, you can verify if the new DNS settings are effective:

host> systemd-resolve --status