Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Compute Engine: how to set hostname permanently?

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?

like image 863
Michael Schmidt Avatar asked Aug 20 '14 15:08

Michael Schmidt


People also ask

What is hostname in GCP?

When you create a virtual machine (VM) instance, Google Cloud creates an internal DNS name from the VM name. Unless you specify a custom hostname, Google Cloud uses the automatically created internal DNS name as the hostname it provides to the VM.

How do I point my domain to Google Compute Engine?

In the Google Cloud console, go to the Custom Domains tab of the App Engine Settings page. Click Add a custom domain. If your domain is already verified, the domain appears in the Select the domain you want to use section. Select the domain from the drop-down menu and click Continue.

Is Google Compute Engine free forever?

The free usage limit does not expire, but is subject to change. Available for eligible customers. Scalable, high-performance virtual machines. Best-in-class performance, reliability, and pricing for all your storage needs.

Can a persistent disk be attached to multiple Google Compute Engine instance?

From Google's documentation: It is possible to attach a persistent disk to more than one instance. However, if you attach a persistent disk to multiple instances, all instances must attach the persistent disk in read-only mode. It is not possible to attach the persistent disk to multiple instances in read-write mode.


2 Answers

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

like image 106
Adrián Avatar answered Sep 16 '22 23:09

Adrián


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
like image 37
Sergey Safarov Avatar answered Sep 17 '22 23:09

Sergey Safarov