Google Compute Engine: how to set hostname permanently?

前端 未结 14 1515
萌比男神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 <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

    0 讨论(0)
  • 2020-12-29 07:11

    Im not sure I understand Adrián's answer. It seems overly complex since you have to run a script each boot why not just use hostname?

    vi /etc/rc.local

    add:

    hostname your_hostname

    thats it. tested and working. no need to fiddle with metadata and such.

    0 讨论(0)
  • 2020-12-29 07:17

    You can also create a simple startup-script to do the jobs:

    $ gcloud compute instances add-metadata <instance-name> --zone <instance-zone> --metadata startup-script='#! /bin/bash 
    hostname <hostname>'
    

    Notice that if you already have a startup-script you need to add to the existing startup-script below command or you will replace all the startup-script:

    $ hostname instance-name
    
    0 讨论(0)
  • 2020-12-29 07:18

    An easy way to fix this is to set up a startup script with custom metadata.

    Key :startup-script 
    Value: 
    #! /bin/bash 
    hostname <desired hostname>
    
    0 讨论(0)
  • 2020-12-29 07:20

    In my CentOS VMs I found that the script /etc/dhcp/dhclient.d/google_hostname.sh, installed by the google-compute-engine RPM, actually changed the hostname. This happens when the instance gets its IP address during boot.

    While it's not the long-term solution I really want, for now I simply deleted this script. The hostname I set with hostnamectl now persists after a reboot.

    The script is likely to be in exactly the same place in Debian/Ubuntu VMs, but of course I don't run any of those.

    0 讨论(0)
  • 2020-12-29 07:22

    I was lucky to set hostname at GCE running CentOS. Source: desantolo.com

    1. Click EDIT on your instance
    2. Go to "Custom metadata" section
    3. Add hostname + your.hostname.tld (change "your.hostname.tld" to your actual hostname
    4. run curl --silent "http://metadata.google.internal/computeMetadata/v1/instance/attributes/hostname" -H "Metadata-Flavor: Google"
    5. run sudo env EDITOR=nano crontab -e to edit crontab
    6. add line @reboot hostname $(curl --silent "http://metadata.google.internal/computeMetadata/v1/instance/attributes/hostname" -H "Metadata-Flavor: Google")
    7. On your keyboard Ctrl + X
    8. On your keyboard hit Y
    9. On your keyboard hit Enter
    10. run reboot
    11. after system rebooted, run hostname and see if your changes applied

    Good luck!

    0 讨论(0)
提交回复
热议问题