Linux Commands Ubuntu

How to Set Hostname Variable in Linux/Ubuntu?

How to Set Hostname Variable in Linux Ubuntu

A hostname is a unique user-generated name used for the identification of a host on a network. Users have the option to set the hostname during the installation of Ubuntu 22.04. There are situations, where a user might require to set up a hostname, for example, on a certain network, two computers have the same hostname. In these situations, in order to avoid network conflict, a change of hostname is required.

This article will demonstrate setting up a hostname on Linux/Ubuntu 22.04 LTS as below:

  • How to Set a Hostname for the Current Session Using Command Line?
  • How to Set a Hostname Permanently Using the Command Line?
  • How to Set a Hostname Using NMTUI (Network Manager TUI)?
  • How to Set a Hostname Using GUI (Graphical User Interface)?

How to Check the Hostname of a System in Linux/Ubuntu 22.04?

A hostname can be checked by using the below-stated command:

$ hostname

From the above image, it can be seen that the hostname of the system is “ubuntu”. Additionally, additional information about the system can be viewed by using the following command:

$ hostnamectl

How to Set a Hostname for the Current Session Using Command Line?

A hostname can be set for the current session by executing the below command:

$ hostnamectl set-hostname ubuntu-test

From the above image, it can be seen that the command is executed successfully. However, the changes will be in effect after restarting the terminal as seen below:

It can be seen from the image above, that the hostname is changed from ubuntu to ubuntu-test. The hostname set by “hostnamectl” is temporary and no boot is required for changes to be in effect.

How to Set a Hostname Permanently Using the Command Line?

The hostname can be changed permanently by editing the following configuration files:

  • /etc/hostname
  • /etc/hosts

These files can be edited by the following steps:

Step 1: Edit /etc/hostname Configuration File

/etc/hostname configuration file can be edited by using a text editor, for example, nano:

$ sudo nano /etc/hostname

The /etc/hostname configuration file contains only the current hostname. This hostname can be edited.

It can be seen from the above figure that the current hostname is ubuntu-test. This hostname can be modified:

From the above figure, it can be seen that the hostname is modified from ubuntu-test to ubuntu. After the edit, the file is saved and closed by pressing Ctrl+O and Ctrl+X respectively.

Step 2: Edit /etc/hosts Configuration File

/etc/hosts configuration file can be edited by using a Nano text editor by executing the following command:

$ sudo nano /etc/hosts

The /etc/hosts configuration file contains hostname along with other information:

From the above image, we can see that the hostname is present in the second line of the file and can be edited:

The hostname is updated from ubuntu-test to ubuntu. After the edit, the file is saved and closed by pressing Ctrl+O and Ctrl+X respectively.

Step 3: Reboot System

After edits in /etc/hostname and /etc/hosts configuration files, the system is rebooted:

$ sudo systemctl reboot

The changes are applied after the system boot. The hostname can be verified by running the below command:

From the above figure, it can be verified that the hostname is changed from ubuntu-test to ubuntu.

How to Set a Hostname Using NMTUI (Network Manager TUI)?

NMTUI (Network Manager TUI) is a graphical command-line utility that allows us to easily configure network interfaces in Linux/Ubuntu. It can be used to set a hostname permanently. NMTUI tool can be launched by executing the “nmtui” command in the terminal:

$ nmtui

After NMTUI is launched, the option “Set system hostname” can be selected and then “OK”:

There will be a prompt to Set Hostname and any name can be typed here:

After typing hostname in “Hostname” field, OK will be selected to proceed with the process:

A user password will be required for authentication:

After authentication, there will be a prompt to press “OK” in order to confirm:

From the above image, it can be seen that the hostname is changed from “ubuntu-test” to “ubuntu”. These changes will be in effect after restarting the terminal:

It can be seen that after restarting the terminal the hostname is changed.

How to Set Hostname Using GUI (Graphical User Interface)?

The hostname can be changed by Ubuntu’s GUI (Graphical User Interface) fairly easily by naviating to “Settings” and then “About”:

From the above image, it can be seen that the current device name (or hostname) is “ubuntu”. By clicking the “ubuntu” button, a new dialog box will appear to replace the current “hostname”:

The hostname can be changed with a new hostname and it can be confirmed by pressing the “Rename” button. This changes the hostname permanently. Further, the change can be verified by checking the About section:

From the above figure, it can be seen that the hostname is modified from ubuntu to ubuntu-testhost.

Conclusion

Hostnames are set using the command line, NMTUi tool, and GUI. Hostnames are set permanently using the nmtui tool, Ubuntus GUI, and by editing “etc/hostname” and “etc/hosts” configuration files, while the “hostnamectl” command is used to set the hostname for the current session. This article has demonstrated setting up a hostname on Linux/Ubuntu 22.04 LTS.

Similar Posts