本文說明如何停止或重新啟動 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
-
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.stop
執行個體 -
如要從客層 OS 停止執行個體:
compute.instances.setMetadata
(如果執行個體使用執行個體層級的公開 SSH 金鑰)。 -
如要重新啟動執行個體,請按照下列指示操作:
compute.instances.start
執行個體 -
如要重新啟動使用加密金鑰的執行個體,請按照下列步驟操作:
compute.instances.startWithEncryptionKey
在執行個體上 -
您可以同時停止一或多個未連接任何本機 SSD 磁碟的執行個體。執行個體中的任何關機指令碼都必須在預設關機期限內完成執行。
-
同時停止一或多個已連結本機 SSD 磁碟的執行個體時,您可以選擇捨棄或保留 (預先發布) 這些磁碟上的資料。執行個體中的任何關機指令碼都必須在預設關機期限內完成執行。
-
這個方法可讓您在關機指令碼執行完畢後停止單一執行個體,或在啟用安全關機後停止執行個體 (不安全關機),或結束進行中的安全關機程序。除非您手動將任何已連結本機 SSD 磁碟的資料備份到持久儲存空間磁碟區,否則從客層 OS 停止執行個體時,系統會捨棄這些磁碟上的所有資料。
如果您已在執行個體中啟用正常關機,可以使用 Google Cloud 主控台、gcloud CLI 或 REST API 停止執行個體,不必正常關機,也可以結束進行中的正常關機程序。
如要同時停止多個執行個體,請使用 Google Cloud 控制台,或針對位於相同區域的執行個體使用 gcloud CLI。
前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面。
選取要停止的執行中執行個體。
按一下「停止」
。如果沒有「停止」選項,請依序點選 「更多動作」> 「停止」。在對話方塊中執行下列操作:
選用:如要停止執行個體,但不要正常關閉執行個體,或是要結束進行中的正常關閉程序,請選取「Skip graceful shutdown (if applicable)」(略過正常關閉程序 (如適用)) 核取方塊。
按一下「停止」確認操作。
INSTANCE_NAMES
:以空格分隔的執行個體名稱清單,例如instance-01 instance-02 instance-03
。ZONE
:執行個體所在的區域。INSTANCE_NAME
:執行個體的名稱。PROJECT_ID
:執行個體所在專案的 ID。ZONE
:執行個體所在的區域。如果您已在執行個體中啟用正常關機,則可以使用 Google Cloud 主控台、gcloud CLI 或 REST API 停止執行個體,不必正常關機,也可以結束進行中的正常關機程序。
如要保留連結至執行個體的本機 SSD 磁碟資料 (使用 Titanium SSD 的機器類型除外),請使用 gcloud CLI 或 REST API 停止執行個體。
如要同時停止多個執行個體,請使用 Google Cloud 控制台,或針對位於相同區域的執行個體使用 gcloud CLI。
前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面。
選取要停止的執行中執行個體。
按一下「停止」
。如果沒有「停止」選項,請依序點選 「更多動作」> 「停止」。在對話方塊中執行下列操作:
選用:如要停止執行個體,但不要正常關閉執行個體,或是要結束進行中的正常關閉程序,請選取「Skip graceful shutdown (if applicable)」(略過正常關閉程序 (如適用)) 核取方塊。
按一下「停止」確認操作。
如要捨棄本機 SSD 資料,請使用
gcloud compute instances stop
指令並加上--discard-local-ssd=true
旗標:gcloud compute instances stop INSTANCE_NAMES \ --discard-local-ssd=true \ --zone=ZONE
如要保留本機 SSD 資料,請使用
gcloud beta compute instances stop
指令並加上--discard-local-ssd=false
標記:gcloud beta compute instances stop INSTANCE_NAMES \ --discard-local-ssd=false \ --zone=ZONE
INSTANCE_NAMES
:以空格分隔的執行個體名稱清單,例如instance-01 instance-02 instance-03
。ZONE
:執行個體所在的區域。如要捨棄本機 SSD 資料,請對
instances.stop
方法發出POST
要求。在要求網址中,加入設為true
的discardLocalSsd
查詢參數:POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/INSTANCE_NAME/stop?discardLocalSsd=true
如要保留本機 SSD 資料,請對
beta.instances.stop
方法發出POST
要求。在要求網址中,加入設為false
的discardLocalSsd
查詢參數:POST https://compute.googleapis.com/compute/beta/projects/PROJECT_ID/zones/ZONE/instances/INSTANCE_NAME/stop?discardLocalSsd=false
PROJECT_ID
:執行個體所在專案的 ID。ZONE
:執行個體所在的區域。INSTANCE_NAME
:執行個體的名稱。如果尚未連線,請連線至執行個體。
如要停止執行個體,請選取下列其中一種方法:
如要執行乾淨關機作業,讓執行個體在關閉客體 OS 前執行關機指令碼,請執行下列指令:
sudo shutdown -h now
否則,如要強制關機,請執行下列指令:
sudo poweroff
如果尚未連線至執行個體,請使用下列其中一種方法:
如要停止執行個體,請選取下列其中一種方法:
如要乾淨停止執行個體,並讓執行個體在關閉客體 OS 前執行關機指令碼,請執行下列指令:
shutdown /s
如要強制關機,請執行下列指令:
shutdown /f
如果執行個體未附加加密磁碟,請參閱重新啟動未附加加密磁碟的執行個體。
否則,請參閱「重新啟動具有加密磁碟的執行個體」。
前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面。
選取一或多個執行個體。
按一下「開始 / 繼續」圖示
。INSTANCE_NAMES
:以空格分隔的執行個體名稱清單,例如instance-01 instance-02 instance-03
。ZONE
:執行個體所在的區域。INSTANCE_NAME
:要重新啟動的執行個體名稱。PROJECT_ID
:執行個體所在專案的 ID。ZONE
:執行個體所在的區域。前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面。
選取要重新啟動的執行個體。
按一下「開始 / 繼續」圖示
。為連接到執行個體的每個加密磁碟指定加密金鑰,然後按一下「啟動」。
INSTANCE_NAMES
:以空格分隔的執行個體名稱清單,例如instance-01 instance-02 instance-03
。ENCRYPTION_KEY_FILE
:包含客戶提供加密金鑰的 JSON 檔案相對路徑。只有在執行個體使用相同的客戶提供加密金鑰時,您才能同時重新啟動多個執行個體。ZONE
:執行個體所在的區域。PROJECT_ID
:執行個體所在專案的 ID。ZONE
:執行個體所在的區域。INSTANCE_NAME
:執行個體的名稱。DISK_URL
:資源網址,對應於已連接磁碟的完整資源名稱,該磁碟已使用客戶提供的加密金鑰加密。ENCRYPTION_TYPE
:您使用的磁碟加密類型,可以是下列其中一種:rawKey
、kmsKeyName
或rsaEncryptedKey
。如果您使用rsaEncryptedKey
型別,請對beta.instances.startWithEncryptionKey
方法發出POST
要求。ENCRYPTION_KEY
:用來加密連結至執行個體的永久磁碟的加密金鑰。rawKey
或rsaEncryptedKey
金鑰必須採用 Base64 編碼。此外,如要準備rsaEncryptedKey
金鑰,請參閱「RSA 金鑰包裝」。
Go
如要在本機開發環境中使用本頁的 Go 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。
詳情請參閱 Set up authentication for a local development environment。
Java
如要在本機開發環境中使用本頁的 Java 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。
詳情請參閱 Set up authentication for a local development environment。
Node.js
如要在本機開發環境中使用本頁的 Node.js 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。
詳情請參閱 Set up authentication for a local development environment。
PHP
如要在本機開發環境中使用本頁的 PHP 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。
詳情請參閱 Set up authentication for a local development environment。
Python
如要在本機開發環境中使用本頁的 Python 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。
詳情請參閱 Set up authentication for a local development environment。
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 驗證說明文件中的「Authenticate for using REST」。
必要的角色
如要取得停止或重新啟動 Compute 執行個體所需的權限,請要求管理員授予您專案的 Compute 執行個體管理員 (v1) (
roles/compute.instanceAdmin.v1
) IAM 角色。如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和機構的存取權」。這個預先定義的角色具備停止或重新啟動運算執行個體所需的權限。如要查看確切的必要權限,請展開「必要權限」部分:
所需權限
如要停止或重新啟動運算執行個體,您必須具備下列權限:
停止執行個體
停止運算執行個體時,或 Compute Engine 因排定動作而停止執行個體時,執行個體會保留附加資源、設定、內部 IP 位址、MAC 位址和中繼資料。不過,執行個體會遺失記憶體內資料和應用程式狀態。如要保留這些狀態,請改為暫停執行個體。
視執行個體是否已連接本機 SSD 磁碟,以及您要如何處理關機指令碼,您可以透過下列方法停止執行個體:
停止沒有本機 SSD 硬碟的執行個體
視停止執行個體時要執行的操作而定,請使用下列選項:
如要停止一或多個執行個體,請選取下列其中一個選項:
主控台
gcloud
如要停止同一區域中的一或多個執行個體,請使用
gcloud compute instances stop
指令:gcloud compute instances stop INSTANCE_NAMES \ --zone=ZONE
更改下列內容:
(選用) 如果您在一或多個執行個體中啟用安全關機,可以停止執行個體,不必安全關機,也可以手動結束進行中的安全關機。做法是使用加上
--no-graceful-shutdown
旗標的gcloud beta compute instances stop
指令:gcloud beta compute instances stop INSTANCE_NAMES \ --no-graceful-shutdown \ --zone=ZONE
Go
Java
Node.js
PHP
Python
REST
如要停止執行個體,請對
instances.stop
方法發出POST
要求:POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/INSTANCE_NAME/stop
更改下列內容:
如果已在執行個體中啟用安全關機功能,您可以停止執行個體,不必安全關機,也可以手動結束進行中的安全關機程序。如要這麼做,請對
instances.stop
方法發出POST
要求。在要求網址中加入noGracefulShutdown=true
查詢參數:POST https://compute.googleapis.com/compute/beta/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME/stop?noGracefulShutdown=true
停止具有本機 SSD 磁碟的執行個體
視您想在停止運算執行個體時執行的操作而定,請使用下列選項:
如要停止一或多個已連結本機 SSD 磁碟的執行個體,請選取下列其中一個選項:
主控台
gcloud
停止同一區域中一或多個已連結本機 SSD 磁碟的執行個體時,請指定是否要捨棄或保留本機 SSD 資料,方法如下:
更改下列內容:
如果已在一或多個執行個體中啟用安全關機,您可以停止執行個體,不必安全關機,也可以結束進行中的安全關機程序。做法是使用加上
--no-graceful-shutdown
旗標的gcloud beta compute instances stop
指令:gcloud beta compute instances stop INSTANCE_NAMES \ --discard-local-ssd=DISCARD_LOCAL_SSD \ --no-graceful-shutdown \ --zone=ZONE
請將
DISCARD_LOCAL_SSD
換成true
,捨棄本機 SSD 磁碟中的資料,或換成false
保留資料。REST
停止已連結本機 SSD 磁碟的執行個體時,請指定是否要捨棄或保留本機 SSD 資料,方法如下:
更改下列內容:
如果已在執行個體中啟用安全關機功能,您可以選擇停止執行個體,而不安全關機,也可以手動結束進行中的安全關機程序。如要這麼做,請對
instances.stop
方法發出POST
要求。在要求網址中加入noGracefulShutdown=true
查詢參數:POST https://compute.googleapis.com/compute/beta/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME/stop?discardLocalSsd=DISCARD_LOCAL_SSD&noGracefulShutdown=true
請將
DISCARD_LOCAL_SSD
換成true
,捨棄本機 SSD 磁碟中的資料,或換成false
保留資料。透過客體 OS 停止執行個體
如果運算執行個體已連結本機 SSD 磁碟,關閉訪客 OS 時,系統會自動捨棄本機 SSD 資料。如要保留這項資料,請在停止執行個體前,手動將資料複製到永久儲存空間選項。
如要從訪客 OS 停止執行個體,請選取下列任一選項:
Linux
Windows
重新啟動執行個體
您可以重新啟動已完全停止的運算執行個體 (執行個體狀態為
TERMINATED
)。如果您在停止執行個體時選擇保留本機 SSD 磁碟的資料,則可能需要在重新啟動執行個體後重新掛接本機 SSD 磁碟。如要進一步瞭解如何掛接本機 SSD 磁碟,請參閱格式化並掛接本機 SSD 裝置。
如要重新啟動執行個體,請根據執行個體是否已連結加密磁碟,使用下列其中一種方法:
重新啟動沒有加密磁碟的執行個體
您可以同時重新啟動多個運算執行個體,也可以個別重新啟動執行個體。如要使用多個執行個體,請使用 Google Cloud 控制台;如要使用位於同一區域的執行個體,請使用 gcloud CLI。如果是個別執行個體,請選取下列任一選項:
主控台
gcloud
如要重新啟動同一區域中的一或多個執行個體,請使用
gcloud compute instances start
指令:gcloud compute instances start INSTANCE_NAMES \ --zone=ZONE
更改下列內容:
Go
Java
Node.js
PHP
Python
REST
如要重新啟動執行個體,請對
instances.start
方法發出POST
要求:POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/INSTANCE_NAME/start
更改下列內容:
重新啟動具有加密磁碟的執行個體
重新啟動已停止的運算執行個體時,如果該執行個體有使用客戶提供的加密金鑰加密的連結磁碟,您必須提供加密金鑰資訊。
您可以同時重新啟動多個執行個體,也可以個別重新啟動執行個體。如要刪除多個執行個體,請使用 Google Cloud 控制台,或針對位於同一區域的執行個體使用 gcloud CLI。如要個別選取執行個體,請選取下列任一選項:
主控台
gcloud
如要重新啟動同一區域中一或多個使用加密磁碟的執行個體,請使用
gcloud compute instances start
指令並加上--csek-key-file
旗標。如果您使用的是 RSA 包裝金鑰,請改用gcloud beta compute instances start
指令並加上--csek-key-file
標記:gcloud compute instances start INSTANCE_NAMES \ --csek-key-file=ENCRYPTION_KEY_FILE \ --zone=ZONE
更改下列內容:
Go
Java
Node.js
PHP
Python
REST
如要重新啟動使用加密磁碟的執行個體,請對
instances.startWithEncryptionKey
方法發出POST
要求:POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/INSTANCE_NAME/startWithEncryptionKey { "disks": [ { "source": "DISK_URL", "diskEncryptionKey": { "ENCRYPTION_TYPE": "ENCRYPTION_KEY" } } ] }
更改下列內容:
後續步驟
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-15 (世界標準時間)。
-