为什么当 nslookup 尝试解析地址时不查询我的 /etc/hosts 文件?

Meh*_*ran 19 dns hosts

127.0.0.1在我的/etc/hosts文件中解析了几个本地域。一段时间内一切正常,但现在当我运行时:

nslookup test.local
Run Code Online (Sandbox Code Playgroud)

结果是:

Server:     192.168.1.3
Address:    192.168.1.3#53

** server can't find test.local: NXDOMAIN
Run Code Online (Sandbox Code Playgroud)

192.168.1.3是我们的网络 DNS,它不应该知道我的本地域test.local。经过几次搜索,我发现该/etc/nsswitch.conf文件包含有关要查询的 DNS 源优先级的信息。但是那里没有问题!这是我的:

# /etc/nsswitch.conf
#
# Example configuration of GNU Name Service Switch functionality.
# If you have the `glibc-doc-reference' and `info' packages installed, try:
# `info libc "Name Service Switch"' for information about this file.

passwd:         compat
group:          compat
shadow:         compat

hosts:          files mdns4_minimal [NOTFOUND=return] dns
networks:       files

protocols:      db files
services:       db files
ethers:         db files
rpc:            db files

netgroup:       nis
Run Code Online (Sandbox Code Playgroud)

那么有谁知道为什么我的hosts文件不包含在 DNS 查找中?

Oli*_*Oli 26

nslookup只进行正确的DNS 解析,这与您的其他应用程序使用的名称服务切换子系统有很大不同;也就是说nslookup忽略/etc/hosts和mDNS。

要测试这样的本地分辨率,请使用使用 NSS 的东西。ping <hostname>例如。这是一个基于/etc/hosts我网络上的条目的简单演示。

$ nslookup bert
Server:     8.8.8.8
Address:    8.8.8.8#53

** server can't find bert: NXDOMAIN

$ ping bert
PING bert (10.10.0.4) 56(84) bytes of data.
64 bytes from bert (10.10.0.4): icmp_seq=1 ttl=64 time=0.352 ms
64 bytes from bert (10.10.0.4): icmp_seq=2 ttl=64 time=0.407 ms
Run Code Online (Sandbox Code Playgroud)

请注意,有一些 DNS 服务器和代理可以影响/etc/hosts文件。在这些情况下,nslookup可能会从本地源返回结果。

  • 应该使用“getent ahosts”而不是“ping”,因为这不需要“ping”具有的所有额外内容。 (6认同)