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 EC2 Linux instance using the hostname command, if you reboot or stop/start the instance it reverts to using the hostname based on the IP address assigned to the instance.

The hostnamectl command should be used to persist a new hostname between instance stops/starts and reboots of Linux distributions that rely on the systemd System and Service Manager to bootstrap the user space. Because both RHEL 7 and Centos 7 use systemd, perform the steps listed under Resolution to designate a new persistent hostname for these Linux distributions.

1.    Run the hostnamectl command from the shell to update the hostname of your RHEL 7 or Centos 7 Linux instance; replace with the hostname you want the instance to use:

sudo hostnamectl set-hostname --static <persistent_host_name>

2.    Use the vi text editor to update the /etc/cloud/cloud.cfg file on your RHEL 7 or Centos 7 Linux instance. For general information about using the vi editor, run the command "info vi" from the command shell (without quotation marks).

sudo vi /etc/cloud/cloud.cfg

Add the following string to the bottom of the file to ensure that the new hostname is preserved between restarts/reboots:

preserve_hostname: true

Save and exit the vi editor.

3.    Run this command to reboot the instance and associate the new hostname with the instance:

sudo reboot

4.    After you reboot the EC2 instance, run the Linux hostname command without any parameters to verify that the hostname change persisted:

hostname

The command should return the new hostname that you specified for in step 1:

<persistent_host_name>

Amazon EC2, Linux, hostname, persistent, hosts, 127.0.0.1, vim, private, RHEL 7, Centos 7


Did this page help you? Yes | No

Back to the AWS Support Knowledge Center

Need help? Visit the AWS Support Center.

Published: 2015-09-03

Updated: 2017-06-08