0

When you create new instance like "my-instance-1", this mean your instance Host name will be also "my-instance-1" !.

The problem is when you change the host name for example to "myhostname.mydomain.net", every time you use the Google SSH browser tool, or upgrade the instance, the host name will be automatically changed to "my-instance-1" !.

Why Google SSH browser tool change instance hostname ?

8
  • They're ephemeral containers, why would you expect it to be static? Feb 8, 2018 at 0:36
  • @Jacob Evans I don't understand exactly what you mean, but "my-instance-1" its not a valid host name !
    – Saif
    Feb 8, 2018 at 2:58
  • Sorry I meant ephemeral. They aren't persistent Feb 8, 2018 at 2:59
  • 1
    Yes Jacob, you are right, ""hostname" is part of the default metadata entries and it is not possible to manually edit any of the default metadata pairs." : stackoverflow.com/questions/25408612/…
    – Saif
    Feb 8, 2018 at 3:12
  • 1
    @Kamran, Thank you for all informations, after creating sh file on /etc/dhcp/dhclient-exit-hooks.d, and make it executable, the hostname persist.
    – Saif
    Feb 8, 2018 at 23:20

2 Answers 2

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 exactly the same place in Debian/Ubuntu VMs, but of course I don't run any of those.

1
  • Thank you Michael Hampton, i remove the file, then set the hostname by hostnamectl set-hostname myhostname.net
    – Saif
    Feb 8, 2018 at 23:24
0

You can specify your own hostnames in your VM instances.

There are 2 easy ways.

  1. Create a Custom Metadata entry hostname with value my.hostname.com

Then I call it on my centos servers like this :

hostname $(curl --silent "http://metadata.google.internal/computeMetadata/v1/instance/attributes/hostname" -H "Metadata-Flavor: Google")

Result is hostname set as per your meta data.

  1. Through the Google DHCP service that assigns your static internal IP's

I create a file "google_hostname.sh" in "/etc/dhcp/dhclient.d" (Using CentOS) Content of the file looks like this :

#!/bin/bash
google_hostname_config() {
  google_set_hostname
}
google_hostname_restore() {
  :
}

The result is, upon Google assigning your ephemeral internal IP upon a reboot it will also do the hostname.

More info: 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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .