Where to start First steps track · 15 stages
Linux Labs

hostname — machine names and resolution

4 min Ubuntu 24.04; hostname 3.23+nmu2ubuntu2

Reads or temporarily sets the host name; selected options report FQDNs and addresses.

Cheat sheet

6 commands Printable version
  • hostname current name
  • hostname -s short name
  • hostname -f resolved FQDN
  • hostname -i name-derived addresses
  • hostname -I interface addresses
  • hostname --version utility version

Overview

Plain hostname prints the current node name. This identifies a machine, not a user, and need not be a fully qualified DNS name. The utility can also change the name, so adding an operand changes the purpose of the invocation.

A fully qualified domain name, or FQDN, depends on system name resolution. The source can be /etc/hosts or DNS according to the resolver configuration. Adding a dotted name does not automatically create a valid DNS record.

Options

14

Option case matters: -i resolves addresses for the host name, whereas -I enumerates interface addresses without relying on resolution of that name. The -I result excludes loopback and IPv6 link-local addresses; order is not guaranteed. Neither option means “public IP”.

-F reads a name from a file and changes state. A plain hostname operand also sets the current node name. For persistent Ubuntu configuration, use hostnamectl and check local name resolution.

Option What it does Example
-a --alias Aliases obtained through name resolution. hostname -a
-f --fqdn The resolved fully qualified host name. hostname -f
-i --ip-address Addresses obtained by resolving the current host name. hostname -i
-I --all-ip-addresses Interface addresses excluding loopback and IPv6 link-local. hostname -I
-s --short The short name before the first dot. hostname -s
-A --all-fqdns Resolved FQDNs for interface addresses, not a single canonical identity. hostname -A
-b --boot Use a default name if none is available; a setting mode. dangerous hostname -b
-d --domain The DNS domain from the resolved FQDN, not the resolver search list. hostname -d
--long Synonym for --fqdn. hostname --long
-F --file file Read a name from a file and set it. dangerous hostname -F ./hostname.txt
-y --yp The NIS/YP domain, not the DNS domain. hostname -y
--nis Synonym for --yp. hostname --nis
-V --version Installed utility version. hostname -V
-h --help Display help and exit. hostname -h

Nothing found. Try a shorter query.

Recipes

3

Identify the server before making changes

Check the node name alongside the user identity. These are independent properties.

bash
hostname
whoami

Check resolution of the current name

getent queries the configured system name sources. A result does not prove that DNS supplied it.

bash
getent hosts "$(hostname)"
hostname -f

Compare resolved and interface addresses

Different name and interface configuration can legitimately produce different results. Inspect ip for details.

bash
hostname -i
hostname -I
ip address

Troubleshooting

3

A short name works but the FQDN does not

Check how the system resolver handles the current name. Do not rename the machine merely to suppress the error.

bash
hostname
getent hosts "$(hostname)"

hostname -I returned several addresses

A machine may have several interfaces and addresses. Do not treat the first result as universally correct; match it to the intended network.

bash
ip address

The old name returned after reboot

A temporary change does not replace persistent configuration. Inspect hostnamectl and /etc/hostname; external provisioning can also change the name.

bash
hostnamectl
cat /etc/hostname

Danger zone

Danger. hostname new-name and hostname -F file change the name when privileges permit. This can disrupt expected resolution and dependent services. The cheat sheet above only reads information. Do not rename a machine to “fix DNS” without diagnosing the cause.

See also

  • hostnamectl — persistent naming with systemd.

  • dns-resolv — name-resolution sources.

  • ip — per-interface addresses.

  • uname — node identity among system information.

Loading…