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/hostnameby hand on a modern system is a workaround;hostnamectldoes 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-nameis 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
| Field | Meaning |
|---|---|
| Static hostname | The name set at boot — the one that sticks |
| Transient hostname | The name currently advertised on the network |
| Pretty hostname | A human-readable name, spaces allowed |
| Machine ID | Unique ID for this install, stable across reboots |
| Boot ID | Unique per boot — changes every restart |
| Virtualization | kvm, docker, vmware, or none |
A container’s
Virtualizationfield 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
| Kind | Set with | Notes |
|---|---|---|
| Static | set-hostname | The boot-time name; the default |
| Transient | set-hostname with --transient | What DHCP or mDNS advertise |
| Pretty | set-hostname with --pretty | Spaces 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:
| Rule | Detail |
|---|---|
| Length | 1–64 characters |
| Allowed characters | letters, digits, -, . and _ |
| Not allowed | spaces, most punctuation, leading or trailing - or . |
| Case | preserved, but comparisons are case-insensitive |
| DNS | only 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.
Related Pages
| Page | Relationship |
|---|---|
| uname | Prints kernel and system info; hostnamectl adds hostname management |
| uptime | The other quick pulse check on a machine you just renamed |
| free | Memory check — useful on the same first-look pass |
| dmesg | Kernel messages, including hostname-related network events |
| networking README | Hostnames matter for DNS, SSH, and every network tool |
hostnamectl Cheatsheet
| Command | Does |
|---|---|
hostnamectl | Show hostname, OS, kernel, and virtualization |
hostnamectl status | Short summary, no IDs |
sudo hostnamectl set-hostname NAME | Set the static hostname |
sudo hostnamectl set-hostname --transient NAME | Set the transient hostname |
sudo hostnamectl set-hostname --pretty "Name" | Set the human-readable name |
hostnamectl --json=pretty status | Machine-readable output |
hostnamectl --version | Show the version |
Set the name once, correctly, and every tool downstream gets it right.