创建虚拟机 (VM) 实例时, Google Cloud 会根据虚拟机名称创建内部 DNS 名称。除非您指定自定义主机名,否则 Google Cloud 会使用自动创建的内部 DNS 名称作为其提供给虚拟机的主机名。
您可以通过指定任何完全限定 DNS 名称来创建使用自定义主机名的虚拟机。自定义主机名有助于保持一致的命名惯例,或支持预期使用特定主机名的应用的要求。
即使您指定了自定义主机名, Google Cloud 也会创建 Compute Engine 内部 DNS 名称。您可以使用此自动创建的内部 DNS 记录连接到虚拟机。内部 DNS 记录解析为内部 DNS 名称,而不是自定义主机名。使用自定义主机名时,您仍然需要在适当的可用区中创建相应的 DNS 记录,例如使用 Cloud DNS。
准备工作
-
如果您尚未设置身份验证,请进行设置。身份验证是通过其进行身份验证以访问 Google Cloud 服务和 API 的过程。如需从本地开发环境运行代码或示例,您可以通过选择以下选项之一向 Compute Engine 进行身份验证:
Select the tab for how you plan to use the samples on this page:
Console
When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.
gcloud
-
After installing the Google Cloud CLI, initialize it by running the following command:
gcloud init
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
- Set a default region and zone.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
-
针对项目的
compute.instances.create
权限 -
使用自定义映像创建虚拟机:针对映像的
compute.images.useReadOnly
权限 -
使用快照创建虚拟机:针对快照的
compute.snapshots.useReadOnly
权限 -
使用实例模板创建虚拟机:针对实例模板的
compute.instanceTemplates.useReadOnly
权限 -
为虚拟机分配旧版网络:针对项目的
compute.networks.use
权限 -
为虚拟机指定静态 IP 地址:针对项目的
compute.addresses.use
权限 -
使用旧版网络时为虚拟机分配外部 IP 地址:针对项目的
compute.networks.useExternalIp
权限 -
为虚拟机指定子网:针对项目或所选子网的
compute.subnetworks.use
权限 -
在使用 VPC 网络时为虚拟机分配外部 IP 地址:针对项目或所选子网的
compute.subnetworks.useExternalIp
权限 -
为虚拟机设置虚拟机实例元数据:针对项目的
compute.instances.setMetadata
权限 -
为虚拟机设置标记:针对虚拟机的
compute.instances.setTags
权限 -
为虚拟机设置标签:针对虚拟机的
compute.instances.setLabels
权限 -
为虚拟机设置要使用的服务账号:针对虚拟机的
compute.instances.setServiceAccount
权限 -
为虚拟机创建新磁盘:针对项目的
compute.disks.create
权限 -
以只读或读写模式挂接现有磁盘:针对磁盘的
compute.disks.use
权限 -
以只读模式挂接现有磁盘:针对磁盘的
compute.disks.useReadOnly
权限
Terraform
如需在本地开发环境中使用本页面上的 Terraform 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭据设置应用默认凭据。
如需了解详情,请参阅 Set up authentication for a local development environment。
Go
如需在本地开发环境中使用本页面上的 Go 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭据设置应用默认凭据。
如需了解详情,请参阅 Set up authentication for a local development environment。 如需了解详情,请参阅身份验证文档中的为本地开发环境设置 ADC。
Java
如需在本地开发环境中使用本页面上的 Java 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭据设置应用默认凭据。
如需了解详情,请参阅 Set up authentication for a local development environment。 如需了解详情,请参阅身份验证文档中的为本地开发环境设置 ADC。
Node.js
如需在本地开发环境中使用本页面上的 Node.js 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭据设置应用默认凭据。
如需了解详情,请参阅 Set up authentication for a local development environment。 如需了解详情,请参阅身份验证文档中的为本地开发环境设置 ADC。
Python
如需在本地开发环境中使用本页面上的 Python 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭据设置应用默认凭据。
如需了解详情,请参阅 Set up authentication for a local development environment。 如需了解详情,请参阅身份验证文档中的为本地开发环境设置 ADC。
REST
如需在本地开发环境中使用本页面上的 REST API 示例,请使用您提供给 gcloud CLI 的凭据。
After installing the Google Cloud CLI, initialize it by running the following command:
gcloud init
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
如需了解详情,请参阅 Google Cloud 身份验证文档中的使用 REST 时进行身份验证。
所需的角色
如需获得创建具有自定义主机名的虚拟机所需的权限,请让您的管理员为您授予项目的 Compute Instance Admin (v1) (
roles/compute.instanceAdmin.v1
) IAM 角色。 如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限。此预定义角色可提供创建具有自定义主机名的虚拟机所需的权限。如需查看所需的确切权限,请展开所需权限部分:
所需权限
使用自定义主机名创建虚拟机需要以下权限:
限制
您必须手动配置自定义主机名的 DNS 记录。Compute Engine 内部 DNS 提供的自动创建的记录不解析自定义主机名。您可以使用以下任一方法来托管自定义主机名的 DNS 记录:
任何其他公开 DNS 域名服务器
创建虚拟机后,就无法更改自定义主机名。
支持的操作系统
使用与访客网络软件集成的钩子脚本在访客环境中设置自定义主机名。没有与客机网络软件集成的脚本的 Linux 操作系统可能没有正确的主机名。
如需查看支持自定义主机名的操作系统版本的完整列表,请参阅操作系统详细信息中的客户机环境支持。
命名惯例
自定义主机名必须符合 RFC 1035 要求才能成为有效主机名。为了满足这些要求,自定义主机名必须符合以下格式规范:
- 主机名至少包含两个标签,如下所示:
- 每个标签包含仅包含以下字符的正则表达式:
[a-z]([-a-z0-9]*[a-z0-9])?
。 - 标签使用点串联。
- 每个标签的长度为 1-63 个字符。
- 每个标签包含仅包含以下字符的正则表达式:
- 主机名不超过 253 个字符。
无效:包含一个标签
my-host1234
有效:包含三个使用串联的标签
my-host1234.example.com
使用自定义主机名创建虚拟机
控制台
在 Google Cloud 控制台中,前往创建实例页面。
为您的虚拟机指定名称。如需了解详情,请参阅资源命名惯例。
展开高级选项部分,然后执行以下操作:
- 展开网络部分。
- 在主机名字段中,指定自定义主机名。
根据需要进行其他虚拟机自定义。
要创建并启动该虚拟机,请点击创建。
下一步:配置 DNS 记录。如需了解详情,请参阅管理记录。
gcloud
使用 Google Cloud CLI,按照从映像创建实例或从快照创建实例的相同说明操作,添加
--hostname
标志,并使用gcloud compute instances create
命令,如下所示:gcloud compute instances create VM_NAME \ --hostname=HOST_NAME
请替换以下内容:
VM_NAME
:虚拟机的名称HOST_NAME
:要分配的完全限定网域主机名
例如,如需创建具有自定义主机名
test.example.com
的虚拟机myinstance
,请运行以下命令:gcloud compute instances create myinstance \ --hostname=test.example.com
下一步:配置 DNS 记录。如需了解详情,请参阅管理记录。
Terraform
您可以借助 Terraform 资源和
如需生成 Terraform 代码,您可以使用 Google Cloud 控制台中的等效代码组件。hostname
参数来创建使用自定义主机名的实例。- 在 Google Cloud 控制台中,前往虚拟机实例页面。
- 点击创建实例。
- 指定所需的参数。
- 在页面顶部或底部,点击等效代码,然后点击 Terraform 标签页以查看 Terraform 代码。
下一步:配置 DNS 记录。如需了解详情,请参阅管理记录。
Go
Java
Node.js
Python
REST
按照 API 说明通过映像或快照创建实例,并在请求正文中指定
hostname
字段。POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances { "name": "VM_NAME", "hostname": "HOST_NAME", ... }
请替换以下内容:
验证自定义主机名
对于 Linux 虚拟机,您可以通过在虚拟机上运行
hostname -f
命令来验证主机名。您还可以使用 Google Cloud 控制台或 Google Cloud CLI 验证自定义主机名。
控制台
gcloud
如需使用
gcloud compute
查看虚拟机的自定义主机名,请使用带--format
标志的instances describe
子命令过滤输出。将VM_NAME
替换为虚拟机的名称。gcloud compute instances describe VM_NAME \ --format='get(hostname)'
例如,如需查看名为
myinstance
的虚拟机的自定义主机名,请运行以下命令。gcloud compute instances describe myinstance \ --format='get(hostname)'
输出结果可能类似于以下内容:
test.example.com
如果未设置自定义主机名,则此命令的输出为空。
后续步骤
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-06-09。
-