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 host.example.com 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:

hostnamectl

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 host.example.com --staticsudo hostnamectl set-hostname host.example.com --transient

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

sudo hostnamectl set-hostname neptune.linuxize.com

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 127.0.0.1 in /etc/hosts. Open the file and change the old host name to the new one.

/ etc / hosts
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’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:

/etc/cloud/cloud.cfg
# 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:

hostnamectl

Your new host name 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

diploma

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.