How do I assign a static hostname to an EC2 Linux instance?

4 minute read
0

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:

  1. Use SSH to connect to your Linux instance.

  2. To switch to the root user, run the following command:

    sudo su -
  3. 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:

  1. To open /etc/cloud/cloud.cfg, run the following command:

    vim /etc/cloud/cloud.cfg
  2. 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.

  3. 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:

  1. To set the static hostname, complete the following steps:

    hostnamectl set-hostname --static persistent-hostname

    Note: Replace persistent-hostname with your hostname.

  2. To open /etc/hosts, run the following command:

    vim /etc/hosts
  3. Enter the following line in the file:

    localhost persistent-hostname

    Note: Replace persistent-hostname with your hostname.

  4. 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
  5. To open /etc/cloud/cloud.cfg, run the following command:

    vim /etc/cloud/cloud.cfg
  6. 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:

  1. To set the static hostname, run the following command:

    hostnamectl set-hostname --static persistent-hostname

    Note: Replace persistent-hostname with your hostname.

  2. For RHEL 7.3 or earlier, run the following command to open /etc/cloud/cloud.cfg:

    vi /etc/cloud/cloud.cfg
  3. 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:

  1. 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.

  2. Run the following command to update the /etc/HOSTNAME file with your hostname:

    echo "persistent-hostname" > /etc/HOSTNAME

    Note: Replace HOSTNAME with your hostname.

  3. To open /etc/hosts, run the following command:

    vim /etc/hosts
  4. 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.

  5. To open /etc/cloud/cloud.cfg file, run the following command:

    vim /etc/cloud/cloud.cfg
  6. Enter the following line in the file:

    preserve_hostname: true

Implement the changes

Complete the following steps:

  1. To reboot the instance, run the following command:

    reboot
  2. 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

AWS OFFICIAL
AWS OFFICIALUpdated 8 days ago