This post was originally published on go2linux.org on March 20, 2009. The domain is no longer mine, but I am the original author. I am republishing it here on garron.me with corrections and improvements.
Introduction
The hostname is the name that identifies your machine on a network. On modern Linux systems (Ubuntu 16.04+, Debian 8+, Fedora, CentOS 7+), the right tool to change it is hostnamectl, part of systemd.
The modern way: hostnamectl
View current hostname:
hostnamectl
Output:
Static hostname: oldname
Icon name: computer-server
Chassis: server
Machine ID: ...
Boot ID: ...
Operating System: Ubuntu 24.04 LTS
Kernel: Linux 6.8.0
Architecture: x86-64
Set a new static hostname:
sudo hostnamectl set-hostname newname
The change takes effect immediately — no reboot needed.
Types of hostname
hostnamectl manages three types:
| Type | Description | Command |
|---|---|---|
| static | Stored in /etc/hostname, persists across reboots | hostnamectl set-hostname name |
| transient | Temporary, set by DHCP or mDNS, lost on reboot | hostnamectl set-hostname name --transient |
| pretty | Human-readable, may include spaces and special characters | hostnamectl set-hostname "My Server" --pretty |
For most purposes you only need to change the static hostname.
Update /etc/hosts
After changing the hostname, update /etc/hosts so the system can resolve its own name:
sudo nano /etc/hosts
Find the line with the old hostname and replace it:
127.0.1.1 newname
Without this step, some applications (like sudo) may show a warning about being unable to resolve the hostname.
Verify the change
hostname
hostnamectl status
The traditional method (non-systemd systems)
On older systems without systemd, the hostname is set in two places:
1. Edit /etc/hostname:
sudo nano /etc/hostname
Replace the content with the new name (just the hostname, no domain):
newname
2. Apply without rebooting:
sudo hostname newname
Or on Debian-based systems:
sudo /etc/init.d/hostname.sh start
The change will be permanent after the next reboot even without that last step, since /etc/hostname is read at boot.