17

Verbatim output from my terminal:

$ getent hosts example.org
2001:500:88:200::10 example.org
$ getent ahosts example.org
192.0.43.10     STREAM 43-10.any.icann.org
192.0.43.10     DGRAM  
192.0.43.10     RAW    

I thought they were both (at the very least ahosts) supposed to print both IPv4 and IPv6 addresses. Why are these printing different address types?

$ uname -srvmpio
Linux 3.2.0-31-generic #50-Ubuntu SMP Fri Sep 7 16:17:36 UTC 2012 i686 i686 i386 GNU/Linux

Edit: Looks like it might be related to this bug report.

l0b0
  • 51,350

1 Answers1

24

Both getent hosts and getent ahosts can potentially print both IPv4 and IPv6 addresses, but they decide which address(es) to print differently (that's the point of having two different commands).

hosts calls gethostbyname2 (the IPv6-supporting variant of gethostbyname). The behavior is hard-coded: look up an IPv6 address, and if none exists, look up an IPv4 address.

ahosts calls getaddrinfo. This function enumerates all protocols for a given name, so it can return both IPv4 and IPv6 addresses in the same call. What it returns is determined by the settings in /etc/gai.conf. Because many applications are still struggling to cope with IPv6, most distributions prefer IPv4 by default and only use IPv6 by explicit request.

Some useful reading on ahosts and gai.conf around Stack Exchange: