As we can see, changing the Hostname of a Linux Machine is just a simple task,
with the help of one single command i.e $hostname name_youwant.
But in AWS if you use this command $hostname name_youwant it will change the host name and it is not persistent.when you reboot the instance, the hostname reverts to its former name.
So to make the hostname persistent we follow some steps as below:
Step1: Run the command hostnamectl in your linux terminal (Centos/REHL)
sudo hostnamectl set-hostname --static persistent-hostname
Replace the “persistent-hostname” with the name you want for your instance
Note: Changing the hostname by using the hostnamectl command doesn’t update the/etc/hosts file. So you can change it manually.
Step2: open the “cloud.cfg” in “/etc/cloud/cloud.cfg” and update it
sudo vi /etc/cloud/cloud.cfg
add the following line at the end of this above file
preserve_hostname: true
save and exit
Step3: Reboot the server
sudo reboot
Step4: Check the hostname
hostname
Congratulations…. 🙂
You follow all the steps and set hostname that is persistent to your ec2-instance.
Please subscribe www.cloudsbaba.com for more content and updates.