20

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?

14 Answers 14

20

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

4
  • Appears to be a viable solution. But I settled with the advice not to change the hostname, because it is not necessary. All outside appearing hostnames can/should be set in the respective applications. Aug 29, 2014 at 13:42
  • 2
    Nice solution @Adrián, thanks! But doesn't work for me. It seems that google sets the hostname after the startup pf the cron daemon... Oct 8, 2015 at 7:27
  • 1
    Inspired by this solution, I wrote a patch that preserves your hostname from overwriting from Google. stackoverflow.com/a/38344684/1325832
    – tacke
    Jul 13, 2016 at 7:12
  • 2
    This did not work for me - another solution here did: stackoverflow.com/a/53607475/8177355 Feb 2, 2019 at 21:19
14

You need to remove the 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

It's worth noting that this script is needed in order to run gcloud beta compute instances create with the --hostname flag. If this script is absent on a base image, new VM instances will preserve the source hostname/FQDN!

2
  • This simply fix the problem Nov 16, 2018 at 3:06
  • Exactly what I was looking for.
    – Samit
    Jul 14, 2019 at 12:42
12

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.

2
  • This does not work - the hostname is not resolvable outside of that one machine.
    – kliew
    Nov 27, 2016 at 21:33
  • Setting the hostname is intended to be local only; changing DNS is a different question.
    – Dan Pritts
    Feb 5, 2019 at 16:28
5

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.

2
  • I don't mind to use a script to set the hostname at boot time. BUt don't know, how to fill in metadata to which script. Aug 20, 2014 at 17:26
  • You can visit developers.google.com/compute/docs/howtos/startupscript for the article on 'startup scripts'. I will also update my answer to include this information. This way it will run every time at startup. When you're specifying which script to run, you can use local file, a file from Google Cloud Storage or a URL. You can visit developers.google.com/compute/docs/… for information on how to update the metadata for your instance in order to apply a startup script.
    – Boyan
    Aug 20, 2014 at 18:31
4

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
3

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!

2

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)

1

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.

2
  • I think Adrián's solution is good when you want to change the hostname by just changing metadata of your instance. You can clone the server, change the metadata and it will work. Otherwise you need to log into the server, edit the file, change the hostname, etc.
    – dgaviola
    Sep 29, 2015 at 14:50
  • Perhaps this will help someone; the reason this is not viable is because GCE resets the hostname every reboot, unless one of the solutions described above is used. (script, cron)
    – Adam John
    May 17, 2018 at 21:48
1

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.

2
  • In Centos7 working but if added "something.com" then only something is showing on hostname, Period is skipping Dec 28, 2017 at 6:06
  • That's the expected behavior, to display something.com, "-f" argument is required to hostname command, as in: hostname -f Jan 2, 2018 at 12:05
1

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

1

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)

0

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

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

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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.