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
Responses
You could use "strace hostname -d" to determine what happens.
On one of our 6.9 systems:
open("/etc/resolv.conf", O_RDONLY) = 3
read(3, "#\n# template to create /etc/reso"..., 4096) = 357
...
write(1, "my-domain.com\n", 15my-domain.com
On one of our 7.3 systems:
connect(3, {sa_family=AF_INET, sin_port=htons(53), sin_addr=inet_addr("x.x.x.x")}, 16) = 0
recvfrom(3, "x .....
write(1, "my-domain.com\n", 15my-domain.com
Where x.x.x.x is the address of the nameserver.
Hi Jörg,
No reason to be confused, you already have answered the question yourself : "In /etc/hosts is another hostname configured than in /etc/hostname. This was a mistake when the host was renamed in the past." Once everything is configured correctly, in every Linux distribution the terminal outputs are the same - it is just right like this example from my currently running fedora 27 workstation ->
[cl@cl-fw-1 ~]$ hostnamecl-fw-1[cl@cl-fw-1 ~]$ hostname -dfritz.box[cl@cl-fw-1 ~]$ hostname -fcl-fw-1.fritz.box
And here (for comparison) you can see the terminal output (exactly same structure) from my Red Hat Enterprise Linux 7.4 Server ->
[cl@cl-rs-1 ~]$ hostnamecl-rs-1[cl@cl-rs-1 ~]$ hostname -dfritz.box[cl@cl-rs-1 ~]$ hostname -fcl-rs-1.fritz.box
Regards,
Christian
Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.
