cloud-init hostname not working as expected [RHEL8/Rocky Linux/Centos 8]

isitar

New Member
Aug 26, 2021
3
0
1
29
Hello

I've searched the other topics but there doesn't seem to be a definitive answer and as far as i saw, noone mentioned the fqdn entry.

i've configured a cloud-init ready image on rocky linux 8 (rhel8 clone)
the network, public key and user work fine. when I mount the /dev/sr0 and have a look at the cloud init files, the hostname is set aswell:

YAML:
[root@judihui sr0]
$ cat user-data

#cloud-config
hostname: SRVRLRCLB01
manage_etc_hosts: true
user: root
disable_root: False
password: VALUE HIDDEN
ssh_authorized_keys:
  - ssh-rsa VALUE HIDDEN

chpasswd:
  expire: False
package_upgrade: true


as far as i can see, the hostname is set correctly (the name i gave the vm).
there is nothing special in the network-data and meta-data

but when i start the vm, the hostname is not really the hostname.
it just keeps whatever was set before. when I change the hostname (hostnamectl set-hostname XYZ) then it persists...

what i found was in the /var/lib/cloud/data/set-hostname

YAML:
{
 "fqdn": "XYZ",
 "hostname": "SRVRLRCLB01"
}

but I have no idea where this XYZ is saved..

I tried putting the following lines in /etc/cloud/config.cfg
YAML:
preserve_hostname: false
prefer_fqdn_over_hostname: false

the hostname variable is definitly set correct as it gets applied to the /etc/hosts file (this is done via the /etc/cloud/tempaltes/hosts.rhel.tmpl template)

so my question is: is there a way to find out where this fqdn entry comes from?
can we somehow change the behavior to set the fqdn instead of hostname / extend the config in the ui to change that?
 
I found a solution :)

if you change the vm name to something like SRVRLRCLB01.mydomain.ch then the fqdn gets set and the hostname is correct.
 
Sorry to dig this up again...
I just tested this and found the same behavior with a Centos 8.4 cloud image, Ubuntu cloud image works fine.
If I just use a hostname as the VM name, the Centos VM hostname ends up as "localhost". If I use a FQDN as the VM name, then the hostname gets set properly. Although /etc/hostname has the FQDN as opposed to just the hostname.
So would this be a bug with proxmox cloud-init or with the Centos cloud-init? And who should fix it?
 
Last edited:
Are you using custom user-data? If you are, then you also need custom metadata.
For example:
Code:
qm config 2000|grep ci
cicustom: user=bbnas:snippets/centos.user,meta=bbnas:snippets/centos.meta.2000

The VM is based on Centos8:
Code:
importing disk '/mnt/pve/bbnas/template/iso/CentOS-8-GenericCloud-8.4.2105-20210603.0.x86_64.qcow2' to VM 2000 ...
  Logical volume "vm-2000-disk-0" created.

Code:
more /mnt/pve/bbnas/snippets/centos.meta.2000
{
"instance-id": "2000",
"local-hostname": "vm2000"
}

VM boots with hostname properly set:
Code:
[  OK  ] Started Execute cloud user/final scripts.
[  OK  ] Reached target Cloud-init target.

CentOS Linux 8
Kernel 4.18.0-305.3.1.el8.x86_64 on an x86_64

Activate the web console with: systemctl enable --now cockpit.socket

vm2000 login

When using PVE generated cloud-init, the meta-data file does not include hostname, and even though the hostname is set initially properly, it is then changed to localhost according to log:
Code:
 Updating hostname to localhost (vm2000)
. This could be artifact of a version of cloud-init that Centos is using, or default options they adopted.
I suggest opening a bugzilla to track this for a more universal cloud image support.


Blockbridge : Ultra low latency all-NVME shared storage for Proxmox - https://www.blockbridge.com/proxmox
 
I can confirm this seems to be an issue still with rocky 9.4. Using the FQDN as the hostname seems to "resolve" this for me for now.