4

I discovered by chance (trying to access a proxied service in a Docker container on a Vagrant box using servicename.docker.localhost from the host) that any hostname ending in ".localhost" resolves to 127.0.0.1 or ::1 on my machine:

$ 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

However, there is no DNS entry for this:

$ 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 doesn't look particularly relevant:

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

Neither does /etc/resolv.conf:

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

Why does *.localhost magically resolve?

This is similar but possibly distinct from this question.


$ 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
  • What does getent say? – Ignacio Vazquez-Abrams May 28 '18 at 0:16
  • Are you using systemd? It has a last resort resolver that (if none other nameserver resolved it before) resolves localhost to ::1. – Isaac May 28 '18 at 9:04
  • @Isaac I think you can write that as an answer. From the page you linked to: "systemd-resolved synthesizes DNS resource records (RRs) for the following cases: [...] any hostname ending in ".localhost" or ".localhost.localdomain") are resolved to the IP addresses 127.0.0.1 and ::1" – Johan Myréen May 28 '18 at 15:47
  • Also just FYI, before /etc/resolv.conf and /etc/hosts there is /etc/nsswitch.conf which controls what sources the libc resolver uses, and in what order. grep '^hosts:' /etc/nsswitch.conf. – Patrick May 28 '18 at 19:54
3

This is caused by the myhostname NSS module which defines the hostname and any name ending in .localhost as 127.0.0.1 in ipv4 and ::1 in ipv6.

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

  • nss-systemd could theoretically do something similar but not only is Arch Linux not configured like this by default nor is systemd. – jdwolf May 28 '18 at 20:49
  • You nailed it - /etc/nsswitch.conf enables myhostname. – l0b0 May 28 '18 at 20:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.