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.

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.

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

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.

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

Done means