使用 systemd-resolved 添加新的 DNS 服务器

使用 systemd-resolved 添加新的 DNS 服务器

要通过名称访问我网络上的其他机器,我必须在我的文件中添加以下两行/etc/resolv.conf

search foo.local
nameserver 192.168.X.Y

但我所做的任何更改在重启后都不会保留。

这是我的 的内容/etc/resolv.conf,它实际上是 的符号链接/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

我阅读了上述手册以及ArchWiki 上的 systemd-resolved,但我不知道需要做什么。

这是 Ubuntu 18.04 VM(在 Ubuntu 18.04 主机上)。

$ 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

答案1

如果您使用的是 systemd-resolved 而不是 NetworkManager,请将以下行添加到最开始/etc/resolv.conf.head

/etc/resolv.conf.head

名称服务器 127.0.0.1

通过在以下部分 下添加以下行,防止 NetworkManager 修改/etc/resolv.conf并将所有内容留给 systemd-resolved[main]/etc/NetworkManager/NetworkManager.conf

/etc/NetworkManager/NetworkManager.conf

dns=无

最后一步,您必须重新启动systemd-resolved

主机> sudo systemctl stop systemd-resolved
主机> sudo systemctl start systemd-resolved

完成后,您可以验证新的 DNS 设置是否有效:

主机> systemd-resolve --status

答案2

使用resolvectl命令。具体来说,dnsdomain子命令可分别用于设置nameserversearch域。

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

resolvectl单独使用可显示当前 DNS 配置。

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

相关内容