How to set or change Hostname in Linux

In Linux, it is not difficult to change the hostname. In Linux, you can change hostname using hostname command.

If you execute hostname command without any parameter, it will display current hostname.

Linux hostname

If you want to change or set hostname of your Linux system, simply run:

# hostname new-hostname

change hostname

The above command will change the hostname immediately but the real problem is, original hostname will be restored again on next reboot.

Set System Hostname Permanently in Linux

Newer version of different Linux distributions such as latest Ubuntu, Debian, CentOS, Fedora, RedHat, etc. comes with systemd, a system and service manager that provides a hostnamectl command to manage hostnames in Linux.

To set system hostname on SystemD based distributions, we will use hostnamectl command as follow:

# hostnamectl set-hostname new-hostname

New Hostname

For older Linux distributions, which uses SysVinit in short init, can have their hostnames changed by simply editing the hostname file.

# vi /etc/hostname

Now, you need to add another record for the hostname:

# vi /etc/hosts

For example:

Then you need to restart service as follow:

# /etc/init.d/hostname restart

On RHEL/CentOS based systems that use init, the hostname is changed by modifying network file:

# vi /etc/sysconfig/network


Leave a Reply