How can I change Instance / VM hostname in AWS / OpenStack / DigitalOcean Cloud platform. When you create an instance in AWS or OpenStack cloud, the hostname is generated based on the name provided and virtualization environment it runs on. For AWS, the private IP address is usually used as part of assigned hostname.

In this guide, we will see how you can change an instance or VM hostname to persist reboots. Follow these few steps to change or set an instance / VM hostname in AWS, OpenStack & DigitalOcean cloud environments.

Step 1: Set the VM hostname in AWS / OpenStack / DigitalOcean

I’m doing this in a system with Systemd, so hostnamectl command can be used for this purpose.

This is my current hostname:

$ hostnamectl 
   Static hostname: fed31.novalocal
         Icon name: computer-vm
           Chassis: vm
        Machine ID: d3ae97713a95466da1ef656ee2bb8456
           Boot ID: b8a8cfa70d78455b835269c88216c0a0
    Virtualization: kvm
  Operating System: Fedora 31 (Cloud Edition)
       CPE OS Name: cpe:/o:fedoraproject:fedora:31
            Kernel: Linux 5.3.7-301.fc31.x86_64
      Architecture: x86-64

I’ll change the hostname from fed31.novalocal to fed31.computingforgeeks.com

sudo hostnamectl set-hostname fed31.computingforgeeks.com

Let’s confirm a new hostname.

$ hostnamectl 
   Static hostname: fed31.computingforgeeks.com
         Icon name: computer-vm
           Chassis: vm
        Machine ID: d3ae97713a95466da1ef656ee2bb8456
           Boot ID: b8a8cfa70d78455b835269c88216c0a0
    Virtualization: kvm
  Operating System: Fedora 31 (Cloud Edition)
       CPE OS Name: cpe:/o:fedoraproject:fedora:31
            Kernel: Linux 5.3.7-301.fc31.x86_64
      Architecture: x86-64

$ cat /etc/hostname 
fed31.computingforgeeks.com

Step 2: Modify Cloud-init configuration

If you reboot a server, the hostname will reset to auto generated name configured by Cloud-init. Cloud-init is the industry standard multi-distribution method for cross-platform cloud instance initialization.

Open the /etc/cloud/cloud.cfg file for editing.

sudo vim /etc/cloud/cloud.cfg

Update the preserve_hostname line by setting it to true or adding one if it doesn’t exist.

preserve_hostname: true

See below

Reboot your server to confirm the hostname is persistent.

sudo reboot

Recheck your server hostname.

$ uptime 
 06:57:02 up 0 min,  1 user,  load average: 0.13, 0.03, 0.01

$ hostname
fed31.computingforgeeks.com

There you have it. In this article, you learned how to change / update a server hostname in OpenStack, AWS & DigitalOcean cloud platform. The same procedure should apply to other Cloud Platforms using Cloud-init.

More guides:

Best Rated AWS Cloud Certifications Preparation Books

Build AWS EC2 Machine Images (AMI) With Packer and Ansible

How to reset / change IAM user password on AWS

How To Create CentOS 8 KVM Image Template on OpenStack

How To Assign a Floating IP Address to an Instance in OpenStack

How To Create OpenStack Instance With a Fixed / Static IP Address