I changed the hostname of my Amazon Elastic Compute Cloud (Amazon EC2) instance. However, when I reboot or stop and then restart the instance, the hostname changes back to the original hostname.
Resolution
Important: Before you update your instance, create an Amazon Elastic Block Store (Amazon EBS)-backed Amazon Machine Image (AMI) of your instance.
Check the existing instance hostname
Complete the following steps:
-
Use SSH to connect to your Linux instance.
-
To switch to the root user, run the following command:
sudo su -
-
To check the instance hostname, run the following command:
hostnamectl
Configure a static hostname
Configure a static hostname for the operating system (OS) that you run.
AL2 or Al2023
For Amazon Linux 2 (AL2) or Amazon Linux 2023 (AL2023), complete the following steps:
-
To open /etc/cloud/cloud.cfg, run the following command:
vim /etc/cloud/cloud.cfg
-
Review the preserve_hostname attribute. If its value is false, then update the value to true.
Example:
preserve_hostname: true
Note: If the file doesn't have the preserve_hostname attribute, then add the attribute to the file and set the value to true.
-
To set the static hostname, run the following command:
hostnamectl set-hostname persistent-hostname
Note: Replace persistent-hostname with your hostname.
Ubuntu
For Ubuntu, complete the following steps:
-
To set the static hostname, complete the following steps:
hostnamectl set-hostname --static persistent-hostname
Note: Replace persistent-hostname with your hostname.
-
To open /etc/hosts, run the following command:
vim /etc/hosts
-
Enter the following line in the file:
localhost persistent-hostname
Note: Replace persistent-hostname with your hostname.
-
If your EC2 instance uses IPv6, then also enter the following configuration data:
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts
-
To open /etc/cloud/cloud.cfg, run the following command:
vim /etc/cloud/cloud.cfg
-
Review the preserve_hostname attribute. If its value is false, then update the value to true.
Example:
preserve_hostname: true
RHEL or CentOS 7, 8, and 9
For Red Hat Enterprise Linux (RHEL) or CentOS 7, 8, and 9, complete the following steps:
-
To set the static hostname, run the following command:
hostnamectl set-hostname --static persistent-hostname
Note: Replace persistent-hostname with your hostname.
-
For RHEL 7.3 or earlier, run the following command to open /etc/cloud/cloud.cfg:
vi /etc/cloud/cloud.cfg
-
Review the preserve_hostname attribute. If the value is false, then update the value to true.
Example:
preserve_hostname: true
SUSE Linux
For SUSE Linux Enterprise Server (SLES), complete the following steps:
-
To set the hostname, run the following command based on your SLES version.
SLES 11:
hostname persistent-hostname
SLES 12 and 15:
hostnamectl set-hostname persistent-hostname
Note: Replace persistent-hostname with your hostname.
-
Run the following command to update the /etc/HOSTNAME file with your hostname:
echo "persistent-hostname" > /etc/HOSTNAME
Note: Replace HOSTNAME with your hostname.
-
To open /etc/hosts, run the following command:
vim /etc/hosts
-
Enter the following line in the file based on your SLES version and replace it with your hostname.
SLES 11:
127.0.0.1 localhost persistent-hostname
SLES 12 and 15:
127.0.0.1 localhost persistent-hostname
Note: Replace persistent-hostname with your hostname.
-
To open /etc/cloud/cloud.cfg file, run the following command:
vim /etc/cloud/cloud.cfg
-
Enter the following line in the file:
preserve_hostname: true
Implement the changes
Complete the following steps:
-
To reboot the instance, run the following command:
reboot
-
To verify the new hostname, run the following command:
hostnamectl
Troubleshoot your issues
If you experience unexpected hostname changes after reboot, then take the following actions:
- Check the system logs at /var/log/cloud-init.log and /var/log/syslog for entries about hostname changes.
- If you install system updates that affect /etc/hosts, the hostname file, or the hostname utility, then complete the preceding resolution steps again.
- Verify that the preserve_hostname setting is true in the /etc/cloud/cloud.cfg file.
Related information
Change the hostname of your AL2 instance