How to Change Hostname on Ubuntu 20.04

Change Hostname on Ubuntu 20.04

In this article, we will have explained the necessary steps to change the Hostname on Ubuntu 20.04 LTS. Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges. All the commands in this tutorial should be run as a non-root user.

A hostname is a name assigned to a “host” i.e. a computer on a network. The hostname is basically just your computer’s name. It’s used to identify your computer on the network. Please note that the hostname set at the time of installation either by sysadmin or cloud service provider such as Linode, Ramnode, and many others. To change the Ubuntu computer name, you must log in as the root user.

Change Hostname on Ubuntu

Step 1. First, before you start installing any package on your Ubuntu server, we always recommend making sure that all system packages are updated.

sudo apt update
sudo apt upgrade

Step 2. Display Current Hostname.

Ubuntu Linux 20.04 LTS server or desktop can simply use the hostnamectl command to change the hostname. To see the current setting just type the following command:

hostnamectl

That should display something similar to the lines below:

Static hostname: ubuntuq-2004
         Icon name: computer-vm
           Chassis: vm
        Machine ID: e280aedec6a247jembeb4f85576bb
           Boot ID: b794a939b6264a5ea7bet18eae9c130d7
    Virtualization: oracle
  Operating System: Ubuntu 20.04 LTS
            Kernel: Linux 5.8.0-26-generic
      Architecture: x86-64

Step 3. Change Hostname.

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

sudo hostnamectl set-hostname host.linuxtips.us
sudo hostnamectl set-hostname "Your Pretty HostName" --pretty
sudo hostnamectl set-hostname host.linuxtips.us --static
sudo hostnamectl set-hostname host.linuxtips.us --transient

For example, to change the system static hostname to meilana.linuxtips.us, you would use the following command:

sudo hostnamectl set-hostname meilana.linuxtips.us

To change your hostname permanently, you’ll also need to edit your /etc/hostname and /etc/hosts file, which is where Ubuntu distributions store the hostname:

127.0.0.1   localhost
127.0.0.1   meilana.linuxtips.us

# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
ff04::1 ip6-allnodes
ff04::2 ip6-allrouters

If the cloud-init package is installed you also need to edit the cloud.cfg file. This package is usually installed by default in the images provided by the cloud providers such as AWS and it is used to handle the initialization of the cloud instances:

sudo nano /etc/cloud/cloud.cfg

Search for preserve_hostname and change the value from false to true:

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

Now, for the changes to take effect, reboot your computer with the following command:

sudo reboot

Once your computer boots, run the following command to verify if the hostname of computer A has changed:

$ hostnamectl
   Static hostname: meilana.linuxtips.us
   Pretty hostname: sesion 15'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.8.0-26-generic
      Architecture: x86-64

That’s all you need to do to change the Hostname on Ubuntu 20.04 Focal Fossa. I hope you find this quick tip helpful. If you have questions or suggestions, feel free to leave a comment below.