How do I assign a static hostname to a private EC2 instance running SLES?

Last updated: 2020-02-20

I changed the hostname of my private Amazon Elastic Compute Cloud (Amazon EC2) instance. But when I reboot, or I stop and then restart the instance, the new hostname reverts to the previously assigned hostname. How do I fix that?

Short Description

EC2 instance hostnames are derived from the IP address that is dynamically assigned to the instance at startup. When you change the hostname of a private instance of EC2 Linux by using the hostname command, the hostname doesn't persist if you reboot, or stop and then restart the instance. If you want the new hostname to persist between instance stops and starts, or reboots, add the new hostname to the appropriate configuration files on your EC2 Linux instance.

Resolution

1.    Connect to your EC2 Linux instance using SSH. For more information, see Connecting to Your Linux Instance Using SSH.

2.    Switch to the root user.

sudo su

3.    Either use the YaST interface or manually update your SLES Linux instance with the new hostname.

YaST interface update

1.    Run the yast command to launch the YaST interface.

yast

2.    Navigate to Network Settings.

SLES 11:

Use the Tab and arrow keys to navigate to Network Devices, then to Network Settings, and then press Enter.

SLES 12 and SLES 15:

Use the Tab and arrow keys to navigate to System, then to Network Settings, and then press Enter.

3.    In Network Settings, use the arrow keys to navigate to Hostname/DNS and use Tab and the Shift + Tab combination to cycle through the available options.

4.    Set the hostname and domain name.

SLES 11 and SLES 12:

Select and set the hostname and domain name to the preferred values.

SLES 15:

Select and set the hostname to the preferred values.

5.    Set Change Hostname via DHCP value to no or clear the value.

SLES 11:

Navigate to Change Hostname via DHCP, and then press the Spacebar to clear this option.

SLES 12 and SLES 15:

Verify that the Set Hostname via DHCP option is set to no.

6.    Use the Tab key to navigate to [OK], and then press Enter to accept changes.

7.    Use the Tab key to navigate to [Quit], and then press Enter to exit YaST.

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

vim /etc/cloud/cloud.cfg

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

10.    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 vim.

11.    Reboot the instance.

sudo reboot

12.    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 should return the new hostname.

Manual update

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

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

vim /etc/hosts

3.    Find the localhost string and append the new hostname, again replacing new-hostname with your hostname.

127.0.0.1 localhost new-hostname

4.    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 vim.

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.

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

vim /etc/cloud/cloud.cfg

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

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

8.    Reboot the instance.

sudo reboot

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