Where does FQDN come from when not set explicitly

hostname

When there is no FQDN explicitly specified on an Ubuntu server:

$ cat /etc/hostname
banana

$ cat /etc/hosts
127.0.0.1 localhost banana

Then where does mydomain.com come from?

$ hostname -A
banana.mydomain.com

Does this have anything to do with DNS records? (They seem to have been set to Google DNS on my virtual host.)

$ cat /etc/resolv.conf
nameserver 8.8.8.8
nameserver 8.8.4.4

DNS records say as follows:

$ nslookup myip
myip.in-addr.arpa name = banana.mydomain.com

Does this mean that hostname -A falls back to DNS records?

I have dozens of hostnames associated with the server IP address through DNS. How do I decide which of the many hostnames is used as the "official" or "primary" hostname?

Best Answer

The FQDN is something you don't set on the machine itself, it's the DNS resolvable name of your machine. To be precise, it is the host name returned by the getaddrinfo system call.

From the hostname manpage:

The FQDN of the system is the name that the resolver(3) returns for the host name.

The resolver is a set of functions, that query the hostname over domain name systems, according to configurations made in the files /etc/resolv.conf and /etc/nsswitch.conf or /etc/host.conf.

The former defines name servers to query for DNS lookups (in your case the Google DNS servers). The latter defines the order in which name queries should be performed. It depends on the resolver library version which of the latter two is used.

Related Question