The hostname is a unique identifier for a Linux machine within a network. It is typically set during system installation and stored in the /etc/hostname file.

You can view the current hostname by using the hostname command, examining the contents of the /etc/hostname file, or observing it in the command prompt when you log into the system.

There are several ways to change the system's hostname, such as editing the /etc/hostname file directly or using the hostname command. However, the recommended approach is to use the hostnamectl tool. This tool is specifically designed for managing hostnames and ensures that the changes persist after a system reboot. By using hostnamectl, you can avoid misconfigurations and make sure your changes are applied correctly.

Steps to change hostname in Linux:

  1. Open a terminal application.
  2. Check the current hostname.
    user@host:~$ hostname
    host
  3. Use hostnamectl to change the hostname.
    user@host:~$ sudo hostnamectl set-hostname newhost
  4. Verify the new hostname by checking it again.
    user@host:~$ hostname
    newhost
  5. Examine the /etc/hostname file to ensure the change has been saved and will persist.
    user@host:~$ cat /etc/hostname
    newhost
  6. Open a new shell to test if the change has been successful.
    user@host:~$ bash
    user@newhost:~$
Discuss the article:

Comment anonymously. Login not required.