linuxhosts-file

app.localhost is still resolved by curl as 127.0.0.1 though overriden in /etc/hosts on Amazon Linux 2


Is there a config to change the behaviour of the name resolver of Amazon Linux 2 to not ignore entry about localhost subdomains ?

i.e currently it does :

docker run -it amazonlinux:2023 /bin/bash  -c "echo '172.4.0.2 app.localhost' >> /etc/hosts ; curl -v app.localhost "

*   Trying 127.0.0.1:80...

while centos (or ubuntu) works "as expected"

docker run -it centos /bin/bash  -c "echo '172.4.0.2 app.localhost' >> /etc/hosts ; curl -v app.localhost "
*   Trying 172.4.0.2...

Why do I need this

On our local development environment I have 2 docker

  1. one with our main application, running on a Amazon Linux 2 (to be as close as possible to production environment)
  2. one containing a mock of an other HTTP service

as the mock needs to be accessed both by the main application from the backend, but also by the frontend, having the mock being named app.localhost allows

  1. on the frontend to use the fact that we don't need to create extra /etc/hosts entry
  2. both frontend and backend use the same name/env variable containing the hostname, which is also the case in production

What I've checked

  1. both have the same `/etc/host.conf config
  2. getent hosts app.localhost for both returns 172.4.0.2
  3. the glic version of Amazon Linux 2 is 2.34 and centos 2.28, and ubuntu 2.35 , so at least it's not a behaviour change in glibc
  4. change the /etc/nsswitch.conf to match the one of my ubuntu
passwd:         files systemd
group:          files systemd
shadow:         files
gshadow:        files

hosts:          files mdns4_minimal [NOTFOUND=return] dns
networks:       files

protocols:      db files
services:       db files
ethers:         db files
rpc:            db files

Is there other places that may affect how localhost names are resolved ?


Solution

  • The difference comes from the version of curl

    see https://daniel.haxx.se/blog/2021/05/31/curl-localhost-as-a-local-host/ and more specifically this commit https://github.com/curl/curl/commit/b5c0fe20e370ea2e5791fce4cedb34a71bc784e5#diff-e0b647f3e21a7fb58400dcec74f381d2fe16ca41e983240c15fc1243c574dbf9R733

    i.e for domain name ending with .localhost curl directly returns 127.0.0.1 and bypass all name resolving.

    Their justification is that RFC 6761 explains

    Name resolution APIs and libraries SHOULD recognize localhost names as special and SHOULD always return the IP loopback address for address queries and negative responses for all other query types. Name resolution APIs SHOULD NOT send queries for localhost names to their configured caching DNS server(s)