GoogleCloudPlatform / guest-configs

Apache License 2.0
31 stars 40 forks source link

google_set_hostname does hot change static hostname (via hostnamectl) #12

Closed kladiv closed 3 years ago

kladiv commented 3 years ago

Hello, we found an uncorrect behaviour on new deployed VM Instances from Images. google_set_hostname does not change static hostname (via hostnamectl if present). After PR https://github.com/GoogleCloudPlatform/compute-image-packages/pull/544 the hostnamectl code block has beed removed.

We tested on RHEL7 (but the same in CentOS7, CentOS8, RHEL8) with no NetworkManager.

]# hostnamectl status
   Static hostname: tim-rhel7-model-image-20201001  <---- Image name (not changed)
Transient hostname: rhel7clus1
         Icon name: computer-vm
           Chassis: vm
        Machine ID: a1ff429dd4bea8e93ed495f541bda2e0
           Boot ID: ef036d7d10594f98ac642e3b73949a1b
    Virtualization: kvm
  Operating System: Red Hat Enterprise Linux
       CPE OS Name: cpe:/o:redhat:enterprise_linux:7.9:GA:server
            Kernel: Linux 3.10.0-1160.6.1.el7.x86_64
      Architecture: x86-64

Static hostname (/etc/hostname) is not changed after VM creation from Image.

In my opinion code below should be added

  if [ `command -v hostnamectl` ]
  then
    hostnamectl set-hostname "$new_host_name"
  fi

Another question: in SLES12, SLES15 there's not google_set_hostname (and google-compute-engine repo/packages). Is it correct? Thank you.

Best Regards, Claudio

hopkiw commented 3 years ago

Just as some background, this script is for Google-created images, so e.g. "RHEL7 with no NetworkManager" is not the environment we target, as our RHEL7 does have NetworkManager.

In Linux, the hostname is a runtime value maintained by the kernel. Regardless of the presence or contents of any configuration values, the source of truth is always the value stored in memory by the kernel, which can be obtained by gethostname(2) function or the hostname(1) utility. The /etc/hostname file is historically used during startup to set the in-memory value, but since GCE VM images may be used on multiple instances and the source of the hostname is really stored in GCE, we have implemented this dynamic hostname setting script instead. That is, you can ignore /etc/hostname, and any software which attempts to read this file to get the current hostname is in error, and should be updated to one of the mechanisms listed.

This package is installed on SLES and called google-guest-configs, but the SLES version of this package does not contain the scripts under discussion, as SLES uses wicd which does not use the dhclient-script or dhclient-exit-hooks mechanisms. Hostnames on SLES are set via wicd via the DHCP response, and the helper scripts therefore don't exist.

shaharmor commented 3 years ago

This happens to us as well when using custom Ubuntu 16.04 images (Which were built upon GCE ubuntu-1604-lts).

When creating a new VM from the custom image, the /etc/hostname file is not updated and still uses the name of the VM used to create the image (Or even the VM before it?)

hopkiw commented 3 years ago

as said above, you can ignore /etc/hostname. what matters is the result of the hostname command. if it is returning the wrong value, you should file a customer support case. or if you want to investigate yourself and think you found a bug, file it here.