I changed the hostname of my private Amazon Elastic Compute Cloud (Amazon EC2) instance. But when I reboot, or I stop and then restart the instance, the new hostname reverts to the hostname that was previously assigned to the instance. How do I fix that?

Amazon EC2 instance hostnames are derived from the IP address that is dynamically assigned to the instance at startup. When you change the hostname of a private instance of EC2 Linux by using the hostname command, the hostname will not persist if you reboot, or stop and then restart the instance. If you want the new hostname to persist between instance stops and starts, or reboots, you must add the new hostname to the appropriate configuration files on your EC2 Linux instance.

1.    Connect to your EC2 Linux instance using SSH. For more information, see Connecting to Your Linux Instance Using SSH.

2.    Switch to the root user.

sudo su

3.    Use either the YaST interface or manually update your SUSE Linux instance with the new hostname.

YaST interface update

1.    Run the yast command to launch the YaST interface.

yast

2.    Using the Tab and arrow keys, navigate to Network Devices, then to Network Settings, and then press Enter.

3.    In Network Settings, use the arrow keys to navigate to Hostname/DNS and use Tab and the Shift + Tab combination to cycle through the available options.

4.    Select and set the hostname and domain name to the preferred values.

5.    Navigate to Change Hostname via DHCP, and then press the Spacebar to clear this option.

6.    Use Tab to navigate to [OK], and then press Enter to accept changes.

7.    Use Tab to navigate to [Quit], and then press Enter to exit YaST.

8.    Use the vim editor to update the /etc/cloud/cloud.cfg file on your SUSE Linux instance.

vim /etc/cloud/cloud.cfg

9.    Find the preserve_hostname string and change the default setting to true so that the hostname is preserved between restarts or reboots.

preserve_hostname: true

10.    Save and exit the vim editor by using the Shift + : (colon) to open a new command entry box in the vim editor, type wq, and then press Enter to save changes and exit vim.

11.    Reboot the instance.

sudo reboot

12.    Connect to your Amazon EC2 instance, and then run the Linux hostname command without any parameters to verify that the hostname change persisted.

hostname

The command should return the new hostname.

Manual update

1.    Use the hostnamectl command to set the new hostname. Replace new-hostname with your hostname.

hostnamectl set-hostname new-hostname

2.    Use the vim editor to update the /etc/hosts file with the new hostname.

vim /etc/hosts

3.    Find the localhost string and append the new hostname, again replacing new-hostname with your hostname.

127.0.0.1 localhost new-hostname

4.    Save and exit the vim editor by using Shift + : (colon) to open a new command entry box in the vim editor, type wq, and then press Enter to save changes and exit vim.

5.    Use the vim editor to update the /etc/cloud/cloud.cfg file on your SUSE Linux instance.

vim /etc/cloud/cloud.cfg

6.    Find the preserve_hostname string, and then change the default setting to true so that the hostname is preserved between restarts or reboots.

preserve_hostname: true

7.    Save and exit the vim editor by using Shift + : (colon) to open a new command entry box in the vim editor, type wq, and then press Enter to save changes and exit vim.

8.    Reboot the instance.

sudo reboot

9.    Connect to your Amazon EC2 instance, and then run the Linux hostname command without any parameters to verify that the hostname change persisted.

hostname

The command should return the new hostname.


Did this page help you? Yes | No

Back to the AWS Support Knowledge Center

Need help? Visit the AWS Support Center.

Published: 2015-09-30

Updated: 2019-01-17