Every Linux machine is required to have a hostname, which is usually configured during system installation. The hostname information is stored in the /etc/hostname file and is used to identify a system within a network, among other things.

You can view the current hostname of the system by viewing the content of the /etc/hostname file or by using the hostname command. Command prompts in Linux also normally display the system's hostname.

You can change the system's hostname by editing the /etc/hostname file or using the hostname command. The proper way, though, is to use the hostnamectl tool. hostnamectl avoids misconfiguration of the /etc/hostname file and ensures that the change is persistent after the system reboots.

Steps to change hostname in Linux:

  1. Launch a terminal application.
  2. Check current host name.
    user@host:~$ hostname
    host
  3. Change host name with hostnamectl.
    user@host:~$ sudo hostnamectl set-hostname newhost
  4. Check current hostname again to confirm.
    user@host:~$ hostname
    newhost
  5. Check /etc/hostname to ensure change is saved and is persistent.
    user@host:~$ cat /etc/hostname
    newhost
  6. Launch new shell to test if change is successful.
    user@host:~$ bash
    user@newhost:~$
Discuss the article:

Comment anonymously. Login not required.