Every Linux machines comes with a host name, which is normally configured during system installation. It is stored in /etc/hostname and normally displayed at the terminal as \h in the PS1 environment variable.

Current host name of the system can be viewed via hostname command line tool or by viewing the content of the /etc/hostname.

System host name in Linux could be changed via hostname command as below but it is not persistent and will return to the original host name after system reboot.

[email protected]:~$ hostname
host
[email protected]:~$ sudo hostname newhost
[email protected]:~$ hostname
newhost
[email protected]:~$ cat /etc/hostname
host

The proper way to change system host name in Linux is by using hostnamectl tool at the terminal.

Steps to change system hostname in Linux:

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

Comment anonymously. Login not required.

Share!