Warm tip: This article is reproduced from serverfault.com, please click

Google Compute Engine: how to set hostname permanently?

发布于 2014-08-20 15:23:01

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.n), but that did not do the job. But it should work via metadata (https://github.com/GoogleCloudPlatform/compute-image-packages/tree/master/google-startup-scripts).

Anybody an idea?

Questioner
Michael Schmidt
Viewed
0
Adrián 2014-08-28 23:41:34

The most simple way to achieve it is creating a simple script and that's what I have done.

I have stored the hostname in the instance metadata and then I retrieve it every time the system restarts in order to set the hostname using a cron job.

$ gcloud compute instances add-metadata <instance> --metadata hostname=<new_hostname> 
$ sudo crontab -e

And this is the line that must be appended in crontab

@reboot hostname $(curl --silent "http://metadata.google.internal/computeMetadata/v1/instance/attributes/hostname" -H "Metadata-Flavor: Google")

After these steps, every time you restart your instance it will have the hostname <new_hostname>. You can check it in the prompt or with the command: hostname