How to set hostname permanently – CentOS / RHEL – 7

Issue:

I have changed /etc/hostname and it changes after reboot.

Solution:

In CentOS / RHEL-7 you need to use the hostnamectlcommand line tool to update the hostname – To set the hostname permanently, issue the below command,

# hostnamectl set-hostname server.yourdomain.com

To view the current hostname and status, run the below,

[root@server1 ~]# hostnamectl
Static hostname: server.yourdomain.com
Icon name: computer-vm
Chassis: vm
Machine ID: e71b5f04137d4d99a31037f13c61afec
Boot ID: fd8cbc3d1f14476e829aa57aa05e4c9b
Virtualization: kvm
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-229.20.1.el7.x86_64
Architecture: x86_64

Other options with hostnamectl:

hostnamectl [OPTIONS…] COMMAND …

Query or change system hostname:

-h –help => Show this help
–version => Show package version
–transient => Only set transient hostname
–static => Only set static hostname
–pretty => Only set pretty hostname
-P –privileged => Acquire privileges before execution
–no-ask-password => Do not prompt for password
-H –host=[USER@]HOST => Operate on remote host

Commands:
status => Show current hostname settings
set-hostname NAME => Set system hostname
set-icon-name NAME => Set icon name for host
set-chassis NAME => Set chassis type for host