Controlling hostname in Linux – hostnamectl

Hostname is a name given to machine. You can check the machine’s hostname using following command.

# hostname

Using the same command you can set hostname also.

# hostname station1.example.com

Basically Linux maintains host in /etc/hostname file. You can edit this file manually.

# nano /etc/hostname

After reboot you can see the changed hostname.

Linux 7 comes with very good utility to manage this. The utility is hostnamectl You can use this utility as below.

# hostnamectl
Static hostname: station1.example.com
Icon name: computer-vm
Chassis: vm
Machine ID: 9a555b999828d84a8efdaaba4cc40009
Boot ID: 7211790faf414f94ad8e07fcfbaa0707
Virtualization: kvm
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-1160.el7.x86_64
Architecture: x86-64

You can change hostname as below.

# hostnamectl set-hostname station2.example.com

The changes will be applied immediately.