Hostname in Linux is allowing it to be identified on a network. The system hostname is either set during installation or dynamically supplied to a virtual machine instance during startup. However, there may be times when you need to modify the hostname.
In this article, we will walk you through the process of changing the hostname in Linux without restarting the system. This method is applicable to modern Linux distributions that make use of system.
What is Hostname?
- A hostname is a one-of-a-kind label assigned to a computer or device on a network. It acts as an identification for that machine, allowing it to be identified and addressed on the network. A hostname can be a basic alphanumeric string with dots and hyphens. When the machine is linked to the internet and functions as a web or mail server, a fully qualified domain name (FQDN) should be used as the hostname.
- The FQDN is composed of two parts: the hostname and the domain name. The hostname normally denotes a specific machine or device, but the domain name identifies the larger network or organization to which the machine belongs. When the hostname and domain name are joined, they form a unique and meaningful identify for the machine.
Checking the Current Hostname
To begin, launch a terminal or connect to your Linux machine using SSH. First, let us look at the current hostname. The following command should be used to check current hostname:
hostnamectl

Change the Hostname in Linux (2 Ways)
Modify Hostname using Hostnamectl Command
The hostnamectl command, which is part of the systemd system and service manager, will be used to update the hostname.
For example, to change the system hostname to cloudbooklet.com, you would use the following command:
sudo hostnamectl set-hostname cloudbooklet.com
While the hostnamectl command does not generate output by default, you can use it to check whether the hostname was successfully changed. The hostnamectl command returns 0 on success and a non-zero failure code otherwise. You can tell if the hostname change was successful by looking at the exit status.
To find out the current hostname is, run the following command:
hostnamectl

The new system hostname and some extra system information, such as the kernel version, will be displayed on the terminal.
Modify Hostname without Restart the system
To change the hostname in Linux, alter the /etc/hostname file as follows:
- Open a terminal or SSH into your Linux machine.
- To modify the /etc/hostname file, use a text editor such as nano, vi, or your preferred Linux text editor:
sudo nano /etc/hostname
- You can find the current hostname in the text editor.

- Change it to the new hostname you choose. Remove the existing hostname and type the new one on a new line.

By modifying the contents of the /etc/hostname file and updating the /etc/hosts file, you can change the hostname in Linux using a text editor in the Terminal.
Save your changes and close the text editor. In nano, press Ctrl + X, then Y to confirm and save the changes.
Also read: You might also find useful our guide on How to Upgrade Linux Kernel in Ubuntu
Conclusion
In conclusion, modifying the hostname in Linux is a straightforward process that can be accomplished by editing the /etc/hostname file and updating the /etc/hostsfile. Furthermore, the hostnamectl command can be used to change the hostname without having to restart the system. Verifying the new hostname ensures a smooth transition. You may easily alter the hostname of your Linux machine using these procedures.
Please feel free to share your thoughts and feedback in the comment section below.
How to Modify the Hostname in Linux