Set hostname using cloud-init

cloud-initubuntu-18.04

I recently tried changing an Ubuntu machine hostname.
After a reboot, I found the name was reverted to it's previous one.

A quick search suggested this was happening because of cloud-init.
If I change the setting preserve_hostname to true, the hostname will persist.

However I found that system updates can possible cause the setting to revert back to false resulting in the hostname reverting as well.

I would like to know if it is possible to change the hostname permanently without any change to cloud-init settings. In other words, where does cloud-init save the original hostname and how to change it?

Best Answer

Write something like below, change the permission so content will be persistent.

write_files:
  - path: /<your path>/hostname.cfg
    permissions: '<your permission>'
    content: |
      preserve_hostname: true

Other way will be make your host file immutable after first time creation.

Related Topic