How to change hostname in CentOS7

Method 1:

To see hostname:
$ hostnamectl
$ hostnamectl status
$ hostname

To set host name to “centos7”, enter:
# hostnamectl set-hostname centos7

To set static host name to “centos7.example.com”, enter:
# hostnamectl set-hostname centos7.example.com --static

To restart the systemd-hostnamed service:
# systemctl restart systemd-hostnamed

To verify changes, enter:
# hostnamectl status

To reboot the system:
# reboot

Method 2:

Add the following entry to network file:
# nano /etc/sysconfig/network

HOSTNAME=centos7.example.com

Add the following entries to hosts file:
# nano /etc/hosts

127.0.0.1 centos1.example.com
::1 centos1.example.com

To restart the systemd-hostnamed service:
# systemctl restart systemd-hostnamed

To verify changes, enter:
# hostnamectl status

To reboot the system:
# reboot