Hostname is a unique identifier assigned to a device in a network. It facilitates easier navigation in a network and device identification. In Linux, you can change the hostname using several different methods, and in this article, we will explore three of them: using the command line, using the hostnamectl tool, and editing the configuration file.
Method 1: Using the Command Line
The easiest way to quickly change the hostname is by using the hostname command. This command immediately changes the hostname, but the change is not permanent and will only persist until the system is restarted.
- Open the terminal.
- Enter the command
sudo hostname new_hostname, wherenew_hostnameis the name you want to set as the new hostname. - The change will take effect immediately, but remember that after a restart, the hostname will revert to its original value.
Method 2: Using the hostnamectl Tool
hostnamectl is a tool part of systemd used for managing the system hostname. This method is recommended for systems using systemd because the change is permanent and survives system restarts.
- Open the terminal.
- Enter the command
sudo hostnamectl set-hostname new_hostname, replacingnew_hostnamewith the desired name. - You can verify that the change has been made by using the command
hostnamectl status.
Method 3: Editing the Configuration File
The last method involves editing the configuration file that stores the hostname. This file is usually located at /etc/hostname.
- Open the terminal.
- Enter the command
sudo nano /etc/hostnameto open the file in the nano editor. You can use any text editor you prefer instead of nano. - Delete the current hostname and type in the new one. Save and close the file.
- It is also recommended to edit the
/etc/hostsfile to add the new hostname to ensure proper functioning of network services. - After editing, restart the system for the changes to take effect.
Changing the hostname in Linux can be useful for various reasons, whether for better network identification or security purposes. Each of the methods mentioned above has its advantages, and the best choice depends on your specific needs and preferences. It's important to always backup relevant configuration files before making changes.



