This will not be a complete answer, just want to share my findings, and it's too long to fit in comments...
First, if you want to reply on
getent you need to have the right config in
/etc/hosts, etc. I have a Fedora 22 and it has the following line in
hosts: files mdns4_minimal [NUTFOUND=return] dns myhostname
getent hosts localhost and
getent hosts LOCALHOST give different results. But after I changed
/etc/nsswitch.conf to have
hosts: files, they give the same result.
I think when you have multiple sources to search, they may treat case differently and give inconsistent results.
Second, you may want to try
getent ahost. It uses
getaddrinfo() instead of
gethostbyname2(). It gives more consistent answers at least in my case. See
Third, I found it interesting to read the source code getent.c and watch the trace by
ltrace getent hosts localhost. There you can see
gethostbyname2(). You can also trace system calls by
ltrace -S and you can see what files are opened, such as
Below is the output of
ltrace. AF_INET6 (10) is tried before AF_INET (2).
[a@localhost ~]$ ltrace getent hosts LOCALHOST
__libc_start_main([ "getent", "hosts", "LOCALHOST" ] <unfinished ...>
mtrace() = <void>
setlocale(LC_ALL, "") = "en_US.UTF-8"
textdomain("libc") = "libc"
argp_parse(0x606440, 3, 0x7ffc1cf8a7c8, 0) = 0
strcmp("hosts", "hosts") = 0
inet_pton(10, 0x7ffc1cf8c67a, 0x7ffc1cf8a680, 0) = 0
inet_pton(2, 0x7ffc1cf8c67a, 0x7ffc1cf8a680, 0x658e2f20) = 0
gethostbyname2(0x7ffc1cf8c67a, 10, 0x7ffc1cf8a680, 0x658e2f20) = 0
gethostbyname2(0x7ffc1cf8c67a, 2, -20, 0x7f166586c8f5) = 0x7f1665b16260
inet_ntop(2, 0xc47000, 0x7ffc1cf8a620, 46) = 0x7ffc1cf8a620
printf("%-15s %s", "127.0.0.1", "localhost.localdomain") = 37
__overflow(0x7f1665b13620, 32, 0, 0x7fffffda) = 32
fputs_unlocked(0xc47041, 0x7f1665b13620, 0x7f1665d36025, 0xfbad2a84) = 1
__overflow(0x7f1665b13620, 10, 0xc47050, 0x74736f686c61636f127.0.0.1 localhost.localdomain localhost
) = 10
+++ exited (status 0) +++
Finally, my suggestion is either 1) control the source of
/etc/nsswitch.conf; or 2) maintain your own database/dictionary.