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

5

7

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?

Adamec answered 30/6, 2016 at 3:14 Comment(0)
A
-10

My really nasty hack-around is to create a cron job that sets the hostname every minute. Improved suggestions appreciated.

Adamec answered 11/7, 2016 at 23:55 Comment(0)
S
7

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.

Stomacher answered 6/4, 2020 at 16:38 Comment(1)
aws-doc: docs.aws.amazon.com/AWSEC2/latest/UserGuide/set-hostname.htmlOstrogoth
J
3

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

Be sure to use the full command: sudo hostnamectl set-hostname --static <persistent_host_name>

Joycejoycelin answered 15/1, 2018 at 20:32 Comment(0)
S
0

Try this:

$ sudo vi /etc/cloud/cloud.cfg

and then comment out 'set_hostname', 'update-hostname', and 'update_etc_hosts' as you require.

Squelch answered 13/7, 2016 at 19:28 Comment(1)
None of those are enabled options in that file. Furthermore, a comment at the top says WARNING: Modifications to this file may be overridden by files in /etc/cloud/cloud.cfg.d. So, as I said in my original post, I commented out set-hostname and update-hostname in /etc/cloud/cloud.cfg.d/00_defaults.cfg. I also commented out update-etc-hosts, as you suggested, but it made no difference and my hostname is still overwritten.Adamec
J
-1

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

sudo hostnamectl set-hostname my.persistent.hostname
Jankey answered 1/5, 2020 at 11:11 Comment(0)
A
-10

My really nasty hack-around is to create a cron job that sets the hostname every minute. Improved suggestions appreciated.

Adamec answered 11/7, 2016 at 23:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.