This document explains how to manually stop, restart, reset, or reboot a virtual machine (VM) instance. These operations are useful in the following scenarios:
-
Stop a VM if you're no longer using it or if you want to modify one or more of its properties; for example, if you want to rename the VM, change its machine type, add or remove attached disks, or change the minimum CPU platform. After stopping the VM, restart it to resume your workload or delete the VM if you no longer need it.
-
Reset a VM if, for example, the VM is unresponsive or the guest operating system (OS) has crashed. After resetting the VM, you can resume your workload.
Reboot a VM from within the guest OS if, for example, you want to wipe the memory of the VM and re-initialize it to its original boot state. After rebooting a VM from within its guest OS, you can resume your workload.
Before you begin
-
If you haven't already, then set up authentication.
Authentication is
the process by which your identity is verified 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:
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
-
Install the Google Cloud CLI, then initialize it by running the following command:
gcloud init
- Set a default region and zone.
Go
To use the Go samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
- Install the Google Cloud CLI.
-
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.
For more information, see Set up authentication for a local development environment.
Java
To use the Java samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
- Install the Google Cloud CLI.
-
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.
For more information, see Set up authentication for a local development environment.
Node.js
To use the Node.js samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
- Install the Google Cloud CLI.
-
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.
For more information, see Set up authentication for a local development environment.
PHP
To use the PHP samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
- Install the Google Cloud CLI.
-
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.
For more information, see Set up authentication for a local development environment.
Python
To use the Python samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
- Install the Google Cloud CLI.
-
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.
For more information, see Set up authentication for a local development environment.
REST
To use the REST API samples on this page in a local development environment, you use the credentials you provide to the gcloud CLI.
Install the Google Cloud CLI, then initialize it by running the following command:
gcloud init
For more information, see Authenticate for using REST in the Google Cloud authentication documentation.
-
Required roles
To get the permissions that you need to stop and restart a VM,
ask your administrator to grant you the
Compute Instance Admin (v1) (roles/compute.instanceAdmin.v1
) IAM role on the project.
For more information about granting roles, see Manage access to projects, folders, and organizations.
This predefined role contains the permissions required to stop and restart a VM. To see the exact permissions that are required, expand the Required permissions section:
Required permissions
The following permissions are required to stop and restart a VM:
-
To stop a VM:
compute.instances.stop
on the VM -
To stop a VM from within the guest OS:
compute.instances.setMetadata
on the VM if it uses instance-level public SSH keys. -
To restart a VM:
compute.instances.start
on the VM -
To restart a VM that uses encryption keys:
compute.instances.startWithEncryptionKey
on the VM -
To reset a VM:
compute.instances.reset
on the VM -
To reboot a VM from within the guest OS:
compute.instances.setMetadata
on the VM
You might also be able to get these permissions with custom roles or other predefined roles.
Stop a VM
When you stop a running VM, the following happens:
Compute Engine sends the ACPI shutdown signal to the VM, and waits a short time for the guest OS to perform a clean shutdown. Then, Compute Engine sets the state of the VM to
STOPPING
first, and then transitions it toTERMINATED
within a minute.The VM resets to its power-on state and retains its internal IPs, MAC addresses, configuration settings, and metadata. However, the VM loses its in-memory data and VM state. If you need to retain the in-memory data and VM state, suspend the VM instead.
Additionally, if you attached any resources to the VM, those resources remain
attached until you manually detach them or you delete the VM. If the VM has any
Local SSD disks attached, stopping the VM discards all data on the Local SSD
disks by default. To keep the data on the Local SSD disks, you must stop the VM
by using the gcloud CLI with the --discard-local-ssd=false
flag.
For more information, see
Stop a VM with Local SSD.
To stop a VM, select one of the methods described in this document:
After you stop a VM, you can either restart it or delete it. If you don't plan to restart the VM, deleting it is recommended.
Stop a VM through Google Cloud
To stop more than one VM at a time, use the Google Cloud console.
Console
To stop one or more VMs, do the following:
In the Google Cloud console, go to the VM instances page.
Select the VMs to stop.
Click
Stop, and then click Stop to confirm.
gcloud
To stop a VM, use the
gcloud compute instances stop
command.
gcloud compute instances stop VM_NAME \
--project=PROJECT_ID \
--zone=ZONE
Replace the following:
VM_NAME
: the name of the VM.PROJECT_ID
: the ID of the project where the VM is located.ZONE
: the zone where the VM is located.
If you attached a Local SSD disk to the VM and you want to keep the Local
SSD data after stopping the VM, use the
gcloud beta compute instances stop
command
with the --discard-local-ssd=false
flag.
gcloud beta compute instances stop VM_NAME \
--discard-local-ssd=false \
--project=PROJECT_ID \
--zone=ZONE
Go
Java
Node.js
PHP
Python
REST
To stop a VM, make a POST
request to the
instances.stop
method.
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME/stop
Replace the following:
VM_NAME
: the name of the VM.PROJECT_ID
: the ID of the project where the VM is located.ZONE
: the zone where the VM is located.
Stop a VM from within the guest OS
To stop a VM from within the guest OS when you're connected to the VM, select one of the following tabs.
Linux
To stop a Linux VM, select one of the following methods:
For a clean shutdown that allows the VM to run shutdown scripts before sending the ACPI shutdown signal to the VM, run the following command:
sudo shutdown -h now
Otherwise, to force a shutdown, run the following command:
sudo poweroff
Windows
To stop a Windows VM, select one of the following methods:
For a clean shutdown that allows the VM to run shutdown scripts before sending the ACPI shutdown signal to the VM, run the following command:
shutdown /s
Otherwise, to force a shutdown, run the following command:
shutdown /f
Restart a VM
You can restart a stopped VM only if the VM is in the TERMINATED
state. To
restart a VM, select one of the following methods described in this document:
Restart a VM without encrypted disks
Restart a stopped VM that doesn't use any encrypted disks (default). To restart more than one VM at a time, use the Google Cloud console.
Console
To restart one or more VMs, do the following:
In the Google Cloud console, go to the VM instances page.
Select one or more VMs.
Click
Start / Resume.
gcloud
To restart a VM, use the
gcloud compute instances start
command.
gcloud compute instances start VM_NAME \
--project=PROJECT_ID \
--zone=ZONE
Replace the following:
VM_NAME
: the name of the VM.PROJECT_ID
: the ID of the project where the VM is located.ZONE
: the zone where the VM is located.
Go
Java
Node.js
PHP
Python
REST
To restart a VM, make a POST
request to the
instances.start
method.
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME/start
Replace the following:
VM_NAME
: the name of the VM to restart.PROJECT_ID
: the ID of the project where the VM is located.ZONE
: the zone where the VM is located.
Restart a VM that uses encrypted disks
You can restart a stopped VM that uses customer-supplied encryption keys to encrypt the persistent disks attached to the VM.
Console
To restart a VM that use encrypted disks, do the following:
In the Google Cloud console, go to the VM instances page.
Select the VM to restart.
Click
Start / Resume.Specify encryption keys for each of the encrypted disks that are attached to this VM, and then click Start.
gcloud
To restart a VM that uses encrypted disks, use the
gcloud compute instances start
command
with the --csek-key-file
flag. If you're using an RSA-wrapped key, use the
gcloud beta compute instances start
command
instead.
gcloud compute instances start VM_NAME \
--csek-key-file=ENCRYPTION_KEY_FILE \
--project=PROJECT_ID \
--zone=ZONE
Replace the following:
VM_NAME
: the name of the VM.ENCRYPTION_KEY_FILE
: the relative path to the JSON file that contains the customer-supplied encryption key.PROJECT_ID
: the ID of the project where the VM is located.ZONE
: the zone where the VM is located.
Go
Java
Node.js
PHP
Python
REST
To restart a VM that uses encrypted disks, make a POST
request to the
instances.startWithEncryptionKey
method.
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME/startWithEncryptionKey
{
"disks": [
{
"source": "DISK_URL",
"diskEncryptionKey": {
"ENCRYPTION_TYPE": "ENCRYPTION_KEY"
}
}
]
}
Replace the following:
PROJECT_ID
: the ID of the project where the VM is located.ZONE
: the zone where the VM is located.VM_NAME
: the name of the VM.DISK_URL
: the resource URL corresponding to the full resource name of the attached disk that is encrypted with a customer-supplied encryption key.ENCRYPTION_TYPE
: the type of disk encryption that you are using, which can be one of the following:rawKey
,kmsKeyName
, orrsaEncryptedKey
. If you use thersaEncryptedKey
type, you must make aPOST
request to the beta API instead of the v1 API.ENCRYPTION_KEY
: the encryption key used to encrypt the persistent disks attached to the VM.rawKey
orrsaEncryptedKey
keys must be base64-encoded. Additionally, to prepare arsaEncryptedKey
key, see RSA key wrapping.
Reset a VM
Resetting a running VM is similar to doing a hard reset on your computer, such as when you press a reset button or press and hold the power button. When you reset a VM, you don't give time to the VM to perform a clean shutdown of the guest OS. Consequently, this might corrupt the disk's filesystems. Google Cloud recommends resetting a VM only if the guest OS has crashed and you have no other options. If you want to re-initialize a VM and perform a clean shut down, see Reboot a VM in this document.
When you reset a VM, the following happens:
The VM re-initializes to its initial boot state with the original metadata, image, and persistent disks.
The VM forcibly wipes the memory contents of the machine unless you back up the data.
The VM state remains
RUNNING
through the process.
Resetting a VM doesn't change any of its VM properties. The VM retains its ephemeral IPs, static external IPs, persistent disks, local SSD disks (if any), and machine type.
Console
To reset one or more running VMs, do the following:
In the Google Cloud console, go to the VM instances page.
Select the VMs to reset.
Click Reset, and then click Reset again.
gcloud
To reset a running VM, use the
gcloud compute instances reset
command.
gcloud compute instances reset VM_NAME \
--project=PROJECT_ID \
--zone=ZONE
Replace the following:
VM_NAME
: the name of the VM.PROJECT_ID
: the ID of the project where the VM is located.ZONE
: the zone where the VM is located.
Go
Java
Node.js
PHP
Python
REST
To reset a running VM, make a POST
request to the
instances.reset
method.
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME/reset
Replace the following:
VM_NAME
: the name of the VM.PROJECT_ID
: the ID of the project where the VM is located.ZONE
: the zone where the VM is located.
Reboot a VM
When you reboot a VM from within the guest OS, the following happens:
The VM re-initializes to its initial boot state with the original metadata, image, and persistent disks.
The VM wipes the memory contents of the machine unless you back up the data.
The VM state remains
RUNNING
through the process.
Compared to resetting a VM, rebooting a VM from within the guest OS waits for the VM to finish off all the running tasks before sending the ACPI shutdown signal to the VM. This allows the VM to perform a clean shutdown.
Rebooting a VM doesn't change any of its VM properties. The VM retains its ephemeral IPs, static external IPs, persistent disks, local SSD disks (if any), and machine type.
Linux
To reset a Linux VM, reboot the guest OS by running the following command:
sudo reboot
Windows
To reset a Windows VM, reboot the guest OS by running the following command:
shutdown /r /t 0
Optionally, to monitor the process of shutting down and restarting the VM, enable the Windows Boot Manager menu.
Billing
After a VM is stopped and Compute Engine sets it state to TERMINATED
,
you no longer incur charges for CPU usage. However, you continue incurring
charges for any resources attached to the VM, such as
persistent disks and
external IP addresses.
To stop incurring charges for the resources attached to a VM, do one of the following:
Detach those resources from the stopped VM.
Delete the VM. This action deletes any resources attached to the VM as well.
What's next
Learn how to schedule VMs to start and stop automatically.
Learn how to limit the runtime of a VM.
Learn how to troubleshoot a VM after it shuts down unexpectedly by doing one of the following:
Troubleshoot the VM using the interactive serial console.
Learn how to change the machine type in a VM.