how to set or change hostname on centos 7

Most of the applications like mail server requires proper hostname set for the server. CentOS now have new command to set and check hostname. To check the current set hostname you can use below command hostnamectl

[root@mail ~]# hostnamectl
 Static hostname: mail.pankaj.com
 Icon name: computer-vm
 Chassis: vm
 Machine ID: 586cdfebba43456eba9928c40c5761d9
 Boot ID: ef208521e56a400e88b00c3156cfdbd5
 Virtualization: vmware
 Operating System: CentOS Linux 7 (Core)
 CPE OS Name: cpe:/o:centos:centos:7
 Kernel: Linux 3.10.0-514.2.2.el7.x86_64
 Architecture: x86-64

Hostname can be of 3 types static, transient and pretty. Static hostnames are traditional hostnames which are stored in /etc/hostname. Pretty hostname are set by kernel or DHCP servers. Pretty hostnames are UTF 8 hostname.

To set hostname

You can use below command to set hostname

hostnamectl set-hostname mail2.pankaj.com --static

You can check the hostname as

 Static hostname: mail2.pankaj.com
Transient hostname: mail.pankaj.com
 Icon name: computer-vm
 Chassis: vm
 Machine ID: 586cdfebba43456eba9928c40c5761d9
 Boot ID: ef208521e56a400e88b00c3156cfdbd5
 Virtualization: vmware
 Operating System: CentOS Linux 7 (Core)
 CPE OS Name: cpe:/o:centos:centos:7
 Kernel: Linux 3.10.0-514.2.2.el7.x86_64
 Architecture: x86-64

You still can see transient hostname is showing old value hence we will set it using below command

hostnamectl set-hostname mail2.pankaj.com --transient

Leave a Comment