You are currently viewing How to Set Hostname in Linux

How to Set Hostname in Linux

The hostname is a readable name easy to remember rather than a 4 number which can change, so the hostname is used to connect to a machine rather than its IP address. This can be used in a linux system allowing the users to recognize and distinguish the machine within a network. 

Even though it is a important task for a system admin or a devops engineer, there are several ways this can be accomplished. In this article, let me share the methods i personally use and know to set hostname in a Linux System.  

The hostname is a crucial identifier for a Linux system, allowing it to be recognized and distinguished within a network. Setting the hostname is a fundamental task that can be accomplished through various methods. In this article, we will explore different approaches to set the hostname in Linux, catering to different preferences and system configurations.

1. Using the hostname Command

The hostname command is the most straightforward one to set a hostname in Linux systems. Open a terminal and type the following command:

sudo hostnamectl set-hostname your-new-hostname

Replace “your-new-hostname” with the desired hostname that you prefer. This command will set the hostname immediately, but keep in mind that it might not persist after a reboot.

2. Editing the /etc/hostname File

Another common method is to directly change hostname by editing the /etc/hostname file. Open the file using a text editor, such as nano or vi: [ remember that it might require superuser access ] 

sudo nano /etc/hostname

Replace the hostname with your desired hostname and save the file, and exit. To apply the changes, you might need to reboot your system or use the hostnamectl command:

sudo hostnamectl set-hostname $(cat /etc/hostname)

3. Modifying the /etc/hosts File

The /etc/hosts file associates IP addresses to respective hostnames. You can manually edit this file to set or change the hostname. Open the file with a text editor:

sudo nano /etc/hosts

Go to  the line that starts with your system’s IP address, followed by the current hostname. Replace the existing hostname with your desired one and save the changes.

4. Using the sysctl Command

The sysctl command can also be used to set the hostname in Linux. Please use the following command in terminal,

sudo sysctl kernel.hostname=your-new-hostname

Replace “your-new-hostname” with the desired hostname. This change is temporary and will be lost after a reboot. To make it permanent, you can add the following line to the /etc/sysctl.conf file:


5. Configuring the Hostname in Network Configuration Files

Linux systems often have network configuration files where you can set the hostname. But the location of this file might vary based on the linux distribution you are using. For example, in Ubuntu, the configuration file is  in /etc/network/interfaces. Open the file and add or modify the following line:

hostname your-new-hostname

Save the file and restart the network service or reboot for the changes to take effect.

6. Using the nmcli Command (For Systems with NetworkManager)

For systems using NetworkManager, you can set the hostname using the nmcli command:

sudo nmcli general hostname your-new-hostname

This command will immediately update the hostname, and the change should persist across reboots.


Setting up the hostname in Linux environment is a simple task and yet it is an essential one for system identification and communication purposes. The methods discussed in these articles are the ones I know and are generally used in the industry. Whether you prefer using command-line utilities like hostnamectl or editing configuration files directly, choosing the method that suits your needs will make sure that there is a smooth and effective change of hostname on your Linux system. Remember to consider the specific requirements and conventions of your Linux distribution when applying these methods.

Leave a Reply