Where to start First steps track · 15 stages
Linux Labs

hostnamectl — host name and machine info

4 min Ubuntu 24.04 · systemd

View and change the host name and machine info; /etc/hostname and /etc/hosts.

Cheat sheet

4 commands Printable version
  • hostnamectl name + machine info
  • hostname -f FQDN
  • hostname -I all IP addresses
  • sudo hostnamectl set-hostname web01
  • + /etc/hosts: 127.0.1.1 web01.example.com web01
  • VM clone: truncate -s0 /etc/machine-id && systemd-machine-id-setup

Overview

hostnamectl (systemd) shows and changes the host name and machine info (OS, kernel, virtualization, machine-id, architecture).

bash
hostnamectl                       # name, OS, kernel, virtualization, machine-id
hostname                          # just the current name (short command)
hostname -f                       # FQDN (full name via DNS/hosts)
hostname -I                       # all IP addresses of the host

Options

6

Without a subcommand, hostnamectl prints the static facts: name, OS, kernel, virtualization, machine-id. With set-hostname it changes the name — by default it sets static and pretty together unless you name --static/--transient/--pretty explicitly. -H runs the operation on a remote machine over SSH, without an interactive login to it.

Option What it does Example
--static Show only the static name (from /etc/hostname). hostnamectl --static
--transient Show or set the transient name (from DHCP/mDNS, until reboot). hostnamectl set-hostname web01 --transient
--pretty Show or set the pretty name for the UI — spaces and UTF-8 allowed. hostnamectl set-hostname "Веб 01" --pretty
--json short|pretty|off Print the machine info as JSON. hostnamectl --json=pretty
-H --host host Perform the operation on a remote machine over SSH. hostnamectl -H user@host
--no-ask-password Do not prompt for a password interactively (for scripts). hostnamectl --no-ask-password set-hostname web01

Nothing found. Try a shorter query.

Recipes

1

Three kinds of name

systemd distinguishes three host names:

Kind Where Command
static /etc/hostname, survives reboot set-hostname web01
transient temporary (from DHCP/mDNS), until reboot set-hostname --transient
pretty "nice" name for the UI (UTF-8, spaces) set-hostname "Web 01" --pretty
bash
sudo hostnamectl set-hostname web01            # static (writes /etc/hostname)
sudo hostnamectl set-hostname "Web 01" --pretty
hostnamectl --static                            # show one specific kind
sudo hostnamectl set-location "Rack 5, DC1"     # extra metadata
sudo hostnamectl set-chassis vm                 # type (vm/server/container)

After changing the name, update /etc/hosts so sudo and local lookups don't stall on resolution:

127.0.1.1   web01.example.com web01

Troubleshooting

4

sudo hangs for a moment after changing the name

The new name doesn't resolve locally. Add it to /etc/hosts (a line 127.0.1.1 name). Check with getent hosts $(hostname) (see dns-resolv).

The name "reverted" after a reboot

cloud-init or DHCP is resetting it. Disable it in cloud-init (preserve_hostname: true in /etc/cloud/cloud.cfg) or at the DHCP source.

hostname -f doesn't return an FQDN

There's no domain-qualified record in /etc/hosts/DNS. Add 127.0.1.1 host.domain host — order matters (FQDN first).

Same machine-id on cloned VMs

When cloning an image, machine-id must be unique, or DHCP leases and journals get confused. Reset it:

bash
sudo truncate -s0 /etc/machine-id && sudo systemd-machine-id-setup
sudo rm -f /var/lib/dbus/machine-id && sudo dbus-uuidgen --ensure

See also

  • systemd — The init system and service manager (PID 1): units, targets, dependencies, logs.
  • dns-resolv — How a name becomes an IP — resolv.conf, systemd-resolved, /etc/hosts, dig.
  • hostname — Reading the name, FQDN and addresses with the hostname utility.
Loading…