How do I assign a static hostname to an Amazon EC2 instance that runs SLES?

3 minute read
0

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 rebooting or stopping and starting your EC2 instance, add the hostname to the appropriate configuration files on the instance.

Note: The following steps apply to SLES. For instructions that apply to other distributions, see one of the following:

Resolution

  1. Connect to your EC2 Linux instance using SSH. For more information, see Connect to your Linux instance using SSH.

  2. Switch to the root user.

    sudo su
  3. Use the hostnamectl command to set the new hostname. Replace new-hostname with your hostname.

    SLES 11:

    hostname new-hostname

    SLES 12 and SLES 15:

    hostnamectl set-hostname new-hostname
  4. Use the vim editor to update the /etc/hosts file with the new hostname.

    vim /etc/hosts
  5. Find the localhost string and append the new hostname. Again, replace new-hostname with your hostname.

    127.0.0.1 localhost new-hostname
  6. Save and exit the vim editor by pressing Shift + : (colon) to open a new command entry box in the vim editor. Type wq and then press Enter to save changes and exit the vim editor.

    SLES 11 (additional step for this OS version only)

    Use the vim editor to update the /etc/HOSTNAME file with the new hostname.

    vim /etc/HOSTNAME

    Find the current hostname string and replace it with the new hostname.

    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.

  7. Use the vim editor to update the /etc/cloud/cloud.cfg file on your SLES Linux instance.

    vim /etc/cloud/cloud.cfg
  8. Find the preserve_hostname string and change the default setting to true so that the hostname is preserved between restarts or reboots.

    preserve_hostname: true
  9. Save and exit the vim editor by pressing Shift + : (colon) to open a new command entry box in the vim editor. Type wq and then press Enter to save changes and exit the vim editor.

  10. Reboot the instance.

    sudo reboot
  11. Connect to your EC2 instance, and then run the Linux hostname command without any parameters to verify that the hostname change persisted.

    hostname

    The command returns the new hostname.

Related information

Change the hostname of your Amazon Linux instance

AWS OFFICIAL
AWS OFFICIALUpdated 3 years ago