How to Change Hostname on Ubuntu 22.04 or 20.04

Changing the hostname of a Linux system is a relatively simple process that can be done through the terminal. This guide will cover how to change the hostname on Ubuntu 22.04 and 20.04 LTS (Long-Term Support) using the command line. The hostname is the name assigned to a computer or device on a network and is used to identify the device on the network. Changing the hostname can help identify or personalize a specific network device.

Method 1: Change hostname with “hostnamectl.”

The first step in changing the Ubuntu hostname is opening the terminal. You can open the terminal by clicking on the terminal icon in the application launcher or by using the keyboard shortcut CTRL + ALT + T. Once the terminal is open, you will need to run the command sudo hostnamectl set-hostname new_hostname.

sudo hostnamectl set-hostname new-hostname

Replace new_hostname with the desired hostname. This command will change the hostname of the system to the specified hostname.

The example below will change the new_hostname to ubuntu-linux.

sudo hostnamectl set-hostname ubuntu-linux

Next, close your terminal, then you can quickly open up your command line terminal with CTRL + ALT + T to verify the changes worked correctly.

Method 2: Change Hostname with Hostname File

Alternatively, you can change the hostname by editing the /etc/hostname file, replacing the current hostname with the desired one, and then saving the file.

sudo nano /etc/hostname

After making the changes, you will need to reboot the system for the changes to take effect.



In conclusion, changing the hostname on Ubuntu can be done using the hostnamectl set-hostname command or by editing the /etc/hostname file. The hostnamectl set-hostname command is the recommended method, as it is a more efficient and user-friendly way to change the hostname. However, editing the /etc/hostname file is a viable alternative if the command is not available on your system. Remember to reboot the system after making changes to ensure that the new hostname takes effect.

See also
How to Install zlib on Ubuntu 22.04 | 20.04

Leave a Comment

Share to...