Google Compute Engine: how to set hostname permanently?

前端 未结 14 1577
萌比男神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条回答
  •  梦毁少年i
    2020-12-29 06:59

    Tested on Debian.

    The dhclient sets the hostname using DHCP

    You can override this by creating a custom hook script in /etc/dhcp/dhclient-exit-hooks.d/custom_set_hostname that would read the hostname from /etc/hostname:

    if [ -f "/etc/hostname" ]; then
        new_host_name=$(cat /etc/hostname)
    fi
    

    The script must have the execute permission.

    It's important to set the new_host_name variable and not calling the hostname command directly as any call to the hostname command will be overriden by another hook or the dhclient-script which uses this variable

提交回复
热议问题