Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I stop cloud-init from overwriting my hostname on AWS (CentOS)

I have a script that runs at boot (via rc.local) and sets my hostname based on the instance name it queries from the AWS CLI. It works, and at the end of the boot log it says " login:"

At figured that cloud-init was overwriting my hostname, so I:

  • set preserve_hostname: true in /etc/cloud/cloud.cfg
  • commented out set-hostname and update-hostname in the cloud_config_modules section of /etc/cloud/cloud.cfg.d/00_defaults.cfg

But in the cloud-init logs I still see "Running module update-hostname" and " Migrated semaphore update_hostname.always to config-update-hostname with frequency always", and a minute or two after boot the hostname returns to the AWS default.

What is going on? How do I stop it?

like image 861
CAJ Avatar asked Jun 30 '16 03:06

CAJ


3 Answers

I know this is an old post, but I found it useful to know.

In the /etc/cloud/cloud.cfg file, add hostname: <fqdn/hostname> just below preserve_hostname: <true/false>:

# This will cause the set+update hostname module to not operate (if true)
preserve_hostname: false
hostname: myhostname

Source: https://cloudinit.readthedocs.io/en/latest/topics/modules.html#set-hostname.

like image 188
Daniel Medrano Avatar answered Oct 27 '22 11:10

Daniel Medrano


https://aws.amazon.com/premiumsupport/knowledge-center/linux-static-hostname-rhel7-centos7/

Be sure to use the full command "sudo hostnamectl set-hostname --static "

like image 40
Drew Avatar answered Oct 27 '22 09:10

Drew


Slightly different for Ubuntu: https://aws.amazon.com/premiumsupport/knowledge-center/linux-static-hostname/

sudo hostnamectl set-hostname my.persistent.hostname
like image 36
River Rock Avatar answered Oct 27 '22 11:10

River Rock