How to change the hostname on Ubuntu 20.04

s2 0

The host name is set when the operating system is installed or is 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 rebooting the system.

Understanding host names

A host name is a label that identifies a device on a network. You shouldn’t have two or more machines with the same host name on the same network.

In Ubuntu, you can use the hostnamectl Command. This tool recognizes three different classes of hostnames:

  • static – The traditional host name. It is saved in the /etc/hostname File and can be set by the user.
  • pretty – A descriptive free-form UTF8 host name used for presentation to the user. For example, Linuxize's laptop.
  • transient – A dynamic host name managed by the kernel. DHCP or mDNS servers can change the temporary host name at runtime. By default, it’s the same as that static Hostname.

It is recommended to use a fully qualified domain name (FQDN

) such as for both static and transient Names.

Only root or users with sudo privileges can change the system host name.

View the current host name

To view the current host name, go to the hostnamectl Command without argument:


In this example the current host name is set to ubuntu2004.localdomain.

Change the system host name

Changing the system host name is a simple process. The syntax is as follows:

sudo hostnamectl set-hostname host.example.comsudo hostnamectl set-hostname "Your Pretty HostName" --prettysudo hostnamectl set-hostname --staticsudo hostnamectl set-hostname --transient

For example, to change the system’s static host name to, you would use the following command:

sudo hostnamectl set-hostname

Optionally, you can also specify the pretty host name:

sudo hostnamectl set-hostname "Linuxize's laptop" --pretty

hostnamectl does not produce any output. If successful, 0 is returned, otherwise a non-zero error code.

The static host name is stored in /etc/hostname, and the pretty hostname is stored in /etc/machine-info File.

You should not use the same host name on two different machines on the same network.

On most systems, the host name is mapped to in /etc/hosts. Open the file and change the old host name to the new one.

/ etc / hosts   localhost

# 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’re running Ubuntu on a cloud instance and the cloud-init Package is installed, you also need the /etc/cloud/cloud.cfg File. This package is usually installed by default in the images provided by the cloud providers and is used to handle the initialization of the cloud instances.

If the file is present on your system, open it:

sudo vim /etc/cloud/cloud.cfg

Search for preserve_hostnameand change the value of false to true:

# This will cause the set+update hostname module to not operate (if true)
preserve_hostname: true

Save the file and close your editor.

Review the change

To verify that the host name has been completely changed, enter the hostnamectl Command:


Your new host name will be printed on the terminal:

   Static hostname:
   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 showed you how to easily change the hostname when installing Ubuntu 20.04 without restarting your computer.

There are a number of reasons why you might need to change the host name. Most often, the host name is set automatically when the instance is created.

Feel free to leave a comment if you have any questions.