How to Set or Change Hostname in Linux

The hostname is the name by which the computer can be identified in the local network, and it is one of the most used components to communicate with a system in a local network.

When a Linux distribution is installed, the hostname is set up at the installation time. Sometimes, users want to change the hostname to serve their specific purpose. To address this importance, this post will demonstrate the possible methods to set up or change the hostname in Linux. The content of this post is as follows:

 Let’s start with the terminal method!

Method 1: To Set or Change the Hostname in Linux Using the Terminal

The first method by which we can change the hostname in the terminal is using the terminal. For this method, we will use the “hostnamectl” command-line utility, which is pre-installed on Linux. Let’s practice a few steps to change the hostname:

Step 1: Get the Current Hostname

First, use the following command to display the current hostname:

$ hostnamectl

In the above method, we can see that the hostname is ubuntu.

Step 2: Change the Hostname

Now, the “hostnamectl” is used to set/change the current hostname to “maddox”:

$ sudo hostnamectl set-hostname maddox

The hostname is changed to “maddox”.

Step 3: Verify the Changed Hostname

To confirm it, again display the current hostname using the command:

$ hostname

The hostname is changed to maddox.

Note; You can also use the “hostnamectl” utility to verify the hostname.

Method 2: To Set or Change the Hostname in Linux Using the GUI

This is a convenient method if the Linux distribution supports the desktop environment. This method is performed on the GUI of Ubuntu 22.04. Let’s see how the hostname is changed using GUI:

Open “Settings” of the Linux:

Then, click on “Sharing” in the left menu and go to the “Computer Name”:

Change the name with the new hostname, as we are again changing “maddox” to “ubuntu”, and close the menu:

The hostname has been changed.

That’s all about changing the hostname in Linux.


To set up or change the hostname in Linux, either by running the command “sudo hostnamectl set-hostname [name]” or by changing the computer name in the “Sharing” option of “Settings”. In this blog, the CLI and GUI methods of changing the hostname have been explained in detail for Linux distributions.