本文档介绍了如何重启或重置 Compute Engine 实例。如需详细了解重置实例的效果,以及暂停、停止或重置实例之间的区别,请参阅暂停、停止或重置 Compute Engine 实例。
重启或重置实例有助于确保最佳性能和稳定性,或有助于解决客机操作系统(OS)冻结、运行缓慢或崩溃等问题。根据实例的客机操作系统状态,执行以下操作之一:
重启实例。如果您的客机操作系统速度缓慢或已冻结,重启可让其有足够的时间在关机前完成正在运行的任务。
重置实例。仅在客机操作系统崩溃或无响应且您没有其他选项时重置实例。重置实例不会让客机操作系统彻底关闭。此操作可能会舍弃未保存的数据,且可能会损坏任何磁盘的文件系统。
准备工作
-
如果您尚未设置身份验证,请进行设置。身份验证是通过其进行身份验证以访问 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.reset
-
如需从客机操作系统内部重启实例,请执行以下操作:
compute.instances.setMetadata
如果您尚未连接到实例,请先连接。
如需重启实例,请运行以下命令:
sudo reboot
如果您尚未连接到实例,请使用以下方法之一连接到实例:
如需重启实例,请运行以下命令:
shutdown /r /t 0
(可选)如需监控关停和重启实例的过程,请启用 Windows 启动管理器菜单。
在 Google Cloud 控制台中,前往虚拟机实例页面。
选择要重置的实例。
点击重置,然后点击重置进行确认。
INSTANCE_NAMES
:以空格分隔的实例名称列表,例如instance-01 instance-02 instance-03
。ZONE
:实例所在的可用区。INSTANCE_NAME
:实例的名称。PROJECT_ID
:实例所在项目的 ID。ZONE
:实例所在的区域。
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。
PHP
如需在本地开发环境中使用本页面上的 PHP 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭据设置应用默认凭据。
如需了解详情,请参阅 Set up authentication for a local development environment。
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 角色。 如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限。此预定义角色包含重置或重启计算实例所需的权限。如需查看所需的确切权限,请展开所需权限部分:
所需权限
您需要具备以下权限才能重置或重启计算实例:
重启实例
重启计算实例可让实例中的客机操作系统在 Compute Engine 发送 ACPI 关停信号之前完成正在运行的任务。这有助于确保彻底关闭客机操作系统。
重启实例会清除实例使用的内存。如果您在实例中使用 RAM 磁盘,并且需要保留这些数据,请在重启实例之前备份数据。
如需重启实例,请选择以下选项之一:
Linux
Windows
重置实例
重置实例会清除实例内存中的所有数据,包括存储在 RAM 磁盘上的所有临时文件。这些数据会永久丢失,并且在重置实例之前,Compute Engine 不会创建备份。
您可以同时重置多个实例或单个实例。对于多个实例,请使用 Google Cloud 控制台;对于位于同一可用区中的实例,请使用 Google Cloud CLI。对于单个实例,请选择以下任一选项:
控制台
gcloud
如需重置单个可用区中一个或多个正在运行的实例,请使用
gcloud compute instances reset
命令:gcloud compute instances reset INSTANCE_NAMES \ --zone=ZONE
替换以下内容:
Go
Java
Node.js
PHP
Python
REST
如需重置正在运行的实例,请向
instances.reset
方法发出POST
请求:POST https://compute.googleapis.com//compute/v1/projects/PROJECT_ID/zones/ZONE/instances/INSTANCE_NAME/reset
替换以下内容:
后续步骤
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-06-19。
-