Connect using service accounts

This document describes how to use a service account to connect to Compute Engine virtual machine (VM) instances using SSH. Setting up SSH for a service account enables you to configure apps to use SSH, which can help you to automate your workloads.

Before you begin

Manually connect to VMs as a service account

To connect to VMs as a service account, use one of the following methods:

Directly impersonate service account

Permissions required for this task

To perform this task, you must have the following permissions:

Use the gcloud CLI --impersonate-service-account flag to connect directly to a VM using a service account's identity. Run the following command to connect to a VM as a service account:

gcloud compute ssh VM_NAME \
    --impersonate-service-account=SERVICE_ACCOUNT_EMAIL

Replace the following:

  • VM_NAME: the name of the VM you want to connect to the service account as.
  • SERVICE_ACCOUNT_EMAIL: the email address associated with the service account.

Impersonate service account from a VM

Permissions required for this task

To perform this task, you must have the following permissions:

You must additionally assign your service account to a VM and set the cloud-platform access scope on the VM.

Impersonate a service account from another VM by doing the following:

  1. Connect to the VM that runs as a service account.
  2. From the VM that runs as a service account, connect to other VMs using the same methods.

What's next

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2026-02-25 UTC.