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.
sudo or root only for changing the running hostname or system configuration.hostname 3.23, systemd 255 and man-pages 6.7.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
hostname prints the kernel hostname returned by gethostname(2).--short cuts at the first dot.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.
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.
edge01, build-02 or db-west all work.hostname sets directly.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.
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.
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
dnsdomainname displays the domain portion obtained from the FQDN.domainname, ypdomainname and nisdomainname display 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.domainname does not prove DNS is configured, and an empty dnsdomainname does not mean the short hostname is unset.hostname prints the chosen short name./etc/hostname contains the intended persistent value, if persistence was required.getent hosts resolves any local FQDN mapping you added.