与网络接口关联的 DNS 解析器是什么?

与网络接口关联的 DNS 解析器是什么?

我目前正在 ubuntu 20.04 机器上工作。

与网络接口关联的 DNS 解析器/名称服务器究竟是什么?当我检查 /etc/resolv.conf 文件时,我可以看到nameserver 127.0.0.53;当我检查时resolvectl status,我得到的DNS Servers: 172.31.0.2是实例的网络接口 (eth0)。我的实例的网络接口配置为dhcp4: true

  1. 为什么会有两个不同的输出?这有什么关系?
  2. DNS 服务器输出172.31.0.2我从resolvectl status命令中得到的 IP 是否由 DHCP 动态分配给 eth0?如果不是,我有什么办法可以查看 DHCP 提供的 DNS 解析器 IP?

答案1

127.0.0.53是 DNS 存根解析器,它是本地 DNS 缓存服务器。如果您输入以下命令,您将看到/etc/resolv.conf指向 的符号链接/run/systemd/resolve/stub-resolve.conf

$ ls -la /etc/resolv.conf
lrwxrwxrwx 1 root root 37 Mar 20 10:16 /etc/resolv.conf -> /run/systemd/resolve/stub-resolv.conf

然后,如果您查看该文件的内容,它将显示127.0.0.53为 DNS 服务器。

$ cat /etc/resolv.conf
# This is /run/systemd/resolve/stub-resolv.conf managed by man:systemd-resolved(8).
# Do not edit.
#
# This file might be symlinked as /etc/resolv.conf. If you're looking at
# /etc/resolv.conf and seeing this text, you have followed the symlink.
#
# 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 "resolvectl status" to see details about the uplink DNS servers
# currently in use.
#
# Third party programs should typically 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 trust-ad
search .

另一个 DNS 服务器172.31.0.2是通过 DHCP 提供给您的计算机的,并自动添加到/run/systemd/resolve/resolv.conf。这是您的上行链路服务器。

如果您更喜欢使用上行服务器,您可以简单地删除指向存根解析器的当前符号链接并为该文件创建一个新的符号链接。

sudo rm /etc/resolve.conf
sudo ln -s /run/systemd/resolve/resolv.conf /etc/resolv.conf

无论哪种方式,如果你运行dig命令,你都会看到实际正在使用的 DNS 服务器。在我的系统上:

$ dig google.com

; <<>> DiG 9.18.12-0ubuntu0.22.04.1-Ubuntu <<>> google.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 52917
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 65494
;; QUESTION SECTION:
;google.com.                    IN      A

;; ANSWER SECTION:
google.com.             213     IN      A       142.250.72.142

;; Query time: 3 msec
;; SERVER: 127.0.0.53#53(127.0.0.53) (UDP)
;; WHEN: Thu Apr 06 10:59:05 PDT 2023
;; MSG SIZE  rcvd: 55

请注意,它指定了127.0.0.53。这是因为 my/etc/resolv.conf是 的符号链接/run/systemd/resolve/stub-resolv.conf

相关内容