Google Compute Engine: how to set hostname permanently?

天涯浪子 提交于 2019-12-18 11:50:03

问题


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?


回答1:


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




回答2:


Edit rc.local

sudo nano /etc/rc.local 

Add your line under the rest:

hostname *your.hostname.com*

Make sure to run the following after for the script to be executed

chmod +x /etc/rc.d/rc.local

Reboot, and profit.




回答3:


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




回答4:


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.




回答5:


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



回答6:


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!




回答7:


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)




回答8:


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.




回答9:


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.




回答10:


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




回答11:


When creating a VM, you can specify a custom FQDN hostname as an optional parameter. This feature is currently in Beta.

$ gcloud beta compute instances create INSTANCE_NAME --hostname example.hostname

This should work across OSes, and prevent the need for workaround scripts. More info in the docs.

-- Sirui (Product Manager, Google Compute Engine)




回答12:


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.




回答13:


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!



来源:https://stackoverflow.com/questions/25408612/google-compute-engine-how-to-set-hostname-permanently

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!