Google Compute Engine: how to set hostname permanently?

前端 未结 14 1583
萌比男神i
萌比男神i 2020-12-29 06:32

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

14条回答
  •  爱一瞬间的悲伤
    2020-12-29 07:11

    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  --metadata 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 . You can check it in the prompt or with the command: hostname

提交回复
热议问题