Google Compute Engine:如何永久设置主机名? [英] Google Compute Engine: how to set hostname permanently?

查看:121
本文介绍了Google Compute Engine:如何永久设置主机名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在GCE中永久设置实例的主机名?我可以通过主机名进行设置,但是重启后它又消失了.

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.

我试图输入元数据(主机名:f.q.d.n),但这没有完成.但是它应该通过元数据( https://github.com来工作. com/GoogleCloudPlatform/compute-image-packages/tree/master/google-startup-scripts ).

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).

有人知道吗?

推荐答案

最简单的方法是创建一个简单的脚本,这就是我所做的.

The most simple way to achieve it is creating a simple script and that's what I have done.

我已将主机名存储在实例元数据中,然后每次系统重新启动时都检索它,以便使用cron作业设置主机名.

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

这是必须在crontab中添加的行

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")

完成这些步骤后,每次重新启动实例时,实例的主机名都将为<new_hostname>. 您可以在提示符下或使用以下命令检查它:hostname

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

这篇关于Google Compute Engine:如何永久设置主机名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