How to Set Hostname Permanently in Linux
Setting a hostname is a fundamental step in configuring a Linux system. The hostname acts as an identifier for your machine on a network, making it easier to manage and communicate with other devices. In this article, weâll explore the importance of having a well-defined hostname and the process of setting it permanently in a Linux environment.
A hostname not only gives the recognition of your system but also has a crucial role in networking. Whether youâre on a local network or the internet, you should have a clear and identifiable hostname for the administration of your Linux machine.
You can set a temporary hostname using the hostname command while a permanent hostname configuration stays the same even after reboots. This article lets you know how to configure a permanent hostname, understand the relevant configuration files, and the necessary steps to apply the changes.
Setting Hostname Temporarily and Permanently in Linux:
Let us check out how to set a hostname temporarily and permanently in Linux:
Temporarily:
Step 1: Check the Current Hostname
First, you need to check the current hostname of the device by using the hostname command in terminal
hostname
Output:
This command displays the current hostname of your Linux machine. We can see that the hostname of our machine is Kali.
Step 2: Set the Hostname Temporarily
Here we are setting the temporary hostname as w3wiki using the command sudo hostnamectl set-hostname <name> and entering the sudo password. Replace <name> with your desired hostname. This change is temporary and will be lost after a system reboot.
sudo hostnamectl set-hostname <name>
Output:
Step 3: Verify Hostname Changes
You can verify the change in hostname by using the hostname or hostnamectl command in the terminal, which will display the temporary hostname we created.
hostname
hostnamectl
Output:
Ensure that the changes are reflected in the system.
Permanently:
Step 1: Check the Current Hostname
First, you need to check the current hostname of the device by using the hostname command in terminal
hostname
Output:
Ensure you know the current hostname before making any changes. This command displays the current hostname of your Linux machine. We can see that the hostname of our machine is Kali.
Step 2: Check File Directories
You need to check the contents of these two file directories â/etc/hostname and /etc/hostsâ and you will erase some content in these files and rewrite it in the next step.
cat /etc/hostname
cat /etc/hosts
Output:
Inspect the contents of these files to confirm the current hostname.
Step 3: Edit /etc/hostname using Vi Editor
Use the sudo vi /etc/hostname command to enter into the vi editor and change the contents in the /etc/hostname file after entering into the vi editor follow these steps:
- Enter insert mode by pressing i.
- Delete the existing hostname and enter the new one.
- Save the changes by pressing Esc, then:wq!.
here we are changing our hostname to w3wiki
sudo vi /etc/hostname
Output:
Step 4: Edit /etc/hosts using Vi Editor
Use the sudo vi /etc/hosts command to enter into the vi editor and change the contents in the /etc/hosts file after entering into the vi editor follow these steps:
- Enter insert mode by pressing i.
- Delete the existing hostname and enter the new one.
- Save the changes by pressing Esc, then:wq!.
here we are changing our hostname to w3wiki
sudo vi /etc/hosts
Output:
Step 5: Save the Hostname Permanently
Now save the hostname permanently by using the sudo hostname <name> command and replace the <name> with the hostname with the hostname you entered in the â/etc/hostname and /etc/hostsâ files. We can see w3wiki has set to the hostname permanently
sudo hostname <name>
Output:
Step 6: Verify Hostname Changes
You can also verify the hostname changes using hostname and hostnamectl commands in the terminal. Ensure that the changes are reflected in the system.
hostname
hostnamectl
Output:
Step 7: Verify After Reboot
Reboot the system and recheck the hostname to confirm the changes have been applied after a restart.
Setting Hostname Permanently From GNOME Settings
Step 1: Open Settings
Open the Applications menu and search for the âAboutâ option then, open it. You will find various settings such as Device Name, Memory, etc.
Step 2: Rename the Hostname
Click the arrow mark next to the Device Name. A window will appear, allowing you to rename the device. Rename it accordingly. We are changing our machineâs hostname from âUbuntuâ to âw3wiki.â
Step 3: Restart
Save the new hostname by clicking âRename,â and the hostname is permanently changed to the new name, here it is changed to âw3wiki.â Restart the machine to ensure the new hostname is set.
Note: Changing the hostname from GNOME settings may not take effect immediately, so a restart of the Linux machine is necessary. This method is not always reliable.
Setting Hostname From Network Manager TUI
Step 1: Check the Current Hostname
First, you need to check the current hostname of the device by using the hostname command in terminal
hostname
Step 2: Set the Hostname
Set the hostname using Network Manager TUI with the ânmcli general hostname <new_name>â command in the terminal, and replace â<new_name>â with your desired new hostname. Here we are setting our new hostname to w3wiki from GFG.
nmcli general hostname w3wiki
Step 3: Privilege Access
After running the previous step, it will prompt for administrator access. Provide the necessary permission by authenticating with your machineâs password.
Step 4: Restart NetworkManager
After setting the hostname, restart NetworkManager using âsudo systemctl restart NetworkManagerâ and restart your machine to apply the changes permanently.
sudo systemctl restart NetworkManager
Step 5: Verification
You can verify the changes using the hostname command in the terminal:
hostname
Conclusion:
In conclusion, setting a hostname in Linux includes both temporary and permanent steps. It is essential for customization and system identification. While temporary changes are used for immediate needs, making permanent adjustments ensures that the chosen hostname stays the same even after reboots. We can change the hostname temporarily by using the sudo hostnamectl set-hostname <name> command and permanently by changing the hostname to â/etc/hostname and /etc/hostsâ It will reflect changes in the hostname even after the reboot. You can also use GNOME and NetworkManager TUI to set the hostname permanently. You can follow the simple steps in the article to change the hostname of a Linux machine permanently