The hostname of any operating system is the readable string. This hostname is set at the time of installation of the Linux operating system, but there are many different methods availables that you can perform to change this hostname after the installation. Before performing the process of changing hostname one must know about the different tools used for it. Two main designators are computer administrator and network. This article gives the full overview about all possible ways to change the hostname on your Linux/Ubuntu operating system.

How to Change Kernel Hostname?

There are different ways to set the hostname on the Linux/Ubuntu operating system given as.

  • Use “hostnamectl” command to change the hostname
  • Use “nmcli” command to change the hostname
  • Use “sysctl” command to change the hostname
  • Use “sudo hostname” command to change the hostname
  • Use “etc/hosts” command to change the hostname
  • Use “cockpit” command to change the hostname
  • Use “nmtui” command to change the hostname
  • Use the GUI method to change the hostname

Method 1: Use “hostnamectl” Command to Change the Hostname

If simply run the “hostnamectl” command without any extra argument in the Linux/Ubuntu terminal then it gives the system information which includes the “static host” and other information. Now open the Linux terminal and run the below-given command.

hostnamectl

Now to change the hostname by using the “hostnamectl” the command to be executed in the Linux/Ubuntu terminal is given as.

sudo hostnamectl set-hostname --static "name"

In the above command

  • sudo” gives privileges to the root user.
  • set-hostname” changes the “static hostname”. 

The result of the above-written command is shown in the below figure. Before running the above command the home name is “tahir” and when the above command is executed in the terminal the hostname has been changed to “Abbas”. 

Method 2: Use “nmcli” Command to Change the Hostname

There are many different functions performed by the “nmcli” command in the Linux/Ubuntu operating system such as displaying, deleting, editing, and activating the network connection. However, the hostname can also be changed by using this command. The command to change the hostname run in the Linux/Ubuntu terminal is written below.

sudo nmcli general hostname new_name

In the above command “nmcli” with “general hostname” change the hostname. The below given result clearly shows that “Abbas” is the hostname before running the above command after executing the above common the hostname changes to “tahir”.

Method 3: Use “sysctl” Command to Change the Hostname

The transient name links to your operating system can easily change by using the “sysctl” command. This command is used to make the configuration of Linux/Ubuntu parameters. The command run to change the hostname is given as.

sudo sysctl kernel.hostname=new_name

In the above command 

  • kernal.hostname” contains the current hostname.
  • New_name” is the new name given to the host.

The output result for the above command is given in the below figure. This result clearly shows the hostname before and after running the above command. “tahir” is the hostname before executing the command and it changes to “Abbas” after executing the above command.  

Method 4: Use “sudo hostname” Command to Change the Hostname

The most simple way to change the hostname on the Linux/Ubuntu operating system is to use the “sudo” command. The command to be run for this purpose is given as.

sudo hostname new_name

The result of the above-stated command is given in the below figure. When executing the above command the hostname is changed from “tahir” to “Abbas”.   

Method 5: Use “etc/hosts” Command to Change the Hostname

To create a shortcut for the host you are using on your operating system “etc/hosts” command is very useful. This is also used to set the hostname. Firstly this command is run with “cat” to show the hostname. 

Method 6: Use “cockpit” Command to Change the Hostname

The “cockpit” command is used to launch the web console to monitor the operating system as a remote machine. We can also use this remote machine to the hostname. The steps to use this method to change the hostname are given as.

Step 1: First, install the “cockpit” command by executing the below-given command in the terminal.

sudo apt install cockpit

Step 2: Now enable the “cockpit” command by executing the below-given command.  

sudo systemctl enable --now cockpit

The above command enables the web console to run the remote machine for the user. To access the remote machine click on the link and log in by inserting the username and password. The below figure is the result for the above command which shows that there is no output which means that the “cockpit” command has been enabled.  

Step 3: Now open the browser and search for the mentioned link in the below figure and insert username and password to login to your remote machine.   

Step 3: Now from the list of icons click on the “Overview” and from the “configuration” click on the “edit” option. 

Step 4: Now write the new hostname in the place of “Real host name” and click on the change in this way the host name will be changed.

Method 7: Use “nmtui” Command to Change the Hostname

To interact with the network manager “nmtui” command provides the cursor based interaction. Simply type the “nmtui” in the terminal and press enter then a user interface opens with different options.

nmtui

The sets involved to change the hostname from this cursor based interface are given below.

Step 1: From this interface press the arrow head down to the “set system hostname” option and then click the ok button.

Step 2: A new window opens which shows the old hostname.

Step 3: Now change the hostname and click on the “Ok” button. 

Step 4: Now simply hot the “Ok” button. Now your hostname has been changes from “tahir” to “Abbas”.  

Step 5: When the hostname changes this window moves back to the first screen then from this simply click on the quit.

Method 8: Use the GUI Method to Change the Hostname

This is the easiest way to change the hostname on the Linux/Ubuntu operating system. The steps to change the hostname by GUI method are given below.

Step 1: Open the setting by simply clicking on it.

Step 2: Now click on the device name from this window. 

Step 3: Now type the new hostname and click on the “Rename”. In this way you can rename the hostname.

Conclusion

To change the hostname in the Linux/Ubuntu operating system there are different method available such as use “hostnamectl” command, use “nmcli” command, use “sysctl” command, use “sudo hostname” command, use “etc/hosts” command, use “cockpit” command and use “nmtui” command to change the hostname. From all above methods the most interesting one is by using the “nmtui” command, simply run this command in the terminal then a coarser base interface opens from where you remove the old name and insert the new one and simply click ok to change the hostname.