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. How do I get the hostname to persist?
To be sure that a hostname persists when an Amazon EC2 instance is rebooted or stopped, you must add the hostname to the appropriate configuration files on your Amazon EC2 instance.
Note: The following steps apply to Amazon Linux. For instructions that apply to Amazon Linux 2, see How do I assign a static hostname to a private Amazon EC2 instance running RHEL 7 or Centos 7?
1. Open /etc/sysconfig/network in vim:
sudo vim /etc/sysconfig/network
2. Change the value of the HOSTNAME parameter (replace persistent_hostname with your preferred hostname):
HOSTNAME=persistent_hostname
3. Set NETWORKING to yes:
NETWORKING=yes
4. 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.
5. Open /etc/hosts in vim:
sudo vim /etc/hosts
6. 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 local host:
127.0.0.1 persistent_hostname.localdomain localhost localhost.localdomain
7. Save and exit the vim editor.
8. To verify that you successfully changed the hostname, reboot your instance, and then run the hostname command:
sudo reboot 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-14
Updated: 2019-01-22