How to set/update hostname in linux machine

What is Hostname?

Hostname is the name given to machine in the network for identification. In a Network, admins allocate unique hostname for identification. Hostname can be a alphanumeric string with dots and dash. Host name of a machine can be viewed by simply typing hostname without any arguments in the console

 ~]# hostname
linux-test-datahub

If you want to change the hostname of a linux machine, simply run hostname <name>

~]# hostname linuxdatahub
~]# hostname
linuxdatahub

Changing Hostname to survive over reboot

As mentioned above the changes done to the hostname of the machine with hostname command, will be lost over the reboot. Below section show how to permanently set the hostname

Using systemd

Latest Linux variants have systemd, which provides a utility called hostnamectl to control/manage hostname in Linux. To change the hostname of a linux machine using hostnamectl, execute command with argument set-hostname

sudo hostnamectl set-hostname linux-data-hub

Edit /etc/hostname and enter the string which you want as hostname

~]# vi /etc/hostname
~]# cat /etc/hostname
linux-data-hub

Using SysVinit or init

Older Linux variants uses init, we can change the hostname of the machine by below steps

  • Edit /etc/hostname and enter the string which you want as hostname
~]# vi /etc/hostname
~]# cat /etc/hostname
linux-data-hub
  • Add record for the new host name in /etc/hosts
127.0.0.1 linux-data-hub
  • Restart the intid
~]# /etc/init.d/hostname restart

Related Article

Read more on different classes of hostnames in linux

Search on LinuxDataHub

Leave a Comment