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 instance of EC2 Linux by using the hostname command, if you reboot or stop/start the instance it will revert to using a hostname derived from the IP address assigned to the instance.

The Linux hostname command can be used by administrators to change the hostname of an EC2 Linux instance. If you want the new hostname to persist between instance stops/starts and reboots, you must add the new hostname to the appropriate configuration files on your EC2 Linux instance.

  1. Update the /etc/sysconfig/network file on your RHEL 5 and 6, Centos 5 and 6, or Amazon Linux instance with the new hostname, and set the value of the NETWORKING parameter to yes.
         sudo vim /etc/sysconfig/network
    Change the name associated with HOSTNAME parameter to the hostname that you want the instance to use even after a restart or reboot.
         HOSTNAME=persistent_host_name
    Set the value of the NETWORKING parameter to yes.
         NETWORKING=yes
    Save and exit the vim editor.
    Note
    After making this change, press 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.
  2. Update the /etc/hosts file on your RHEL 5 and 6, Centos 5 and 6, or Amazon Linux instance with the new hostname.
         sudo vim /etc/hosts
    Change the entry beginning with 127.0.0.1 to read as follows:
         127.0.0.1 persistent_host_name.localdomain persistent_host_name localhost localhost.localdomain
    Save and exit the vim editor.
  3. Run the following command to reboot the instance to pick up the new hostname:
         sudo reboot
  4. After you reboot the EC2 instance, run the Linux hostname command again without any parameters to verify that the hostname change persisted.
         hostname
    The command should return the new hostname.
         persistent_host_name

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


Did this page help you? Yes | No

Back to the AWS Support Knowledge Center

Need help? Visit the AWS Support Center.