🎁 New User? Get 20% off your first purchase with code NEWUSER20 Register Now →
Menu

Categories

hostnamectl Command

Beginner Systemd & Services man(1)

Control system hostname

👁 9 views 📅 Updated: Mar 15, 2026
SYNTAX
hostnamectl [COMMAND]

What Does hostnamectl Do?

hostnamectl manages the system hostname. It can set the static hostname (persistent), the pretty hostname (UTF-8 descriptive name), and the transient hostname (temporary).

hostnamectl is the systemd way to manage hostnames, replacing manual editing of /etc/hostname. Changes are permanent and take effect immediately.

hostnamectl also shows useful system information including the machine ID, boot ID, operating system, kernel, and architecture.

Options & Flags

OptionDescriptionExample
status Show hostname and system info hostnamectl status
set-hostname Set the hostname sudo hostnamectl set-hostname webserver01
--static Show/set static hostname hostnamectl --static
--pretty Show/set pretty hostname sudo hostnamectl set-hostname "Web Server 01" --pretty
--transient Show/set transient hostname hostnamectl --transient

Practical Examples

#1 Show system info

Shows hostname, OS, kernel, and architecture.
$ hostnamectl
Output: Static hostname: webserver01 Operating System: Ubuntu 22.04 Kernel: Linux 5.15.0 Architecture: x86-64

#2 Set hostname

Permanently changes the hostname.
$ sudo hostnamectl set-hostname production-web-01

#3 Set pretty hostname

Sets a descriptive name (can include spaces and special characters).
$ sudo hostnamectl set-hostname "Production Web Server 01" --pretty

#4 Update /etc/hosts

Changes hostname and updates /etc/hosts to match.
$ sudo hostnamectl set-hostname newname && sudo sed -i "s/oldname/newname/g" /etc/hosts

Tips & Best Practices

Update /etc/hosts too: After changing hostname, update /etc/hosts to map the new name to 127.0.1.1. This prevents DNS resolution issues.
Three hostname types: Static: persistent across reboots. Pretty: descriptive (UTF-8). Transient: temporary (from DHCP).
No spaces in static hostname: Static hostname must be a valid DNS label: lowercase, no spaces, hyphens allowed. Use --pretty for descriptive names.

Frequently Asked Questions

How do I change the hostname permanently?
sudo hostnamectl set-hostname newname. Then update /etc/hosts to match.
How do I check the hostname?
hostnamectl shows the hostname along with OS and kernel information.
What is a pretty hostname?
A human-readable name that can include spaces and UTF-8 characters: "Production Database Server". Used for display purposes.

Master Linux with Professional eBooks

Curated IT eBooks covering Linux, DevOps, Cloud, and more

Browse Books →