17

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
iomartin
  • 271

3 Answers3

16

Use the resolvectl command. Specifically, the dns and domain subcommands can be used to set the nameserver and search domains respectively.

resolvectl dns eth0 8.8.8.8 8.8.4.4
resolvectl domain eth0 my.domain.com

Use resolvectl by itself to show the current DNS configuration.

$ resolvectl
Link 2 (eth0)
Current DNS Server: <main dns ip>
       DNS Servers: <list of extra dns ips>
        DNS Domain: <list of search domains>
2

TL;DR

$ sudo systemd-resolve -i <INTERFACE_NAME> --set-dns=<DNS_SERVER_1> --set-dns=<DNS_SERVER_N>

You can easily set your desired DNS servers for your interfaces on your machines, using systemd-resolve command.

All you have to know is the name of the interface you want to set dns servers for. Assign --set-dns flags, and it's done.

Now, you can make sure whether the changes have been applied or not by executing the following command:

$ sudo systemd-resolve --status <INTERFACE_NAME>
0

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
Mohamed
  • 103