Configure apps to use SSH

This document describes how to configure apps to programmatically connect between two virtual machine (VM) instances using SSH and OS Login. Enabling apps to use SSH can be useful for automating system management processes.

All code samples used in this guide is hosted on the GoogleCloudPlatform/python-docs-samples GitHub page.

Before you begin

  • Set up SSH for a service account.
  • Set up OS Login on your project, or on a VM that runs as a service account.
  • If you haven't already, 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 as follows.

    Select the tab for how you plan to use the samples on this page:


    When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.


    1. Install the Google Cloud CLI, then initialize it by running the following command:

      gcloud init
    2. Set a default region and zone.

Set up an SSH app

Set up your app to manage SSH keys and initiate SSH connections to Compute Engine VMs. At a high level, your app should do the following:

  1. Import the Google OS Login library to build client libraries, which enables you to authenticate with the OS Login API.
  2. Initialize the OS Login Client object to enable your app to use OS Login.
  3. Implement a create_ssh_key() method that generates an SSH key for the VM's service account and adds the public key to the service account.
  4. Call the get_login_profile() method from the OS Login library to get the POSIX user name that the service account uses.
  5. Implement a run_ssh() method to execute a remote SSH command.
  6. Remove the temporary SSH key files.

Sample SSH app

The sample app demonstrates a possible implementation of an SSH app. In this example, the app uses the run_ssh() method to execute a command on a remote instance and return the command output.

Example of using the OS Login API to apply public SSH keys for a service
account, and use that service account to run commands on a remote
instance over SSH. This example uses zonal DNS names to address instances
on the same internal VPC network.
from __future__ import annotations

import argparse
import subprocess
import time
from typing import Optional
import uuid

from import oslogin_v1
import requests

HEADERS = {"Metadata-Flavor": "Google"}

def execute(
    cmd: list[str],
    cwd: Optional[str] = None,
    capture_output: bool = False,
    env: Optional[dict] = None,
    raise_errors: bool = True,
) -> tuple[int, str]:
    Run an external command (wrapper for Python subprocess).

        cmd: The command to be run.
        cwd: Directory in which to run the command.
        capture_output: Should the command output be captured and returned or just ignored.
        env: Environmental variables passed to the child process.
        raise_errors: Should errors in run command raise exceptions.

        Return code and captured output.
    print(f"Running command: {cmd}")
    process =
        stdout=subprocess.PIPE if capture_output else subprocess.DEVNULL,
    output = process.stdout
    returncode = process.returncode

    if returncode:
        print(f"Command returned error status {returncode}")
        if capture_output:
            print(f"With output: {output}")

    return returncode, output

def create_ssh_key(
    oslogin_client: oslogin_v1.OsLoginServiceClient,
    account: str,
    expire_time: int = 300,
) -> str:
    Generates a temporary SSH key pair and apply it to the specified account.

        oslogin_client: OS Login client object.
        account: Name of the service account this key will be assigned to.
            This should be in form of `user/<service_account_username>`.
        expire_time: How many seconds from now should this key be valid.

        The path to private SSH key. Public key can be found by appending `.pub`
        to the file name.

    private_key_file = f"/tmp/key-{uuid.uuid4()}"
    execute(["ssh-keygen", "-t", "rsa", "-N", "", "-f", private_key_file])

    with open(f"{private_key_file}.pub") as original:
        public_key =

    # Expiration time is in microseconds.
    expiration = int((time.time() + expire_time) * 1000000)

    request = oslogin_v1.ImportSshPublicKeyRequest()
    request.parent = account
    request.ssh_public_key.key = public_key
    request.ssh_public_key.expiration_time_usec = expiration

    print(f"Setting key for {account}...")

    # Let the key properly propagate

    return private_key_file

