本页面介绍了如何为您的 Google Cloud 控制台项目设置默认区域或可用区。
向 Compute Engine 发出请求时,您可能需要根据请求的特定要求提供区域或地区。任何涉及区域资源的请求均需要提供区域名称;任何涉及可用区资源的请求均需要提供可用区名称。例如,创建静态外部 IP 地址时必须指定区域,而创建虚拟机实例时必须指定可用区。
创建项目时,Compute Engine 会根据您创建项目的位置自动选择默认区域和地区。Compute Engine 会尝试选择靠近项目创建位置的区域和地区,这样,您创建的资源到达客户或客户端的延迟时间就会缩短。如果您要在其他区域或地区创建资源,则可以替换默认选项。
如需详细了解区域和地区,请参阅区域和地区。
如需详细了解 Compute Engine 的全球、区域和地区性资源,请参阅全球、区域和地区性资源。
准备工作
-
如果您尚未设置身份验证,请进行设置。身份验证是通过其进行身份验证以访问 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
-
安装 Google Cloud CLI。 安装完成后,运行以下命令来初始化 Google Cloud CLI:
gcloud init如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI。
- Set a default region and zone.
默认区域和地区对您的项目的影响
默认区域和可用区设置仅会影响 Google Cloud CLI 和 Google Cloud 控制台等客户端工具。当您使用这些工具构建请求时,这些工具会自动选择默认区域和地区以帮助您管理资源。
当您使用 Google Cloud 控制台创建区域级或可用区级资源(例如地址或实例)时,Compute Engine 会为您设置区域和可用区字段。您可以接受预先填充的值,也可以明确更改其中一个或这两个值。
当您使用 gcloud CLI 时,可以忽略设置
--region和--zone标志以使用新项目的默认区域和可用区属性。您可以随时更改元数据服务器中的默认区域和可用区设置、在本地替换 gcloud CLI 的默认区域和可用区,或者使用 gcloud CLI 或 Google Cloud 控制台手动替换每个请求的设置。
查找默认区域或地区
元数据服务器包含项目的相关元数据信息,例如项目 ID 和项目名称。您可以查询元数据服务器来获取和使用项目相关信息。
如果为项目指定默认区域和地区,则 Compute Engine 会在元数据服务器中为项目设置这些值。
使用以下值设置默认地区和区域:
google-compute-default-regiongoogle-compute-default-zone
如需查看您的默认区域和地区设置,请运行以下
gcloud命令,并将PROJECT_ID替换为您的项目 ID:gcloud compute project-info describe --project PROJECT_ID在响应中查找默认区域和地区的元数据值。如果响应中缺少
google-compute-default-region和google-compute-default-zone键及其值,则表示未设置默认区域或地区。更改默认区域或地区
系统不限制为默认属性设置哪些区域或可用区。例如,您可以在您的默认区域之外设置默认地区。创建和管理资源时,请检查默认区域和可用区设置。
控制台
如需更改默认区域或可用区,请按照以下步骤操作:
在 Google Cloud 控制台中,前往设置页面。
从区域下拉菜单中选择默认区域。
从地区下拉菜单中选择默认地区。
点击保存。
gcloud
您可以向元数据服务器发出请求来更改默认区域和可用区。例如:
gcloud compute project-info add-metadata \ --metadata google-compute-default-region=europe-west1,google-compute-default-zone=europe-west1-b使用任何方法更新默认元数据后,运行
gcloud init命令来重新初始化default配置。只有运行gcloud init命令后,gcloud CLI 才会刷新默认区域和可用区设置。如需详细了解如何使用 gcloud CLI 选项设置默认区域和可用区,请参阅设置默认属性。
后续步骤
- 详细了解区域和地区。
- 详细了解全球、区域和可用区资源
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-27。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-27。"],[[["\u003cp\u003eThis page explains how to set and manage default regions and zones for your Google Cloud project, which are necessary for creating regional and zonal resources like static IP addresses and virtual machine instances.\u003c/p\u003e\n"],["\u003cp\u003eCompute Engine automatically selects a default region and zone based on the project's origin, but users can override these defaults to create resources in different locations, to reduce latency.\u003c/p\u003e\n"],["\u003cp\u003eDefault region and zone settings affect client tools like the Google Cloud CLI and Google Cloud console by automatically pre-populating these values when creating resources, but can be manually changed or overridden.\u003c/p\u003e\n"],["\u003cp\u003eYou can check your current default region and zone settings by querying the metadata server using the \u003ccode\u003egcloud compute project-info describe\u003c/code\u003e command, which will list \u003ccode\u003egoogle-compute-default-region\u003c/code\u003e and \u003ccode\u003egoogle-compute-default-zone\u003c/code\u003e if they are set.\u003c/p\u003e\n"],["\u003cp\u003eThe default region and zone can be modified either through the Google Cloud console settings page, or by using the \u003ccode\u003egcloud compute project-info add-metadata\u003c/code\u003e command to update the metadata server, followed by the \u003ccode\u003egcloud init\u003c/code\u003e command to refresh the local configuration.\u003c/p\u003e\n"]]],[],null,["# Changing the default region or zone\n\n*** ** * ** ***\n\nThis page explains how to set a default\n[region or zone](/compute/docs/regions-zones) for your Google Cloud console\n[project](/resource-manager/docs/creating-managing-projects).\n\nWhen you make a request to Compute Engine, you might be required to\nprovide a region or a zone depending on the specific requirements of your\nrequest. Any request involving\n[regional](/compute/docs/regions-zones/global-regional-zonal-resources#regionalresources)\nresources requires that you supply a region name, and any request involving\n[zonal](/compute/docs/regions-zones/global-regional-zonal-resources#zoneresource)\nresources requires that you supply a zone name. For example, you must specify\na region when you create a static external IP address, and you must specify\na zone when you create a virtual machine (VM) instance.\n\nWhen you create a project, Compute Engine automatically selects\na default region and zone based on the location from which you create the\nproject. Compute Engine attempts to pick a region and a\nzone that are close to where the project originates so that resources you\ncreate have reduced latency to your customers or clients. If you want to\ncreate resources in a different region or zone, you can override the default\nselection.\n\nFor more information about regions and zones, see\n[Regions and zones](/compute/docs/regions-zones).\n\nFor more information about global, regional, and zonal Compute Engine\nresources, see\n[Global, regional, and zonal resources](/compute/docs/regions-zones/global-regional-zonal-resources).\n\nBefore you begin\n----------------\n\n- If you haven't already, set up [authentication](/compute/docs/authentication). Authentication verifies your identity for access to Google Cloud services and APIs. To run code or samples from a local development environment, you can authenticate to Compute Engine by selecting one of the following options:\n\n Select the tab for how you plan to use the samples on this page: \n\n ### Console\n\n\n When you use the Google Cloud console to access Google Cloud services and\n APIs, you don't need to set up authentication.\n\n ### gcloud\n\n 1.\n [Install](/sdk/docs/install) the Google Cloud CLI.\n\n After installation,\n [initialize](/sdk/docs/initializing) the Google Cloud CLI by running the following command:\n\n ```bash\n gcloud init\n ```\n\n\n If you're using an external identity provider (IdP), you must first\n [sign in to the gcloud CLI with your federated identity](/iam/docs/workforce-log-in-gcloud).\n | **Note:** If you installed the gcloud CLI previously, make sure you have the latest version by running `gcloud components update`.\n 2. [Set a default region and zone](/compute/docs/gcloud-compute#set_default_zone_and_region_in_your_local_client).\n\nHow a default region and zone affect your project\n-------------------------------------------------\n\nThe default region and zone settings affect only client tools, such as the\nGoogle Cloud CLI and the Google Cloud console. When you use these tools to\nconstruct your requests, the tools help you manage resources by automatically\nselecting the default region and zone.\n\nWhen you use the Google Cloud console to create regional or zonal resources\nlike addresses and instances, Compute Engine sets the region and zone\nfields for you. You can accept the pre-populated values or explicitly change\none or both of the values.\n\nWhen you use the gcloud CLI, omit setting the `--region` and\n`--zone` flags to use the default region and zone properties for the new\nproject.\n\nYou can always change the default region and zone settings in the metadata\nserver, override the default region and zone locally for the\ngcloud CLI, or override the settings manually for each request\nby using either the gcloud CLI or the Google Cloud console.\n\nLooking up the default region or zone\n-------------------------------------\n\nThe metadata server contains metadata information about a project, including\nthe project ID and project name. You can query the metadata server to get\nand use information about the project.\n\nIf you specify a default region and zone for your project, then\nCompute Engine sets these values in the\n[metadata server](/compute/docs/storing-retrieving-metadata) for your project.\n\nDefault regions and zones are set by using the following values:\n\n- `google-compute-default-region`\n- `google-compute-default-zone`\n\nTo see what your default region and zone settings are, run the following\n[`gcloud`](/compute/docs/gcloud-compute) command, replacing\n\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e with your project ID: \n\n gcloud compute project-info describe --project \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e\n\nLook for the default region and zone metadata values in the response. If the\n`google-compute-default-region` and `google-compute-default-zone`\nkeys and values are missing from the response, that means no default region\nor zone is set.\n\nChanging the default region or zone\n-----------------------------------\n\nThere are no restrictions on which regions or zones you can set for your default properties. For example, it is possible to set a default zone that is outside of your default region. Check the default region and zone settings when creating and managing your resources. \n\n### Console\n\nTo change your default region or zone, follow these steps:\n\n1. In the Google Cloud console, go to the **Settings** page.\n\n [Go to Settings](https://console.cloud.google.com/compute/settings)\n2. From the **Region** drop-down menu, select a default region.\n\n3. From the **Zone** drop-down menu, select a default zone.\n\n4. Click **Save**.\n\n### gcloud\n\nYou can change the default region and zone\nby making a request to the\n[metadata](/compute/docs/storing-retrieving-metadata) server. For example: \n\n```\ngcloud compute project-info add-metadata \\\n --metadata google-compute-default-region=europe-west1,google-compute-default-zone=europe-west1-b\n```\n\nAfter you update the default metadata by using any method, run the\n`gcloud init` command to reinitialize your `default` configuration.\nThe gcloud CLI refreshes the default region and zone settings\nonly after you run the `gcloud init` command.\n\nFor more information about using the gcloud CLI options to set default\nregions and zones, see\n[Set default properties](/compute/docs/gcloud-compute#default-properties).\n\nWhat's next\n-----------\n\n- Learn more about [regions and zones](/compute/docs/regions-zones).\n- Learn more about [global, regional, and zonal resources](/compute/docs/regions-zones/global-regional-zonal-resources)."]] -