Skip to main content
  1. Linux Articles/
  2. Ubuntu 26.04 LTS Server/

Changing the Hostname on Ubuntu 26.04 LTS Server (hostnamectl)

Table of Contents

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.

Command to check the hostname
hostnamectl
Example: checking the hostname
kazulog@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:~$
FieldMeaning
Static hostnameThe permanent hostname, stored in /etc/hostname and kept across reboots
Transient hostnameA temporary hostname held by the kernel. It reverts to the static one at reboot, and is not shown when the two match
Pretty hostnameA display name that may contain spaces and punctuation. Shown only when set
Machine ID / Boot IDIdentifiers for the system and for this boot
VirtualizationThe virtualisation platform

hostname and uname -n both print the hostname currently in effect (the transient one when it is set).

Changing the Hostname Permanently

Command to change the hostname
sudo hostnamectl set-hostname [NAME]
FieldValue
[NAME]The hostname to set
Example: changing the hostname
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.

Example: the prompt after logging in again
kazulog@webserver:~$ hostname
webserver
kazulog@webserver:~$ whoami
kazulog

The 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.

Example: /etc/hosts 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 $?
0

On 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.

Example: checking name resolution
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=0

getent 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.

Command to change the hostname temporarily
sudo hostname [NAME]
Example: changing the hostname temporarily
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
dbserver

hostnamectl 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.

Example: using hostnamectl --transient
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.

Command to set the display name
sudo hostnamectl set-hostname --pretty "[NAME]"
Example: setting the display 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.

Example: the state after a reboot
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 localhost

In 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.

Example: the cloud-init settings
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

Example: 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.

StepSession log
The initial statelog
Changing the hostname and checking /etc/hostslog
Name resolution and sudolog
The prompt after logging in againlog
–transient and the display namelog
A temporary change with the hostname commandlog
The state after a rebootlog
The cloud-init settingslog
Restoring the hostnamelog

Reference

hostnamectl(1) - systemd documentation

Related articles

Ubuntu official pages