Why does *.localhost resolve to 127.0.0.1 and ::1?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
3
down vote

favorite












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






share|improve this question





















  • What does getent say?
    – Ignacio Vazquez-Abrams
    May 28 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 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 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 at 19:54














up vote
3
down vote

favorite












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






share|improve this question





















  • What does getent say?
    – Ignacio Vazquez-Abrams
    May 28 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 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 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 at 19:54












up vote
3
down vote

favorite









up vote
3
down vote

favorite











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






share|improve this question













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








share|improve this question












share|improve this question




share|improve this question








edited May 28 at 3:39
























asked May 27 at 23:34









l0b0

26k17104226




26k17104226











  • What does getent say?
    – Ignacio Vazquez-Abrams
    May 28 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 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 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 at 19:54
















  • What does getent say?
    – Ignacio Vazquez-Abrams
    May 28 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 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 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 at 19:54















What does getent say?
– Ignacio Vazquez-Abrams
May 28 at 0:16




What does getent say?
– Ignacio Vazquez-Abrams
May 28 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 at 9:04




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 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 at 15:47




@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 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 at 19:54




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 at 19:54










1 Answer
1






active

oldest

votes

















up vote
3
down vote



accepted










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






share|improve this answer





















  • 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 at 20:49










  • You nailed it - /etc/nsswitch.conf enables myhostname.
    – l0b0
    May 28 at 20:58











Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f446371%2fwhy-does-localhost-resolve-to-127-0-0-1-and-1%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote



accepted










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






share|improve this answer





















  • 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 at 20:49










  • You nailed it - /etc/nsswitch.conf enables myhostname.
    – l0b0
    May 28 at 20:58















up vote
3
down vote



accepted










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






share|improve this answer





















  • 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 at 20:49










  • You nailed it - /etc/nsswitch.conf enables myhostname.
    – l0b0
    May 28 at 20:58













up vote
3
down vote



accepted







up vote
3
down vote



accepted






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






share|improve this answer













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







share|improve this answer













share|improve this answer



share|improve this answer











answered May 28 at 20:48









jdwolf

2,362116




2,362116











  • 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 at 20:49










  • You nailed it - /etc/nsswitch.conf enables myhostname.
    – l0b0
    May 28 at 20:58

















  • 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 at 20:49










  • You nailed it - /etc/nsswitch.conf enables myhostname.
    – l0b0
    May 28 at 20:58
















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 at 20:49




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 at 20:49












You nailed it - /etc/nsswitch.conf enables myhostname.
– l0b0
May 28 at 20:58





You nailed it - /etc/nsswitch.conf enables myhostname.
– l0b0
May 28 at 20:58













 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f446371%2fwhy-does-localhost-resolve-to-127-0-0-1-and-1%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?