Set a Linux Hostname Without Breaking FQDN Lookups
A hostname change looks harmless until logs, prompts and monitoring all disagree about what to call the box. The hostname command sorts out short names, persistence and FQDN lookups properly.
The route
Jump straight to the step you need, or tick off Done means at the end.
This guide sets the short hostname, explains what survives a reboot, and checks whether the fully qualified domain name (FQDN) actually resolves. It also separates DNS naming from the older NIS/YP domain commands, which share the same program and are easy to confuse.
- Time: about 10 minutes.
- Access: a shell account for inspection;
sudoor root only for changing the running hostname or system configuration. - Versions used: the locally installed
hostname3.23, systemd 255 and man-pages 6.7.
1. Record the current state
Start with read-only commands. Save the output if this is a remote machine: a hostname change can make logs, prompts and monitoring labels look different even though it does not rename the machine's files or network interfaces.
hostname
hostname --short
hostname --all-fqdns
hostname --all-ip-addresses
- Plain
hostnameprints the kernel hostname returned bygethostname(2). --shortcuts at the first dot.- FQDN and address commands depend on resolver configuration and may print nothing, several lines, or names in an order you must not rely on. A reverse DNS failure here is not evidence that the short hostname is wrong.
Check the installed implementation and the static setting too:
hostname --version
sudo cat /etc/hostname
On a system using systemd, /etc/hostname normally contains one newline-terminated static hostname, with comments ignored. Systemd gives a valid kernel-command-line hostname priority during boot, then the static hostname, then a transient hostname such as one supplied by DHCP, and finally falls back to localhost. A configured static hostname therefore usually beats a transient network-provided one.
2. Choose a valid short name
Use one label unless you have a specific reason not to. The systemd hostname file describes a name of up to 64 lower-case ASCII letters, digits or hyphens, forming a valid DNS label, with no dots recommended.
- Pick something stable.
edge01,build-02ordb-westall work. - No spaces, underscores or shell punctuation. They will bite you later.
- Do not fake an FQDN by shoving a domain into the short name. An FQDN comes from name resolution against the current hostname; it is not a second value
hostnamesets directly.
3. Test a temporary change
Changing the running hostname needs elevated privileges and lasts only until the next reboot unless you also change the boot configuration. Warn anyone using the machine first, especially if prompts, audit records or service discovery use the hostname.
sudo hostname edge01
hostname
hostname --short
Both verification commands should now print edge01. If the command reports "you must be root", keep the old hostname and get the required privilege rather than working around the restriction.
This change is reversible. Substitute the value you recorded in step 1:
sudo hostname OLD_SHORT_NAME
Replace OLD_SHORT_NAME with the actual value, not the literal placeholder. A temporary change does not rewrite /etc/hostname, so a reboot normally restores the configured name.
4. Make the name persistent
Back up the static file, then edit it as root. This is a service-affecting configuration change: keep the backup until the next boot has been checked.
sudo cp -p /etc/hostname /etc/hostname.before-edge01
sudoedit /etc/hostname
Leave one name and a final newline, for example:
edge01
Do not replace the file with an FQDN unless you have checked how the software on this host consumes it. The local systemd documentation recommends a single label.
Recovery
If the edit was wrong, restore the backup before rebooting:
sudo cp -p /etc/hostname.before-edge01 /etc/hostname
To apply the chosen value to the current boot as well, run the privileged hostname command from step 3. The file is read during boot by the system's initialisation, but editing it alone does not guarantee that every already-running service notices the change.
5. Make local FQDN resolution deliberate
If an application needs edge01.example.org, configure name resolution rather than trying to set that string with hostname. The /etc/hosts format is one IP address, a canonical hostname, then optional aliases. A common IPv4 arrangement for a local machine:
127.0.0.1 localhost
127.0.1.1 edge01.example.org edge01
Only add an address that really belongs to this host, and preserve existing IPv6 and localhost entries. Back up the file before editing:
sudo cp -p /etc/hosts /etc/hosts.before-edge01
sudoedit /etc/hosts
getent hosts edge01.example.org
hostname --fqdn
Checkpoint
The getent hosts lookup should return the configured address and name once the resolver consults /etc/hosts. The FQDN command can still differ when DNS, NIS, resolver ordering or multiple interfaces are involved.
The installed manpage specifically warns against treating hostname --fqdn, hostname --domain, dnsdomainname and hostname --ip-address as reliable answers in multi-address or mobile environments. Prefer hostname --all-fqdns and hostname --all-ip-addresses when you need the complete set, and do not assume their output order.
To undo only this local mapping, restore the backup after checking that it contains the previous, intact file:
sudo cp -p /etc/hosts.before-edge01 /etc/hosts
Common traps: DNS and NIS names are not the same thing
dnsdomainnamedisplays the domain portion obtained from the FQDN.domainname,ypdomainnameandnisdomainnamedisplay the NIS/YP domain instead, and are not interchangeable with a DNS query. Setting an NIS/YP domain also needs root and only lasts until reboot; do not run a setting form unless this host genuinely uses NIS or YP.- The aliases point at the same hostname utility, but their no-argument behaviour differs. A non-empty result from
domainnamedoes not prove DNS is configured, and an emptydnsdomainnamedoes not mean the short hostname is unset.
Done means
hostnameprints the chosen short name./etc/hostnamecontains the intended persistent value, if persistence was required.getent hostsresolves any local FQDN mapping you added.- You have not mistaken an NIS/YP domain for a DNS domain.
- You kept recovery copies until the current boot and dependent services were checked.