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

我的问题解决了我的Gentoo Linux系统上主机名-f的一个奇怪的行为。

症状:

rt01 ~ # time hostname -f rt01.domain.net real 0m5.007s user 0m0.001s sys 0m0.000s 

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

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

该机器上的iptables安装程序阻止它连接到任何DNS服务器。 所以在这一点上,这是被devise阻止的。 我的期望是,以下configuration不需要执行此步骤:

是/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 

/ etc / hosts文件

 # IPv4 and IPv6 localhost aliases 127.0.0.1 rt01.domain.net rt01 localhost <public ip> rt01.domain.net rt01 ::1 localhost 

/etc/conf.d/hostname

 # Set to the hostname of this machine hostname="rt01" 

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

DNS查询的parsing顺序取决于/etc/nsswitch.conf的内容。 具体来说就是hosts上的选项顺序

例如,这一行将会使您的机器在检查本地文件之前查询服务器:

 hosts: dns files 

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