Stopping and starting an VM


This page describes how to stop and start a virtual machine (VM) instance. Note that Compute Engine uses STOP and TERMINATE interchangeably. To suspend and resume a VM, read Suspending and resuming an instance. For more information about stopping and suspending a VM, see Instance life cycle.

You can stop a VM temporarily if you no longer need it and restart it at a later time. A stopped VM retains its persistent disks, its internal IPs, and its MAC addresses. However, the VM shuts down the guest OS and loses its application state. Essentially, a stopped VM resets to its power-on state and no data is saved. Stop a VM if you want to change the machine type, add or remove attached disks, change the minimum CPU platform, add or remove GPUs, or apply sizing recommendations.

Stopping a VM causes Compute Engine to send the ACPI shutdown signal to the VM. Modern guest operating systems are configured to perform a clean shutdown before powering off in response to the power off signal. Compute Engine waits a short time for the guest to finish shutting down and then transitions the VM to the TERMINATED state.

A stopped VM does not incur charges, but all resources attached to the VM continue to incur charges. For example, you are charged for persistent disks and external IP addresses even if a VM is stopped. To stop being charged for attached resources, you can reconfigure a stopped VM to not use those resources, and then delete the resources.

If you need to retain the guest OS and application state, suspend the instance instead.

Before you begin

Restrictions

You cannot stop a VM with a local SSD attached. Compute Engine does not prevent you from shutting down a VM from inside the guest operating system if the VM has a local SSD. Instead, you must migrate your critical data off of the local SSD to a persistent disk or to another VM before you delete the instance completely. Compute Engine does not prevent you from shutting down the guest operating system on a VM with a local SSD, so take precautions.

Billing

VMs in the TERMINATED state are not charged for per-second usage and do not count toward your regional CPU quota. However, any resources attached to the virtual machine, such as static IPs and persistent disks, are charged until they are deleted.

You can choose to stop VMs that you are not using, saving you from being charged for VMs that aren't active. When you are ready, you can start the VMs again, with the same VM properties, metadata, and resources.

Stopping a VM

To stop a VM, use the Google Cloud Console, the gcloud tool, or the Compute Engine API.

Console

  1. In the Cloud Console, go to the VM instances page.

    Go to VM instances

  2. Select one or more VMs that you want to stop.

  3. Click Stop.

gcloud

Use the instances stop command and specify one or more VMs that you want to stop.

gcloud compute instances stop VM_NAME

Replace VM_NAME with the name of the VM you want to stop.

API

In the API, construct a POST request to stop a VM.

POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME/stop

Replace the following:

  • PROJECT_ID: the project your VM is in
  • ZONE: the zone where your VM is located
  • VM_NAME: the name of the VM you want to stop

A TERMINATED VM still exists with its configuration settings and instance metadata, but it loses its in-memory data and virtual machine state. Any resources that are attached to the terminated VM remain attached until you manually detach those resources or delete the VM.

After the VM is in the TERMINATED state, you can restart the instance or delete it. If you do not plan to restart the VM, delete it.

Stopping an VM through the OS

Optionally, you can stop a VM through the guest operating system by using the sudo shutdown -h now or sudo poweroff command. Run one of these commands while you are logged into the virtual machine:

sudo shutdown -h now
sudo poweroff

Restarting a stopped VM that doesn't have an encrypted disk

To start a stopped VM, use the instances().start method. This boots up a stopped VM that is currently in the TERMINATED state.

The start method restarts a VM in a TERMINATED state, whereas methods such as reset() and sudo reboot only work on VMs that are currently running. Almost all VMs can be restarted, as long as the VM is in a TERMINATED state.

Console

  1. In the Google Cloud Console, go to the VM instances page.

    Go to the VM instances page

  2. Select the boxes next to one or more VMs to start.

  3. Click Start.

gcloud

To start your VMs using gcloud compute:

gcloud compute instances start VM_NAME

Replace VM_NAME with the name of the VM you want to start.

API

In the API, make a POST request to the following URI:

https://www.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME/start

Replace the following:

  • PROJECT_ID: the project your VM is in
  • ZONE: the zone where your VM is located
  • VM_NAME: the name of the VM you want to start

To restart your VMs using the client libraries, construct a request to the instances().start method:

