Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท System Information

Linux hostnamectl command

Reference for hostnamectl: show the host name, operating system, kernel and machine ID, set static and pretty host names, and read /etc/os-release in scripts.

hostnamectl shows and changes the machine's host name and a few related facts on systems that use systemd. Run on its own, it prints a compact summary: the static host name, the machine ID, the operating system, the kernel, the architecture and (on virtual machines) the virtualization type. It talks to the systemd-hostnamed service, so changes take effect without a reboot.

A machine can have three names: the static name stored in /etc/hostname (what the system calls itself at boot), the transient name handed out by DHCP or mDNS, and a free-form pretty name for people ("Ann's Build Server").

Terminal
hostnamectl
sudo hostnamectl set-hostname web01
hostnamectl --static

Try it

hostnamectl needs systemd, which the sandbox does not run. The examples show the information it reads from ordinary files, which work anywhere.

Bash The same facts from plain files

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Valid host names (a checker)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
hostnamectl [OPTION]... [COMMAND]
Command or option Description
status Show the summary (the default).
set-hostname NAME Set static and transient names. Add --static, --transient or --pretty to set one.
set-hostname "" With --transient or --pretty, clear that name.
set-icon-name NAME Set the icon shown in graphical tools.
set-chassis TYPE desktop, laptop, server, vm, container and others.
set-deployment ENV Environment label such as production or staging.
set-location TEXT Free-form location.
--static, --transient, --pretty Print just one of the names, or choose which to set.
-H [USER@]HOST Operate on a remote machine over SSH.
--json=short|pretty Machine-readable status.

Returns: 0 on success; non-zero if the name is invalid or you lack permission (changes need root or polkit authorization).

Usage notes

  • Host name rules: up to 63 characters per DNS label (Linux itself allows up to 64 bytes in total, so keep names short); letters, digits and hyphens; no underscores or spaces. Use lowercase for portability.
  • Static versus transient: the static name lives in /etc/hostname and is the one that survives reboots. A DHCP-supplied name may override the transient one while static is unset.
  • Update /etc/hosts too. Some programs resolve the machine's own name through /etc/hosts; if you rename the host without updating the 127.0.1.1 line, sudo can print "unable to resolve host" delays.
  • hostname without systemd: hostname NAME changes only the running name, and you edit /etc/hostname yourself. In containers the name is set by the runtime (it usually equals the container ID).
  • Cloud images frequently reset the host name at boot from metadata (cloud-init). Set preserve_hostname: true there to keep your change.
  • OS details come from /etc/os-release, a shell-sourceable file with NAME, VERSION_ID, ID and PRETTY_NAME. Scripts should read that file, not parse hostnamectl output.
  • Machine ID (/etc/machine-id) is unique per installation; do not copy it into cloned images, or the clones will collide.