Amazon EC2 instance hostnames are derived from the IP address that is dynamically assigned to the instance at startup. Although you can change the hostname of a private instance of EC2 Linux by using the hostname command, if you reboot or stop/start the instance it will revert to using a hostname derived from the IP address assigned to the instance
For a hostname to be static on RHEL 7 or CentOS 7, you have to perform the below steps.
- Edit /etc/hostname and replace the value with the hostname that you want.
- Update /etc/hosts file. Change the entry beginning with 127.0.0.1 to include your hostname
- Edit /etc/sysconfig/network and append HOSTNAME=xxxxxxx with your hostname
- Edit /etc/cloud/cloud.cfg and append the following line at the end of the file without the quotes: “preserve_hostname: true”
- Change hostname using hostnamectl command
Your hostname change will be persistent across reboots now.