hostname and hostname -f (Not Getting FQDN)
Luke Hebert
- 2 minutes read - 244 wordsIf hostname -f is returning a short name instead of a fully qualified domain name, the most common culprits are /etc/hosts and /etc/sysconfig/network. In most cases I have seen, the short name is the value of the HOSTNAME variable set in /etc/sysconfig/network on the affected system. You would not expect a short name to come back from an external DNS resolver, so the problem is almost always local configuration.
How the hostname command works under the hood
When you run hostname with no flags, the underlying system call is gethostname. That call simply returns whatever the kernel has stored as the local hostname, with no network lookup involved.
When you add a flag such as -a, -d, -f, or -i, the command switches to gethostbyname. That function is network-aware: it consults /etc/nsswitch.conf and /etc/host.conf to decide whether to resolve the name through /etc/hosts, DNS, or another source.
What to check
/etc/sysconfig/network- Verify thatHOSTNAMEis set to the full FQDN (for example,web01.example.com) rather than just the short name./etc/hosts- Make sure the entry maps the FQDN to the correct IP address and that no conflicting short-name entry is taking precedence./etc/nsswitch.conf- Confirm thehostsline lists the sources in the order you expect (typicallyfiles dns).
After making changes, run hostname -f again. If the FQDN still does not appear, the issue is likely a stale entry in /etc/hosts or a DNS record that does not match the local name.