Google Compute Engine: how to set hostname permanently?

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

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

    That isn't possible. Please take a look at this answer. The following article explains that the "hostname" is part of the default metadata entries and it is not possible to manually edit any of the default metadata pairs. As such, you would need to use a script or something else to change the hostname every time the system restarts, otherwise it will automatically get re-synced with the metadata server on every reboot.

    You can find information on startup scripts for GCE in this article. You can visit this one for info on how to apply the script to an instance.

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

    Need to remove file /etc/dhcp/dhclient.d/google_hostname.sh

    rm -rf /etc/dhcp/dhclient.d/google_hostname.sh
    rm -rf /etc/dhcp/dhclient-exit-hooks.d/google_set_hostname
    
    0 讨论(0)
  • 2020-12-29 07:08

    Non-cron/metadata/script solution.

    Edit /etc/dhclient-(network-interface).conf or create one if it doesn't exist.

    Example:

    sudo nano /etc/dhclient-eth0.conf
    

    Then add the following line, replacing the desired FQDN between the double quotes:

    supersede host-name "hostname.domain-name";
    

    Persists between reboots and hostname and hostname -f works as intended.

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

    There is some hack you can do to achieve this as i did. Just do:

    sudo chattr +i /etc/hosts
    

    This command actually makes the file "(i)mmutable", which means even root can't change it (unless root does chattr -i /etc/hosts first, of course).

    As above, you can undo this with sudo chattr -i /etc/hosts

    Cheer!

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

    If anyone finds this solution does not work for them on GCS instance. Then I suggest you try using exit hooks as described by Google Support.

    In fact, some distributions of Linux like CentOS and Debian use dhclient-script script to configure the network parameters of the machine. This script is invoked from time to time by dhclient which is dynamic host configuration protocol client and provides a means for configuring one or more network interfaces using the DHCP protocol, BOOTP protocol, or if these protocols fail, by statically assigning an address.

    The following text is a quote from the man (manual) page of dhclient-script:

    After all processing has completed, /usr/sbin/dhclient-script checks for the presence of an executable /etc/dhcp/dhclient-exit-hooks script, which if present is invoked using the ´.´ command. The exit status of dhclient-script will be passed to dhclient-exit-hooks in the exit_status shell variable, and will always be zero if the script succeeded at the task for which it was invoked. The rest of the environment as described previ‐ ously for dhclient-enter-hooks is also present. The /etc/dhcp/dhclient-exit-hooks script can modify the valid of exit_status to change the exit status of dhclient-script.

    That being said, by taking a look into the code snippet of dhclient-script, we can see the script checks for the existence of an executable /etc/dhcp/dhclient-up-hooks script and all scripts in /etc/dhcp/dhclient-exit-hooks.d/ directory.

    ETCDIR="/etc/dhcp"
    
    193 exit_with_hooks() {
    194     exit_status="${1}"
    195 
    196     if [ -x ${ETCDIR}/dhclient-exit-hooks ]; then
    197         . ${ETCDIR}/dhclient-exit-hooks
    198     fi
    199 
    200     if [ -d ${ETCDIR}/dhclient-exit-hooks.d ]; then
    201         for f in ${ETCDIR}/dhclient-exit-hooks.d/*.sh ; do
    202             if [ -x ${f} ]; then
    203                 . ${f}204             fi
    205         done
    206     fi
    207     
    208     exit ${exit_status}209 }
    

    Therefore, in order to modify the hostname of your Linux VM you can create a custom script with .sh extension and place it in /etc/dhcp/dhclient-exit-hooks.d/ directory. If this directory does not exist, you can create it. The content of the custom script will be:

    hostname YourFQDN.sh
    

    >

    be sure to make this new .sh file executable:

     chmod +x YourFQDN.sh
    

    Source: (https://groups.google.com/d/msg/gce-discussion/olG_nXZ-Jaw/Y9HMl4mlBwAJ)

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