Stopping and starting a 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 a VM. For more information about stopping and suspending a VM, see VM instance life cycle.

You can stop a VM temporarily if you no longer need it and restart it later. 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. If you need to retain the guest OS and application state, suspend the VM instead. 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 machine type recommendations.

Stopping a VM causes Compute Engine to send the ACPI shutdown signal to the VM. Modern guest operating systems (OS) 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 OS to finish shutting down and then transitions the VM to the TERMINATED state.

Before you begin

Restrictions

You cannot stop and restart a VM with a local SSD attached. However, if the VM uses local SSDs, Compute Engine does not prevent you from shutting down the VM from inside the guest OS. If you do shut down a VM using local SSDs through the guest OS, you can't restart the VM and the data on the local SSD is lost. Make sure that you migrate your critical data from the local SSD to a persistent disk or to another VM before deleting the VM.

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 VM, such as persistent disks and external IP addresses, are charged until they are deleted. To stop being charged for attached resources, you can reconfigure a stopped VM to detach those resources, and then delete the resources.

You can choose to stop VMs that you're 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.

Stop 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

To stop a VM, use the gcloud compute 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

To stop a VM, construct a POST request using the instances.stop method:

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.

When a VM is in the TERMINATED state, you can restart the VM or delete it. If you don't plan to restart the VM, delete it.

Stop a VM through the OS

You can optionally stop a VM through the guest OS by using the sudo shutdown -h now or sudo poweroff command. Use these commands to stop a VM that uses local SSDs. Run one of these commands while you are logged into the VM:

sudo shutdown -h now
sudo poweroff

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

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

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

Console

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

    Go to VM instances

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

  3. Click Start/Resume.

gcloud

To start a VM, use the gcloud compute instances start command and specify one or more VMs that you want to start:

gcloud compute instances start VM_NAME

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

API

To start a VM, construct a POST request using the instances.start method:

POST 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 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

Restart 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 VM instances

  2. Select the VM that you want to start.

  3. Click Start/Resume. 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

To start a VM that uses encryption keys, use the gcloud compute instances start command with the --csek-key-file flag. If you are using an RSA-wrapped key, use the gcloud beta command.

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

Replace the following:

  • VM_NAME: the name of the VM you want to start
  • ENCRYPTION_KEY_FILE: the relative path to the JSON file that contains the encryption key. This key is used to encrypt persistent disks that are attached to the VM. For more information, see the Encryption key file format.

API

To start a VM that has encryption keys, construct a POST request using 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 project your VM is in
  • ZONE: the zone where your VM is located
  • VM_NAME: the name of the VM you want to start
  • 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: 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 VM. Keys of type rawKey or rsaEncryptedKey must be base64-encoded. Prepare a key of type rsaEncryptedKey by following the instructions in RSA key wrapping.

Reset 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 a VM forcibly wipes the memory contents of the machine and resets the VM to its initial state. The VM does not perform a clean shutdown of the guest OS. Throughout this process, the VM remains in the RUNNING state.

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

Console

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

    Go to VM instances

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

  3. Click Reset.

gcloud

To reset a running VM, use the gcloud compute instances reset command and specify one or more VMs that you want to reset:

gcloud compute instances reset VM_NAME

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

API

To reset a VM, construct a POST request using the instances.reset method:

 POST 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 VM using 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

When you run the reset command, the subsequent zone operation returns DONE after the VM has shut down.

Other restart methods

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

  • 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 in the preceding example, by using the Start menu. In the Start menu, click the arrow next to Log off, and click Restart.

What's next