I changed the hostname of my Amazon Elastic Compute Cloud (Amazon EC2) instance. However, when I reboot or stop and then start the instance, the hostname changes back to the original hostname.
Short description
To make a hostname persist when you reboot or stop and start your EC2 instance, add the hostname to the instance's relevant configuration files.
The following steps apply to Amazon Linux and RHEL 5 or 6, and CentOS 5 or 6. For instructions that apply to other distributions, see the following resources:
Important: RHEL 5 or 6 and CentOS 5 or 6 are end of life (EOL) distributions. This means that they no longer receive active support, such as security updates. It's a best practice to use operating system distributions that still receive regular updates.
Resolution
-
Open /etc/sysconfig/network in vim:
sudo vim /etc/sysconfig/network
-
Change the value of the HOSTNAME parameter (replace persistent_hostname with your preferred hostname):
HOSTNAME=persistent_hostname
-
Set NETWORKING to yes:
NETWORKING=yes
-
Save and exit the vim editor.
Note: After making this change, press SHIFT + : to open a new command entry box in the vim editor. Type wq, and then press Enter to save your changes and exit vim.
-
Open /etc/hosts in vim:
sudo vim /etc/hosts
-
Change the entry beginning with 127.0.0.1 to the following, replacing persistent_hostname, localdomain, and localhost with your preferred hostname, local domain, and localhost:
127.0.0.1 persistent_hostname.localdomain localhost localhost.localdomain
-
Save and exit the vim editor.
-
To verify that you successfully changed the hostname, reboot your instance, and then run the hostname command:
sudo reboot
hostname
The command returns the new hostname.
Related information
Change the hostname of your Amazon Linux instance