See here, or man gethostbyname
.
It seems to work as expected when run on a simple C program that calls gethostbyname
, which I had ChatGPT write, and which includes:
hints.ai_family = AF_UNSPEC; // AF_INET or AF_INET6 to force version
hints.ai_socktype = SOCK_STREAM;
status = getaddrinfo(argv[1], NULL, &hints, &res)
It seems to work as expected:
$ cat h
my_alias google.com
$ HOSTALIASES=./h ./test_hostaliases my_alias
IP addresses for my_alias:
IPv4: 172.253.63.139
IPv4: 172.253.63.102
…
$ sudo HOSTALIASES=./h ltrace -e getaddrinfo ./test_hostaliases my_alias
test_hostaliases->getaddrinfo("my_alias", nil, 0x7ffe8a4224b0, 0x7ffe8a4224a8) = 0
IP addresses for my_alias:
IPv4: 172.253.63.101
IPv4: 172.253.63.100
…
….but ping
does not work:
$ HOSTALIASES=./h ping my_alias
ping: my_alias: Name or service not known
…but trying to debug with ltrace
I realized it does work as root:
$ sudo HOSTALIASES=./h ltrace -e getaddrinfo ping my_alias
ping->getaddrinfo("my_alias", nil, 0x7fff7e3baba0, 0x7fff7e3bab88) = 0
PING google.com (142.251.111.101) 56(84) bytes of data.
64 bytes from bk-in-f101.1e100.net (142.251.111.101): icmp_seq=1 ttl=60 time=8.86 ms
$ sudo HOSTALIASES=./h ping my_alias
PING google.com (172.253.62.100) 56(84) bytes of data.
64 bytes from bc-in-f100.1e100.net (172.253.62.100): icmp_seq=1 ttl=111 time=9.09 ms
…
What is going on here?