Set Server Hostname in EC2|OpenStack|DigitalOcean|Azure Instance

Posted on 311 views

How can I change Linux Server hostname permanently in AWS EC2 / OpenStack / DigitalOcean / Azure 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: Change Linux Server hostname in AWS EC2|OpenStack|DigitalOcean|Azure

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.computingpost.com

sudo hostnamectl set-hostname fed31.computingpost.com

Let’s confirm a new hostname.

$ hostnamectl 
   Static hostname: fed31.computingpost.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.computingpost.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

change-openstack-aws-server-hostname-1024x538

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

coffee

Gravatar Image
A systems engineer with excellent skills in systems administration, cloud computing, systems deployment, virtualization, containers, and a certified ethical hacker.