How to Change Hostname on Ubuntu 20.04
3 min read
The hostname is set during the installation of the operating system or dynamically assigned to the virtual machine when it is created.
This guide explains how to set or change the hostname on Ubuntu 20.04 without needing to restart the system.
Understanding Host Names
A hostname is a label that identifies a device on a network. You shouldn’t have two or more machines with the same hostname, on the same network.
In Ubuntu, you can edit the system hostname and related settings using the
hostnamectl command. This tool recognizes three different classes of hostname:
static- The traditional hostname. It is stored in the
/etc/hostnamefile and can be set by the user.
pretty- A descriptive free-form UTF8 hostname used for presentation to the user. For example,
transient- A dynamic hostname that is maintained by the kernel. DHCP or mDNS servers can change the transient hostname at run time. By default, it is the same as the
It is recommended to use a fully-qualified domain name (
) such as
host.example.com for both
Only root or users with sudo privileges can change the system hostname.
Displaying the Current Hostname
To view the current hostname, invoke the
hostnamectl command without any argument:
In this example, the current hostname is set to
Changing the System Hostname
Changing the system hostname is a simple process. The syntax is as follows:
sudo hostnamectl set-hostname host.example.com
sudo hostnamectl set-hostname "Your Pretty HostName" --pretty
sudo hostnamectl set-hostname host.example.com --static
sudo hostnamectl set-hostname host.example.com --transient
For example, to change the system static hostname to
neptune.linuxize.com, you would use the following command:
sudo hostnamectl set-hostname neptune.linuxize.com
Optionally you can also set the pretty hostname:
sudo hostnamectl set-hostname "Linuxize's laptop" --pretty
hostnamectl does not produce output. On success, 0 is returned, a non-zero failure code otherwise.
The static hostname is stored in
/etc/hostname, and the pretty hostname is stored in
You shouldn’t use the same hostname on two different machines on the same network.
On most systems, the hostname is mapped to
/etc/hosts. Open the file and change the old hostname to the new one.
127.0.0.1 localhost 127.0.0.1 neptune.linuxize.com # The following lines are desirable for IPv6 capable hosts ::1 localhost ip6-localhost ip6-loopback ff02::1 ip6-allnodes ff02::2 ip6-allrouters
If you are running Ubuntu on a cloud instance and the
cloud-init package is installed, you also need to edit the
/etc/cloud/cloud.cfg file. This package is usually installed by default in the images provided by the cloud providers, and it is used to handle the initialization of the cloud instances.
If the file exists on your system open it:
sudo vim /etc/cloud/cloud.cfg
preserve_hostname, and change the value from
# This will cause the set+update hostname module to not operate (if true) preserve_hostname: true
Save the file and close your editor.
Verify the change
To verify the hostname has been fully changed, enter the
Your new hostname will be printed on the terminal:
Static hostname: neptune.linuxize.com Pretty hostname: Linuxize's desktop Icon name: computer-vm Chassis: vm Machine ID: a04e3543f3da460294926b7c41e87a0d Boot ID: aa31b274703440dfb622ef2bd84c52cb Virtualization: oracle Operating System: Ubuntu 20.04 LTS Kernel: Linux 5.4.0-26-generic Architecture: x86-64
We have shown you how to easily change the hostname on Ubuntu 20.04 installation without restarting the machine.
There are a number of reasons why you may need to change the hostname. The most common is when the hostname is automatically set upon the instance creation.
Feel free to leave a comment if you have any questions.