为什么 *.localhost 解析为 127.0.0.1 和 ::1?

为什么 *.localhost 解析为 127.0.0.1 和 ::1?

我偶然发现(尝试使用主机上的 servicename.docker.localhost 访问 Vagrant 盒子上 Docker 容器中的代理服务),任何以“.localhost”结尾的主机名在我的计算机上解析为 127.0.0.1 或 ::1 :

$ ping -4 -c1 -n foo.localhost
PING localhost (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.147 ms

--- localhost ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.147/0.147/0.147/0.000 ms
$ ping -6 -c1 -n foo.localhost
PING foo.localhost(::1) 56 data bytes
64 bytes from ::1: icmp_seq=1 ttl=64 time=0.072 ms

--- foo.localhost ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.072/0.072/0.072/0.000 ms

但是,没有这样的 DNS 条目:

$ nslookup -type=AAAA foo.localhost
Server:     192.168.2.82
Address:    192.168.2.82#53

** server can't find foo.localhost: NXDOMAIN
$ nslookup -type=A foo.localhost
Server:     192.168.2.82
Address:    192.168.2.82#53

** server can't find foo.localhost: NXDOMAIN

/etc/hosts 看起来不是特别相关:

127.0.0.1   my-machine-name.my-work-domain  my-machine-name localhost.localdomain   localhost
::1     localhost.localdomain   localhost

/etc/resolv.conf 也没有:

domain my-work-domain
search my-work-domain
nameserver 192.[…]
nameserver 192.[…]

为什么 *.localhost 会神奇地解析?

这与以下类似但可能不同这个问题


$ getent hosts
127.0.0.1       my-machine-name.my-work-domain my-machine-name localhost.localdomain localhost
127.0.0.1       localhost.localdomain localhost
$ getent hosts foo.localhost
::1             localhost

答案1

这是由myhostnameNSS 模块引起的,该模块将主机名和任何以 .localhost 结尾的名称定义为 ipv4 中的 127.0.0.1 和 ipv6 中的 ::1。

https://www.freedesktop.org/software/systemd/man/nss-myhostname.html

相关内容