How do I set the hostname of an instance in GCE permanently? I can set it via hostname,but after reboot it is gone again.
I tried to feed in metadata (hostname:f.q.d
If anyone finds this solution does not work for them on GCS instance. Then I suggest you try using exit hooks as described by Google Support.
In fact, some distributions of Linux like CentOS and Debian use dhclient-script script to configure the network parameters of the machine. This script is invoked from time to time by dhclient which is dynamic host configuration protocol client and provides a means for configuring one or more network interfaces using the DHCP protocol, BOOTP protocol, or if these protocols fail, by statically assigning an address.
The following text is a quote from the man (manual) page of dhclient-script:
After all processing has completed, /usr/sbin/dhclient-script checks for the presence of an executable /etc/dhcp/dhclient-exit-hooks script, which if present is invoked using the ´.´ command. The exit status of dhclient-script will be passed to dhclient-exit-hooks in the exit_status shell variable, and will always be zero if the script succeeded at the task for which it was invoked. The rest of the environment as described previ‐ ously for dhclient-enter-hooks is also present. The /etc/dhcp/dhclient-exit-hooks script can modify the valid of exit_status to change the exit status of dhclient-script.
That being said, by taking a look into the code snippet of dhclient-script, we can see the script checks for the existence of an executable /etc/dhcp/dhclient-up-hooks script and all scripts in /etc/dhcp/dhclient-exit-hooks.d/ directory.
ETCDIR="/etc/dhcp" 193 exit_with_hooks() { 194 exit_status="${1}" 195 196 if [ -x ${ETCDIR}/dhclient-exit-hooks ]; then 197 . ${ETCDIR}/dhclient-exit-hooks 198 fi 199 200 if [ -d ${ETCDIR}/dhclient-exit-hooks.d ]; then 201 for f in ${ETCDIR}/dhclient-exit-hooks.d/*.sh ; do 202 if [ -x ${f} ]; then 203 . ${f}204 fi 205 done 206 fi 207 208 exit ${exit_status}209 }Therefore, in order to modify the hostname of your Linux VM you can create a custom script with .sh extension and place it in /etc/dhcp/dhclient-exit-hooks.d/ directory. If this directory does not exist, you can create it. The content of the custom script will be:
hostname YourFQDN.sh
>
be sure to make this new .sh file executable:
chmod +x YourFQDN.sh
Source: (https://groups.google.com/d/msg/gce-discussion/olG_nXZ-Jaw/Y9HMl4mlBwAJ)