How do I assign a static hostname to a private Amazon EC2 instance running RHEL 7 or Centos 7?

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.

  1. Edit /etc/hostname and replace the value with the hostname that you want.
  2. Update /etc/hosts file. Change the entry beginning with 127.0.0.1 to include your hostname
  3. Edit /etc/sysconfig/network and append HOSTNAME=xxxxxxx with your hostname
  4. Edit /etc/cloud/cloud.cfg and append the following line at the end of the file without the quotes: “preserve_hostname: true”
  5. Change hostname using hostnamectl command

Your hostname change will be persistent across reboots now.

For more information:
https://aws.amazon.com/premiumsupport/knowledge-center/linux-static-hostname-rhel7-centos7/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s