Introduction

A hostname is a user-generated custom name which identifies a computer system in a network. In Ubuntu 20.04, users assign a hostname to the machine during OS setup.

You may decide to change your computer’s hostname for several reasons. For example, no two computers on a network can have the same hostname and, if you find yourself in this situation, you will have to change the hostname to avoid a network conflict.

In this tutorial, you will learn how to change a hostname on Ubuntu 20.04 using the Linux command line or GUI.

How to Change Hostname in Ubuntu 20.04

Prerequisites

How to Check Current Hostname on Ubuntu 20.04

To check the current hostname of your Ubuntu system, use one of two available commands.

  • The hostname command displays only the hostname itself.

Checking hostname using the hostname command

  • The other command, hostnamectl, displays additional information about your computer system.

Checking hostname using the hostnamectl command

The Static hostname line displays your machine’s hostname.


Note: Valid hostnames are between 2 and 64 characters in length. They can contain only letters, numbers, periods, and hyphens, but must begin and end with letters and numbers only.


Temporarily Change Hostname on Ubuntu 20.04

Use the hostname command to make a temporary change to your computer’s hostname.

In the terminal, type the following, replacing new-hostname with the name you choose:

sudo hostname new-hostname

If successful, this step will not provide any output. To confirm the result of the process, check the current system hostname:

Changing hostname using hostname command

Change Hostname on Ubuntu 20.04 (No Reboot Required)

If you wish to permanently change the hostname without rebooting your computer, use the hostnamectl command.

Step 1: Use set-hostname to Change the Hostname

Type the following command:

hostnamectl set-hostname new-hostname

Use your own hostname choice instead of new-hostname.

Step 2: Use hostnamectl to Confirm the Change

Just like the hostname command, if successful, hostnamectl set-hostname does not produce any output. Therefore, use hostnamectl to check the result.

Changing hostname using hostnamectl set-hostname command

Step 3: Change the Pretty Hostname (Optional)

A “pretty” hostname is the hostname presented to the user, not to another computer on a network. A computer system identifies another computer only by its static hostname.


Note: The pretty hostname does not have the naming limitations of its static counterpart – any UTF-8 value is permitted.


To change a machine’s “pretty” hostname, use the same hostnamectl command with the --pretty option:

hostnamectl set-hostname "new-hostname" --pretty

Replace new-hostname with your own UTF-8 value.

After checking the result with hostnamectl, you will notice an additional line in the output, listing the computer’s “pretty” hostname.

Changing the pretty hostname using hostnamectl command with --pretty option


Note: The "pretty" hostname is stored in /etc/machine-info. Updating this file is another way to perform this optional step.


Change Hostname on Ubuntu 20.04 - Alternative Method (Reboot Required)

Another way to permanently change the hostname is by editing two configuration files:

      • /etc/hostname
      • /etc/hosts

The changes take effect immediately after system reboot.

Step 1: Open /etc/hostname and Change the Hostname

Edit the file with a text editor of your choice. In this example, we will be using the Vim editor:

sudo vi /etc/hostname

The /etc/hostname file contains only the current hostname. Replace it with your new choice.

Save the file and exit.

Editing the /etc/hostname file

Step 2: Open /etc/hosts and Change the Hostname

Now edit the /etc/hosts file in the same way.

sudo vi /etc/hosts

The file /etc/hosts maps hostnames to IP addresses. Look for the hostname you wish to change and simply replace it with your new choice.

Save the edits and exit.

Editing the /etc/hosts file

Step 3: Reboot the System

Reboot your computer to apply the changes:

sudo systemctl reboot

Note: If you use the cloud-init package to run a cloud instance of Ubuntu, you need to perform another step before rebooting. Go to /etc/cloud/cloud.cfg file and make sure the line preserve_hostname is set to TRUE.


Change Hostname on Ubuntu 20.04 Using GUI

The Ubuntu GUI also provides a way to edit the system hostname:

1. First, navigate to Settings > About.

Navigating to the About section in Ubuntu 20.04 settings

2. Now, locate the Device Name field.

Locating the Device Name field in the About section of Ubuntu 20.04 settings

3 Clicking the Device Name field opens the Rename Device dialogue box.

4. In the Rename Device dialogue box, replace the current hostname with a new one and confirm your choice by clicking the Rename button. This action permanently changes the hostname.

Changing the Device Name in the About section of Ubuntu 20.04 settings

Conclusion

This article presented three methods in which you can change the hostname on your Ubuntu 20.04 machine, using the command line or GUI. All the methods are simple and straightforward, so you can choose whichever one fits your use case best.


Next you should also read