Checking and Changing the Hostname
This article explains how to check and change the hostname of Ubuntu 26.04 LTS Server. The hostnamectl command manages it, and it distinguishes a permanent setting from a temporary one.
Reading the Hostname
Running hostnamectl with no arguments prints the hostname together with information about the machine.
hostnamectlkazulog@sv1:~$ hostnamectl
Static hostname: sv1
Icon name: computer-vm
Chassis: vm 🖴
Machine ID: fa7f95751e0a4b7d896ac94fe4e90998
Boot ID: d00a5c4e8c18417081176227877c6282
Virtualization: kvm
Operating System: Ubuntu 26.04 LTS
Kernel: Linux 7.0.0-30-generic
Architecture: x86-64
Hardware Vendor: QEMU
Hardware Model: Standard PC _i440FX + PIIX, 1996_
Hardware Version: pc-i440fx-8.0
Firmware Version: 1.16.3-debian-1.16.3-2
Firmware Date: Tue 2014-04-01
Firmware Age: 12y 5month 1w 3d
kazulog@sv1:~$ hostname
sv1
kazulog@sv1:~$ uname -n
sv1
kazulog@sv1:~$| Field | Meaning |
|---|---|
Static hostname | The permanent hostname, stored in /etc/hostname and kept across reboots |
Transient hostname | A temporary hostname held by the kernel. It reverts to the static one at reboot, and is not shown when the two match |
Pretty hostname | A display name that may contain spaces and punctuation. Shown only when set |
Machine ID / Boot ID | Identifiers for the system and for this boot |
Virtualization | The virtualisation platform |
hostname and uname -n both print the hostname currently in effect (the transient one when it is set).
Changing the Hostname Permanently
sudo hostnamectl set-hostname [NAME]| Field | Value |
|---|---|
| [NAME] | The hostname to set |
kazulog@sv1:~$ sudo hostnamectl set-hostname webserver
kazulog@sv1:~$ hostnamectl | head -3
Static hostname: webserver
Icon name: computer-vm
Chassis: vm 🖴
kazulog@sv1:~$ hostname
webserver
kazulog@sv1:~$ cat /etc/hostname
webserver
kazulog@sv1:~$/etc/hostname is rewritten and the change survives a reboot.
The prompt of the running shell does not change straight away, because it shows the hostname from the time you logged in. Log in again to see the new one.
kazulog@webserver:~$ hostname
webserver
kazulog@webserver:~$ whoami
kazulogThe Relationship with /etc/hosts
hostnamectl set-hostname updates /etc/hostname but leaves /etc/hosts alone. In the example above, /etc/hosts still carries the old name after the change.
kazulog@sv1:~$ grep 127.0 /etc/hosts
127.0.1.1 ubuntu sv1
127.0.0.1 localhost
kazulog@sv1:~$ sudo true
kazulog@sv1:~$ echo $?
0On some systems this makes sudo print sudo: unable to resolve host. On Ubuntu 26.04 the warning normally does not appear, because systemd-resolved answers queries for the machine’s own hostname.
kazulog@webserver:~$ getent hosts webserver; echo exit=$?
fe80::5054:ff:fe6f:31df webserver
exit=0
kazulog@webserver:~$ getent hosts sv1
127.0.1.1 ubuntu sv1
kazulog@webserver:~$ sudo -n true; echo exit=$?
exit=0getent returns the machine’s own link-local address and sudo runs without a warning. Where the warning does appear, edit the 127.0.1.1 line in /etc/hosts to use the new hostname.
Changing the Hostname Temporarily
For a change that disappears at the next reboot, use the hostname command.
sudo hostname [NAME]kazulog@webserver:~$ sudo hostname dbserver
kazulog@webserver:~$ hostname
dbserver
kazulog@webserver:~$ hostnamectl | head -3
Transient hostname: dbserver
Static hostname: webserver
Pretty hostname: Kazulog Web Server
kazulog@webserver:~$ cat /etc/hostname
webserver
kazulog@webserver:~$ uname -n
dbserverhostnamectl now shows a Transient hostname that differs from the Static hostname. /etc/hostname is untouched, so the static name comes back after a reboot.
hostnamectl set-hostname --transient [NAME] can also set a temporary name, but it has no effect once a static hostname is set, and prints a hint saying so.
kazulog@webserver:~$ sudo hostnamectl set-hostname --transient dbserver
Hint: static hostname is already set, so the specified transient hostname will not be used.
kazulog@webserver:~$ hostnamectl | head -2
Static hostname: webserver
Icon name: computer-vm
kazulog@webserver:~$ hostname
webserver
kazulog@webserver:~$ cat /etc/hostname
webserver
kazulog@webserver:~$Use the hostname command when you want a temporary change.
The Display Name (Pretty Hostname)
A separate display name may contain spaces and punctuation that a hostname cannot.
sudo hostnamectl set-hostname --pretty "[NAME]"kazulog@webserver:~$ sudo hostnamectl set-hostname --pretty 'Kazulog Web Server'
kazulog@webserver:~$ hostnamectl | head -3
Static hostname: webserver
Pretty hostname: Kazulog Web Server
Icon name: computer-vm
kazulog@webserver:~$ cat /etc/machine-info
PRETTY_HOSTNAME="Kazulog Web Server"It is stored in /etc/machine-info, and passing an empty string removes it.
After a Reboot
A reboot drops the transient hostname back to the static one. The static and pretty hostnames remain.
kazulog@webserver:~$ hostnamectl | head -3
Static hostname: webserver
Pretty hostname: Kazulog Web Server
Icon name: computer-vm
kazulog@webserver:~$ hostname
webserver
kazulog@webserver:~$ cat /etc/hostname
webserver
kazulog@webserver:~$ grep 127.0 /etc/hosts
127.0.1.1 ubuntu sv1
127.0.0.1 localhostIn clouds and on virtualisation platforms, the hostname can be overwritten at boot. With cloud-init, the hostname is set from its configuration on first boot. Set preserve_hostname to true in /etc/cloud/cloud.cfg to keep your own value.
kazulog@webserver:~$ grep -E 'preserve_hostname|manage_etc_hosts' /etc/cloud/cloud.cfg
preserve_hostname: false
kazulog@webserver:~$This test system has preserve_hostname: false, but the hostname is only applied on the first boot, so the reboot above did not overwrite it. Setting the hostname at build time is covered in Automating the Initial Setup with cloud-init.
Restoring the Hostname
kazulog@webserver:~$ sudo hostnamectl set-hostname sv1
kazulog@webserver:~$ sudo hostnamectl set-hostname --pretty ''
kazulog@webserver:~$ hostnamectl | head -2
Static hostname: sv1
Icon name: computer-vm
kazulog@webserver:~$ cat /etc/hostname
sv1
kazulog@webserver:~$Test Environment and Session Logs
The examples were captured on Ubuntu 26.04 LTS Server running on CML. The session log of each step can be downloaded below.
| Step | Session log |
|---|---|
| The initial state | log |
| Changing the hostname and checking /etc/hosts | log |
| Name resolution and sudo | log |
| The prompt after logging in again | log |
| –transient and the display name | log |
| A temporary change with the hostname command | log |
| The state after a reboot | log |
| The cloud-init settings | log |
| Restoring the hostname | log |
Reference
hostnamectl(1) - systemd documentation
Related articles
- Changing the Hostname on Ubuntu 26.04 LTS Server (hostnamectl)
- Updating Packages on Ubuntu 26.04 LTS Server (apt update / upgrade)
- Setting the Timezone and Time Synchronisation on Ubuntu 26.04 LTS Server
- Creating Users and Granting sudo Privileges on Ubuntu 26.04 LTS Server
- Configuring the SSH Server on Ubuntu 26.04 LTS Server
- Setting Up a telnet Server on Ubuntu 26.04 LTS Server
- Configuring inetd (the Super-Server) on Ubuntu 26.04 LTS Server
- Setting Up a TFTP Server on Ubuntu 26.04 LTS Server (tftpd-hpa)
- FTP Server Setup on Ubuntu 26.04 LTS Server (vsftpd)
- Setting Up a syslog Server on Ubuntu 26.04 LTS Server (rsyslog)
- Managing Services with systemctl and Reading Logs with journalctl on Ubuntu 26.04 LTS Server
- Configuring Automatic Updates on Ubuntu 26.04 LTS Server (unattended-upgrades)
- Automating the Initial Setup of Ubuntu 26.04 LTS Server with cloud-init
- Configuring Kernel Parameters on Ubuntu 26.04 LTS Server (sysctl)
- Configuring the Network on Ubuntu 26.04 LTS Server (Netplan)
- Configuring Name Resolution on Ubuntu 26.04 LTS Server (systemd-resolved)
- Changing the NTP Source on Ubuntu 26.04 LTS Server (chrony)
- Static Routes on Ubuntu 26.04 LTS Server (Netplan)
- How to Install the Latest neovim from the Official Site on Ubuntu 26.04 LTS Server