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:

Hostname displayed

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:

Hostname and hosts are displayed

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:

command to edit the hostname file

hostname set to w3wiki

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:

command to edit the host file

hostname set to w3wiki

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:

Hostname successfully set to w3wiki

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.

Similar Reads

Setting Hostname Temporarily and Permanently in Linux:

Let us check out how to set a hostname temporarily and permanently in Linux:...

Permanently:

Step 1: Check the Current Hostname...

Setting Hostname Permanently From GNOME Settings

Step 1: Open Settings...

Setting Hostname From Network Manager TUI

Step 1: Check the Current 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 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...