Skip to content

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

4 minute read
0

I changed the hostname of my Amazon Elastic Compute Cloud (Amazon EC2) Linux instance. However, when I reboot or stop and start the instance, the hostname changes back to the original hostname.

Resolution

Prerequisite: Create an Amazon Elastic Block Store (Amazon EBS)–backed Amazon Machine Image (AMI) of your EC2 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. Make sure that the value for the preserve_hostname attribute is true.
    Example attribute:

    preserve_hostname: true

    Note: If the file doesn't have the preserve_hostname attribute, then add the attribute to the file.

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

    hostnamectl set-hostname persistent-hostname

    Note: Replace persistent-hostname with your hostname.

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 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. Make sure that the value for the preserve_hostname attribute is true.
    Example attribute:

    preserve_hostname: true

RHEL or CentOS 8, 9, and 10

For Red Hat Enterprise Linux (RHEL) or CentOS 8, 9, and 10, 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. Run the following command to open /etc/cloud/cloud.cfg:

    vi /etc/cloud/cloud.cfg
  3. Make sure that the value for the preserve_hostname attribute is true.
    Example attribute:

    preserve_hostname: true

SUSE Linux 12,15, and 16

For SUSE Linux Enterprise Server (SLES), complete the following steps:

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

    hostnamectl set-hostname 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:

    127.0.0.1 localhost persistent-hostname 

    Note: Replace persistent-hostname with your hostname.

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

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

    preserve_hostname: true

Reboot the instance

To reboot the instance, run the following command:

reboot

To verify that the instance uses the new hostname after reboot, run the following command:

hostnamectl

Troubleshoot issues

If you still 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 installed system updates that affect /etc/hosts, the hostname file, or the hostname utility, then complete the 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 OFFICIALUpdated 13 days ago