Google Cloud Platform Changes Hostname in Rocky Linux
Introduction
Google Cloud Platform Rocky Linux VMs experience hostname behavior changes where the system hostname reverts from FQDN to short name after reboot or NetworkManager restart. This issue is specific to versions running on GCP using the official rocky-linux-8-optimized-gcp
image.
Problem
When setting a hostname to an FQDN using hostnamectl set-hostname test.domain.com
, the hostname correctly shows the FQDN initially. However, after a reboot or NetworkManager restart, the hostname reverts to just the short name portion.
Symptoms
The following behavior is observed:
# hostnamectl set-hostname test.domain.com
# hostname
test.domain.com
# reboot
# hostname
test
NetworkManager logs show hostname changes occurring:
NetworkManager: hostname: static hostname changed from "test.domain.com" to "test"
NetworkManager: policy: set-hostname: set hostname to 'test' (from system configuration)
Note that hostname -f
still returns the FQDN correctly, but the basic hostname
command only returns the short name.
Resolution
This issue is caused by a change in Google's guest configuration scripts. This new NetworkManager dispatch script is located at:
/etc/NetworkManager/dispatcher.d/google_hostname.sh
The dispatcher script contains the following:
# Ensure that the hostname and IP address are set only for the primary NIC.
new_ip_address=$(jq -r .networkInterfaces[0].ip <<< $instance) new_host_name=$(jq -r .hostname <<< $instance) google_set_hostname
google_set_hostname
is the script that sets the hostname to the shortened, unqualified domain name, version of the current system hostname. The NetworkManager dispatcher is a system service that executes custom scripts in response to network state changes. In this case, the script runs if [[ $2 == "up" ]];
which runs the google_set_hostname
any time a network interface comes online.
References & related articles
Google Cloud Guest Configs Repository
NetworkManager Dispatcher Documentation