The hostname is a key identifier for a Linux system on a network. It helps other machines recognize and communicate with your system. The hostname is usually configured during the initial system setup and stored in the /etc/hostname file.

You can view the current hostname by using the hostname command or checking the contents of the /etc/hostname file. It can also be seen directly in your command prompt when you log in. Understanding how to check and change the hostname is essential for proper network management.

To change the hostname, it is recommended to use the hostnamectl command. This tool is designed to update the hostname effectively and ensures that the change persists after a reboot. Using hostnamectl prevents potential misconfigurations and ensures the new hostname is applied correctly.

Steps to change hostname in Linux:

  1. Open the terminal.
  2. Check the current hostname.
    user@host:~$ hostname
    host
  3. Use hostnamectl to set a new hostname.
    user@host:~$ sudo hostnamectl set-hostname newhost
  4. Verify the new hostname.
    user@host:~$ hostname
    newhost
  5. Ensure the change is saved in the /etc/hostname file.
    user@host:~$ cat /etc/hostname
    newhost
  6. Open a new terminal session to confirm the hostname has been updated.
    user@host:~$ bash
    user@newhost:~$
Discuss the article:

Comment anonymously. Login not required.