def run_ssh(cmd: str, private_key_file: str, username: str, hostname: str) -> str:
    Runs a command on a remote system.

        cmd: command to be run.
        private_key_file: private SSH key to be used for authentication.
        username: username to be used for authentication.
        hostname: hostname of the machine you want to run the command on.

        Output of the executed command.
    ssh_command = [
    print(f"Running ssh command: {' '.join(ssh_command)}")
    tries = 0
    while tries < 3:
            ssh =
                env={"SSH_AUTH_SOCK": ""},
        except (subprocess.CalledProcessError, subprocess.TimeoutExpired) as err:
            tries += 1
            if tries == 3:
                if isinstance(err, subprocess.CalledProcessError):
                        f"Failed to run SSH command (return code: {err.returncode}. Output received: {err.output}"
                    print("Failed to run SSH - timed out.")
                raise err
            return ssh.stdout

def main(
    cmd: str,
    project: str,
    instance: Optional[str] = None,
    zone: Optional[str] = None,
    account: Optional[str] = None,
    hostname: Optional[str] = None,
    oslogin: Optional[oslogin_v1.OsLoginServiceClient] = None,
) -> str:
    Runs a command on a remote system.

        cmd: command to be executed on the remote host.
        project: name of the project in which te remote instance is hosted.
        instance: name of the remote system instance.
        zone: zone in which the remote system resides. I.e. us-west3-a
        account: account to be used for authentication.
        hostname: hostname of the remote system.
        oslogin: OSLogin service client object. If not provided, a new client will be created.

        The commands output.
    # Create the OS Login API object.
    if oslogin is None:
        oslogin = oslogin_v1.OsLoginServiceClient()

    # Identify the service account ID if it is not already provided.
    account = (
        account or requests.get(SERVICE_ACCOUNT_METADATA_URL, headers=HEADERS).text

    if not account.startswith("users/"):
        account = f"users/{account}"

    # Create a new SSH key pair and associate it with the service account.
    private_key_file = create_ssh_key(oslogin, account)
        # Using the OS Login API, get the POSIX username from the login profile
        # for the service account.
        profile = oslogin.get_login_profile(name=account)
        username = profile.posix_accounts[0].username

        # Create the hostname of the target instance using the instance name,
        # the zone where the instance is located, and the project that owns the
        # instance.
        hostname = hostname or f"{instance}.{zone}.c.{project}.internal"

        # Run a command on the remote instance over SSH.
        result = run_ssh(cmd, private_key_file, username, hostname)

        # Print the command line output from the remote instance.
        return result
        # Shred the private key and delete the pair.
        execute(["shred", private_key_file])
        execute(["rm", private_key_file])
        execute(["rm", f"{private_key_file}.pub"])

if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
        "--cmd", default="uname -a", help="The command to run on the remote instance."
    parser.add_argument("--project", help="Your Google Cloud project ID.")
    parser.add_argument("--zone", help="The zone where the target instance is located.")
    parser.add_argument("--instance", help="The target instance for the ssh command.")
    parser.add_argument("--account", help="The service account email.")
        help="The external IP address or hostname for the target instance.",
    args = parser.parse_args()


Run the SSH app

After you create an app that uses SSH, you can run the app by following a process similar to the following example, which installs and runs the sample app. The libraries you install may differ, depending on the programming language the app uses.

Alternatively, you can write an app that imports and runs it directly.

  1. Connect to the VM that hosts the SSH app.

  2. On the VM, install pip and the Python 3 client library:

    sudo apt update && sudo apt install python3-pip -y && pip install --upgrade google-cloud-os-login requests
  3. Optional: If you are using the sample app, download it from GoogleCloudPlatform/python-docs-samples:

    curl -O
  4. Run the SSH app. The sample app uses argparse to accept variables from the command line. In this example, instruct the app to install and run cowsay on another VM in your project.

    python3 \
       --cmd 'sudo apt install cowsay -y && cowsay "It works!"' \
       --project=PROJECT_ID --instance=VM_NAME --zone=ZONE

    Replace the following:

    • PROJECT_ID: the project ID of the VM that the app is connecting to.
    • VM_NAME: the name of the VM that the app is connecting to.
    • ZONE: the zone of the VM that the app is connecting to.

    The output is similar to the following:

     It works!
          \   ^__^
           \  (oo)\_______
              (__)\       )\/\
                  ||----w |
                  ||     ||

What's next

  • Download and view the full code sample. The full sample includes a small example of using all of these methods together. Feel free to download it, change it, and run it to suit your needs.
  • Learn more about about how SSH connections work in Compute Engine, including SSH key configuration and storage.