Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

hostnamectl Command Guide for Managing the System Hostname

hostnamectl shows and changes the system hostname, and reports machine and kernel details — the modern replacement for editing /etc/hostname by hand.


What is hostnamectl, and Why Use It?

hostnamectl is part of systemd and talks to the hostname service directly. It shows your machine’s name, the kernel you are running, the architecture, and the OS, and it lets you change the hostname safely — updating the files, the running kernel state, and the network identity in one step instead of the old scattered approach of editing two files and hoping.

Why Learn hostnamectl?

  • It is the supported way — editing /etc/hostname by hand on a modern system is a workaround; hostnamectl does the whole job.
  • It is one command, not three — the old method needed hostname, a file edit, and a reboot.
  • It sets all three hostname kinds — static (the boot name), transient (what the system currently advertises), and pretty (human-readable).
  • It reports everything at once — kernel, architecture, OS, and virtualization in one screen.
  • It is scriptable — hostnamectl set-hostname new-name is idempotent and safe to run repeatedly.

When Should You Use hostnamectl?

Use it when you set up a new machine, rename a server, or need to report what kernel and OS you are running. Reach for uname -a instead if you only want a one-line kernel string for a bug report — hostnamectl is for changing names and seeing the whole picture.


1. See the Whole Picture

The bare command prints everything it knows:

hostnamectl
 Static hostname: web-01
       Icon name: computer-vm
         Chassis: vm
      Machine ID: 4f2a1c8e9b3d47a1b0c5d6e7f8a9b0c1
         Boot ID: 7c8d9e0f1a2b3c4d5e6f7a8b9c0d1e2f
  Virtualization: kvm
Operating System: Ubuntu 24.04.1 LTS
          Kernel: Linux 6.8.0-45-generic
    Architecture: x86-64

What the fields mean

FieldMeaning
Static hostnameThe name set at boot — the one that sticks
Transient hostnameThe name currently advertised on the network
Pretty hostnameA human-readable name, spaces allowed
Machine IDUnique ID for this install, stable across reboots
Boot IDUnique per boot — changes every restart
Virtualizationkvm, docker, vmware, or none

A container’s Virtualization field is how you spot you are inside Docker without checking anything else.


2. Change the Hostname

sudo hostnamectl set-hostname web-02

That updates the static hostname, the running kernel hostname, and — on the next login prompt — your shell prompt. Verify:

hostnamectl
 Static hostname: web-02

The three hostname kinds

KindSet withNotes
Staticset-hostnameThe boot-time name; the default
Transientset-hostname with --transientWhat DHCP or mDNS advertise
Prettyset-hostname with --prettySpaces and capitals allowed

Example of each:

sudo hostnamectl set-hostname web-02                        # static: web-02
sudo hostnamectl set-hostname --transient web-02.local      # transient
sudo hostnamectl set-hostname --pretty "Web Server 02"      # pretty

A pretty hostname with spaces is displayed by some login managers but cannot be used by most network tools. Set all three only if you have a reason to.


3. Change Only One Kind

If you want the pretty name to differ from the machine name without affecting networking:

sudo hostnamectl set-hostname --pretty "Production Web 02"

Check that the static name did not move:

hostnamectl status

Only the fields that changed are shown:

 Static hostname: web-02
       Pretty hostname: Production Web 02

Useful variants

hostnamectl status                    # short summary, no Machine/Boot IDs
hostnamectl --json=pretty status      # machine-readable output
hostnamectl set-hostname --static ""  # clear the static hostname (rare)

4. Read It in a Script

Because the output is predictable, you can pull single values out:

#!/bin/bash
# Report kernel and OS in one line
kernel=$(hostnamectl | grep '^ *Kernel:' | sed 's/^ *Kernel: //')
os=$(hostnamectl | grep '^Operating System:' | cut -d: -f2- | xargs)
echo "$os on kernel $kernel"
Ubuntu 24.04.1 LTS on kernel Linux 6.8.0-45-generic

A cleaner JSON version

hostnamectl --json=pretty status | grep -E '"hostname"|"Kernel"'
        "hostname": "web-02",
      "Kernel": "Linux 6.8.0-45-generic",

JSON mode is the right choice when a script needs to parse this — grep and sed on the human-readable output break when systemd changes the layout.


5. Hostname Rules

The kernel is strict about what counts as a valid hostname. hostnamectl will reject most things you try:

RuleDetail
Length1–64 characters
Allowed charactersletters, digits, -, . and _
Not allowedspaces, most punctuation, leading or trailing - or .
Casepreserved, but comparisons are case-insensitive
DNSonly letters, digits, and - are safe for DNS names

Example of a rejection:

sudo hostnamectl set-hostname "web 02"
# Failed to set hostname: Invalid argument

And the corrected version:

sudo hostnamectl set-hostname web-02

Keep hostnames to letters, digits, and hyphens. That is the subset that works everywhere: the shell prompt, DNS, TLS certificates, and every tool that assumes a normal name.


Common Pitfalls

Forgetting sudo. Changing the hostname is a privileged operation. Without it you get Failed to set hostname: Access denied.

hostnamectl set-hostname web-02
# Failed to set hostname: Access denied
sudo hostnamectl set-hostname web-02   # works

Expecting the prompt to change immediately. Your current shell keeps the old prompt until you start a new one. Run exec bash, open a new terminal, or log out and back in.

Renaming a machine that is already in service. If DNS records, SSH keys, monitoring, or /etc/hosts reference the old name, changing the hostname breaks them. Update the dependents first, then rename.

# Check what references the old name before renaming
grep -r old-name /etc/hosts /etc/hostname 2>/dev/null

Assuming the pretty hostname is used by tools. Most software reads the static or transient name. A pretty hostname is cosmetic.

Confusing hostnamectl with uname -n. They can differ. uname -n reports the current (transient) name; hostnamectl reports all three. When they disagree, trust hostnamectl.


PageRelationship
unamePrints kernel and system info; hostnamectl adds hostname management
uptimeThe other quick pulse check on a machine you just renamed
freeMemory check — useful on the same first-look pass
dmesgKernel messages, including hostname-related network events
networking READMEHostnames matter for DNS, SSH, and every network tool

hostnamectl Cheatsheet

CommandDoes
hostnamectlShow hostname, OS, kernel, and virtualization
hostnamectl statusShort summary, no IDs
sudo hostnamectl set-hostname NAMESet the static hostname
sudo hostnamectl set-hostname --transient NAMESet the transient hostname
sudo hostnamectl set-hostname --pretty "Name"Set the human-readable name
hostnamectl --json=pretty statusMachine-readable output
hostnamectl --versionShow the version

Set the name once, correctly, and every tool downstream gets it right.