def restartInstance(auth_http, gce_service):
  request = gce_service.instances().start(project="myproject", zone="us-central1-a", instance="example-instance")
  response = request.execute(auth_http)

  print response

For more information about this method, see the instances().start reference documentation.

Restarting a VM that has encrypted disks

If the VM you want to restart uses customer-supplied encryption keys, you must provide those keys when trying to restart the VM.

Console

  1. In the Google Cloud Console, go to the VM instances page.

    Go to the VM Instances page

  2. Click the name of the VM that you want to start. This opens the instance details page.

  3. Click the Start button. A window opens where you can specify encryption keys for the devices that are attached to this VM.

  4. Specify encryption keys for each of the encrypted disks that are attached to this VM.

  5. Click Start to start the VM.

gcloud

When you start the VM, provide the key using the --csek-key-file flag and the name of the disk. If you are using an RSA-wrapped key, use the gcloud beta component:

gcloud compute instances start VM_NAME \
    --csek-key-file ENCRYPTION_KEY

Replace the following:

  • INSTANCE_NAME: the name of the VM
  • ENCRYPTION_KEY: the encryption key that you use to encrypt persistent disks that are attached to the VM

API

In the API, construct a POST request to the instances.startWithEncryptionKey method:

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"
       }
    }
  ]
}

Replace the following:

  • PROJECT_ID: your project ID.
  • ZONE: the zone for this instance.
  • INSTANCE_NAME: the name of the instance.
  • ENCRYPTION_TYPE: the type of disk encryption that you are using: rawKey, kmsKeyName, or rsaEncryptedKey. When using rsaEncryptedKey, you must use the beta API instead of the v1 API.
  • ENCRYPTION_KEY: the encryption key that you use to encrypt persistent disks that are attached to the instance. Keys of type rawKey or rsaEncryptedKey must be base64-encoded. Additional steps must be taken to prepare a key of type rsaEncryptedKey; for more information, see RSA key wrapping.
  • DISK_URL: the resource URL corresponding to the full resource name of the attached disk that is encrypted with a customer-supplied encryption key.

Resetting a VM

Performing a reset on your VM is similar to doing a hard reset on your computer where you might press a reset button or press and hold the power button. Resetting an VM forcibly wipes the memory contents of the machine and resets the virtual machine to its initial state. The VM does not perform a clean shutdown of the guest OS. Throughout this process, the VM remains in RUNNING state.

You can perform a reset on a running VM by using the Reset button in the Cloud Console , the instances reset command in gcloud, or by making a POST request in the API.

Console

  1. In the Google Cloud Console, go to the VM instances page.

    Go to the VM instances page

  2. Select one or more VMs to reset.

  3. Click Reset.

gcloud

To reset your VM using gcloud compute:

gcloud compute instances reset VM_NAME

Replace VM_NAME with the name of the VM you want to reset.

API

In the API, make a POST request to the following URI, replacing the project, zone, and VM name appropriately:

https://www.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME/reset

Replace the following:

  • PROJECT_ID: the project your VM is in
  • ZONE: the zone where your VM is located
  • VM_NAME: the name of the VM you want to reset

To reset your instance using the client libraries, construct a request to the instances().reset method:

def resetInstance(auth_http, gce_service):
  request = gce_service.instances().reset(project="myproject", zone="us-central1-a", instance="example-instance")

  print response

For more information about this method, see the instances().reset reference documentation.

After running the reset command, the subsequent zone operation returns DONE after the instance has completely shut down.

Using other restart methods

You can also choose to reset your VM using the following methods as well:

  • sudo reboot (Linux only): Call this method from within the VM. This method wipes the memory and re-initializes the VM with the original metadata, image, and persistent disks. This command does not pick up any updated versions of the image, and the VM retains the same ephemeral IP address. This is similar to restarting your computer.
  • Rebooting a Windows VM: You can reboot a Windows VM, similar to sudo reboot above, by using the Start menu. In the Start menu, click the arrow next to Log off, and click Restart.
  • gcloud compute instances delete followed by gcloud compute instances create: This is a completely destructive restart that initializes the VM with any information passed into gcloud compute instances create. You can then select any new images or other resources you'd like to use. The restarted VM will probably have a different IP address. This method potentially swaps the physical machine hosting the VM.

What's next