Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

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:

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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