To resolve this issue what we want to do is go into your Compute Engine overview page, select the instance we want to fix the hostname for, we must create a new entry under the “custom metadata” area, so click “edit” the instance. Then in the control panel add a key value “hostname” and in the second field add the actual hostname you want. I’m using gio.desantolo.com in my example.
Once you click save, start up your machine. Don’t worry if the management page for this instance does not show the custom metadata listed, apparently there is a delay and it takes awhile.
We’re now booting into the instance/VM we just edited and want to SSH into it. Let’s query the metadata we just created to see if we can apply my fix.
SSH into the instance and run the following command in shell:
curl --silent "http://metadata.google.internal/computeMetadata/v1/instance/attributes/hostname" -H "Metadata-Flavor: Google"
After running this command, it should return the custom metadata hostname we just set. If you see the hostname you want being returned after running this command we can move forward.
Now we want to sudo into root and enter crontab editor (run “crontab -e”) add the following to the bottom of your crontab:
@reboot hostname $(curl --silent "http://metadata.google.internal/computeMetadata/v1/instance/attributes/hostname" -H "Metadata-Flavor: Google")
Now reboot your instance and your hostname should be set to the “custom metadata” we created.
2 responses to “Overriding Google Compute Engine hostname from getting reset”