Need help to understand the resolution of Hostname and FQDN
Hello @all,
Today I could use some help to understand how the resolution of the Hostname and the FQDN on RHEL works.
Here is what I discovered this morning on a misconfigured host:
$ hostname -d
$hostname --fqdn
my-foo
But my-foo is not the FQDN. Instead I suspected my-foo.example.com to be the FQDN of this host. So I looked up some files which come to mind that could have to do something with this strange behaviour:
# cat /etc/hostname
my-foo
# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.1 foo.example.com foo
In /etc/hosts is another hostname configured then in /etc/hostname. This was a mistake when the host was renamed in the past. So I changed the wrong entry in /etc/hosts so it looks like:
# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.1 my-foo.example.com my-foo
And now I get the results I expect:
# hostname -f
my-foo.example.com
# hostname -d
example.com
# hostname
my-foo
From the hostname manpage I know that different syscalls are being used depending on the options chosed:
The function gethostname(2) is used to get the hostname. When the hostname -a, -d, -f or -i is called will gethostbyname(3) be called. The difference in gethostname(2) and gethostbyname(3) is that gethostbyname(3) is network aware, so it consults /etc/nsswitch.conf and /etc/host.conf to decide whether to read information in /etc/hostname or /etc/hosts
While using hostname -d or -f
the information is read from /etc/hosts. But how the information gets processed? Does gethostbyname(3) a DNS request with the names found in /etc/hosts to determine the hosts FQDN? Or does some other mechanism do the trick?
Please, enlighten me because I'm confused.
Best regards,
Joerg