5 秒主机名 -f 和一个奇怪的 DNS 请求

5 domain-name-system gentoo fqdn

我的问题解决了我的 Gentoo Linux 系统上 hostname -f 的奇怪行为。

症状:

rt01 ~ # time hostname -f
rt01.domain.net

real    0m5.007s
user    0m0.001s
sys     0m0.000s
Run Code Online (Sandbox Code Playgroud)

使用 strace 我可以看到执行了以下步骤:

  • 阅读 /etc/host.conf(可能)以确定应该如何进行主机查找的顺序
  • 阅读 /etc/hosts (在我看来这应该足够了,而不是......)
  • 连接到 /etc/resolv.conf 中指定的 DNS 服务器
  • 再次读取 /etc/hosts

该机器上的 iptables 设置阻止它连接到任何 DNS 服务器。所以它 - 按设计 - 在这一点上被阻止。我的期望是,使用以下配置不需要此步骤:

/etc/host.conf

# This keyword specifies how host lookups are to be performed. It
# should be followed by one or more lookup methods, separated by
# commas.  Valid methods are bind, hosts, and nis.
#
order hosts, bind
Run Code Online (Sandbox Code Playgroud)

/etc/hosts

# IPv4 and IPv6 localhost aliases
127.0.0.1       rt01.domain.net rt01 localhost
<public ip>     rt01.domain.net rt01
::1             localhost
Run Code Online (Sandbox Code Playgroud)

/etc/conf.d/主机名

# Set to the hostname of this machine
hostname="rt01"
Run Code Online (Sandbox Code Playgroud)

有人可以向我解释一下,为什么 hostname -f 尝试连接到 DNS 服务器。我认为 /etc/hosts 提供的信息应该足够了。

zym*_*han 3

DNS 查询的解析顺序取决于 的内容/etc/nsswitch.conf。具体来说,该行的选项顺序为hosts

例如,此行将导致您的计算机在检查本地文件之前查询服务器:

hosts:          dns files
Run Code Online (Sandbox Code Playgroud)

手册页:http://man7.org/linux/man-pages/man5/nsswitch.conf.5.html

  • 删除 dns(以便保留“主机:文件”)解决了我的问题。非常感谢。 (2认同)