[root@GFVM4 ~]# hostname
GFVM4
[root@GFVM4 ~]#
[root@GFVM4 ~]#
[root@GFVM4 ~]# getent hosts gfvm4
192.168.122.151 GFVM4
[root@GFVM4 ~]# getent hosts GFVM4
fe80::5054:ff:feac:787 GFVM4
[root@GFVM4 ~]#
[root@GFVM4 ~]#
[root@GFVM4 ~]#
[root@GFVM4 ~]# ifconfig ens5
ens5: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.122.151 netmask 255.255.255.0 broadcast 192.168.122.255
inet6 fe80::5054:ff:feac:787 prefixlen 64 scopeid 0x20<link>
ether 52:54:00:ac:07:87 txqueuelen 1000 (Ethernet)
RX packets 452 bytes 33008 (32.2 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 204 bytes 26112 (25.5 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@GFVM4 ~]#
Is the above expected behaviour?
As you can see I have setup all caps HOSTNAME GFVM4.
if I use the same name(ALL CAPS) it returns ipv6 address. If I use small lettered hostname, it returns ipv4 address.
Is this correct behaviour?
Running Fedora21 as a qemu based VM.
Thanks
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/nsswitch.conf
,/etc/resolv.conf
,/etc/hosts
, etc. I have a Fedora 22 and it has the following line in/etc/nsswitch.conf
.getent hosts localhost
andgetent hosts LOCALHOST
give different results. But after I changed/etc/nsswitch.conf
to havehosts: 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 usesgetaddrinfo()
instead ofgethostbyname2()
. It gives more consistent answers at least in my case. Seeman getent
.Third, I found it interesting to read the source code getent.c and watch the trace by
ltrace getent hosts localhost
. There you can seeinet_pton()
andgethostbyname2()
. You can also trace system calls byltrace -S
and you can see what files are opened, such as/etc/hosts
.Below is the output of
ltrace
. AF_INET6 (10) is tried before AF_INET (2).Finally, my suggestion is either 1) control the source of
getent
in/etc/nsswitch.conf
; or 2) maintain your own database/dictionary.Just a guess but, from http://linuxmanpages.net/manpages/fedora21/man1/getent.1.html:
In http://ftp.netbsd.org/pub/NetBSD/NetBSD-current/src/usr.bin/getent/getent.c "if (argc == 2)" means "% getent hosts" (no key provided), so this is performed:
I'm not great in C, and the only source I found by a quick googling refers to NetBSD, but it seems that if the first inet_pton (http://linuxmanpages.net/manpages/fedora21/man3/inet_pton.3.html) find an entry for IPv6, the second function for IPv4 is skipped. Also, I didn't see any forcing of the case at first look (meaning that case-insentive seems not forced).
That said, you may have 2 entries for GFVM4, one is upper and the other is lower. The upper one has probably an IPv6 address associated.
It would be great if you could provide confirm or not about different entries in different case, in order to confirm (or not). If confirmed, I'd say it's the expected behaviour of the getent software.