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.
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.