There are two methods that I learned about when I wanted to change hostname on CentOS 7.
Method 1:
To see hostname:
Simply, we can check what is our system’s name by issuing following commands:
# hostnamectl
# hostnamectl status
# hostname
To set hostname to “centos7”:
# hostnamectl set-hostname centos7
To set static hostname to “centos7.domain.com”
# hostname set-hostname centos7.domain.com --static
To restart the systemd-hostnamed service:
# systemctl restart systemd-hostnamed
To verify changes:
# 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