为什么 nslookup 在 ping 时不使用 mdns?

dsp*_*pjm 9 networking domain-name-system resolv.conf ping dnsmasq

在 dnsmasq.conf 中:

address=/local/127.0.0.1
Run Code Online (Sandbox Code Playgroud)

在 resolv.conf 中:

# Generated by NetworkManager
domain example.com
search example.com
nameserver 127.0.0.1
nameserver 10.66.127.17
nameserver 10.68.5.26
Run Code Online (Sandbox Code Playgroud)

我可以使用 nslookup:

# nslookup www.local
Server:     127.0.0.1
Address:    127.0.0.1#53

Name:   www.local
Address: 127.0.0.1
Run Code Online (Sandbox Code Playgroud)

但我不能使用 ping:

# ping www.local
ping: unknown host www.local
Run Code Online (Sandbox Code Playgroud)

我在 ping www.local 时使用 tcpdump 捕获 lo,没有数据包,而数据包如

# tcpdump -i em1 -n | grep local

tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on em1, link-type EN10MB (Ethernet), capture size 65535 bytes
20:14:38.189335 IP 10.66.65.188.mdns > 224.0.0.251.mdns: 0 A (QM)? www.local. (27)
20:14:39.190700 IP 10.66.65.188.mdns > 224.0.0.251.mdns: 0 A (QM)? www.local. (27)
20:14:41.192979 IP 10.66.65.188.mdns > 224.0.0.251.mdns: 0 A (QM)? www.local. (27)
Run Code Online (Sandbox Code Playgroud)

从物理接口出现。

这意味着 ping 正在使用 mdns,但为什么 nslookup 不使用 mdns?当 mdns 不返回有用的错误时,为什么 ping 不会使用正常的 dns?

谢谢。

小智 8

ping使用 glibc 的名称解析系统,称为 Name Service Switch。这使用该/etc/nsswitch.conf文件来知道在哪里查找以便将名称解析为 IP。hosts:此文件中的行表示每个服务的优先顺序。例如,files代表本地/etc/hosts文件,dns使用该/etc/resolv.conf文件联系 DNS 服务器,并mdns使用 mdns。

但是,nslookup不使用它。它直接与指定的 DNS 服务器通信/etc/resolv.conf,因此不能使用mdns.

但我无法回答你最后一个问题。如果你同时mdnsdns/etc/nsswitch.conf,即使mdns第一,它应该首先尝试解析名称mdns,那么如果没有答案使用dns