> [!WARNING]
>
> **Preview
> --- SSH support for Cloud Run**
>
>
> This feature is
>
> subject to the "Pre-GA Offerings Terms" in the General Service Terms section of the
> [Service Specific
> Terms](https://docs.cloud.google.com/terms/service-terms#1).
>
> Pre-GA features are available "as is" and might have limited support.
>
> For more information, see the
> [launch stage descriptions](https://cloud.google.com/products/#product-launch-stages).

SSH lets you establish a secure, interactive shell connection to your running
container instances. This provides direct access to the container's file system
and runtime environment, enabling you to inspect system resources, verify
configurations, and troubleshoot.

SSH for Cloud Run uses the following services to secure
connections:

- [Identity-Aware Proxy TCP forwarding](https://docs.cloud.google.com/iap/docs/tcp-forwarding-overview.md.txt) proxies SSH connections, so requests must pass authentication and authorization checks before they reach your container. You can extend IAP access policies with [context-aware access](https://docs.cloud.google.com/iap/docs/cloud-iap-context-aware-access-howto.md.txt) to restrict access based on attributes such as IP address and end-user device.
- [OS Login](https://docs.cloud.google.com/compute/docs/oslogin.md.txt) uses [SSH certificate authentication](https://docs.cloud.google.com/compute/docs/oslogin/certificates.md.txt) to authenticate users. When you connect, the gcloud CLI mints a short-lived SSH certificate for you that expires after five minutes. The certificate is validated inside your container. Google checks IAM permissions for every login attempt.

SSH for Cloud Run automatically adds the required Google-managed
binaries---including `sshd`, certificate validation binaries, and the MirBSD Korn
Shell (`mksh`)---to your container's file system. Auto-provisioning `mksh` ensures
that a shell program is available during your session even if your container
image doesn't include one (for example, in distroless or minimal images). These
binaries are only auto-provisioned and accessible for as long as SSH connections
are open, and are removed once the session ends. Existing SSH configurations,
including `/etc/passwd` entries, are ignored by Cloud Run SSH in
favor of Google-managed configurations.

Third-party open source copyright notices for Google-managed SSH dependencies
are mounted inside your container when an SSH session is established at the
following path: `/usr/share/licenses/google_ssh/THIRD_PARTY_NOTICES`.

## SSH sessions and service instances

When you connect to a service using SSH, note the following:

- If no instance of the service is running, Cloud Run starts a new instance for the SSH session.
- Cloud Run aims to keep the service instance active for the duration of the SSH session. You are billed for the instance as usual while it is active.
- Required binaries (`sshd`, certificate validation tools, and `mksh`) are auto-provisioned only while SSH connections remain open. When the session ends, these binaries are removed from the container.

## Limitations

The following limitations apply to SSH:

- SSH sessions last for a maximum of 24 hours. After 24 hours, the session is disconnected and you must reconnect.
- IAP disconnects SSH sessions after one hour of inactivity. For more information, see [IAP TCP forwarding known limitations](https://docs.cloud.google.com/iap/docs/using-tcp-forwarding.md.txt#known_limitations).
- SSH is only available for services that run on Cloud Run's [second generation environment](https://docs.cloud.google.com/run/docs/configuring/execution-environments.md.txt) and [Cloud Run instances](https://docs.cloud.google.com/run/docs/instances/create-and-manage-instances.md.txt).
- When using SSH, host keys in the image are overwritten. The keys remain overwritten after the SSH session ends, until the service instance is restarted.
- PuTTY is not supported on Windows. Use the OpenSSH client.
- You don't need a shell (such as `bash` or `sh`) or an SSH server in your container image. Cloud Run automatically provisions `sshd` and `mksh` while SSH connections are open. However, other debugging utilities (such as `curl` or package managers) aren't provisioned automatically.
- Extra care should be taken to secure your container when using SSH. Consider security best practices such as running application code as a non-root user.
- When an SSH connection cold-starts a multi-container service with [container startup order](https://docs.cloud.google.com/run/docs/configuring/services/containers.md.txt#container-ordering) (`container-dependencies`), the request is routed before the container environment is ready. Cloud Run returns an HTTP `500` error and immediately stops the new instance. If you connect using the Google Cloud CLI, the command fails. To work around this issue, connect using SSH only when an instance is already running. For example, send a request to the service first to start an instance, or configure [minimum instances](https://docs.cloud.google.com/run/docs/configuring/min-instances.md.txt).

## Before you begin

1. Enable the Cloud Run Admin API, Cloud Resource Manager API, Identity-Aware Proxy API, and Cloud OS Login APIs:

   ```bash
     gcloud services enable run.googleapis.com \
         cloudresourcemanager.googleapis.com \
         iap.googleapis.com \
         oslogin.googleapis.com
     
   ```
2. [Install and initialize](https://docs.cloud.google.com/sdk/docs.md.txt) the gcloud CLI.
3. Update components:

   ```bash
   gcloud components update
   ```
4. **Windows:** Ensure the OpenSSH client is installed.

   OpenSSH Client is typically installed by default on Windows 10 and later. If it is not installed, you can install it by running the following command in PowerShell as an Administrator:

   ```powershell
   Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0
   ```

   For more information, see [Install OpenSSH](https://learn.microsoft.com/en-us/windows-server/administration/openssh/openssh_install_firstuse).
5. Review the Cloud Run [pricing](https://cloud.google.com/run/pricing) page. To generate a cost estimate based on your projected usage, use the [pricing calculator](https://cloud.google.com/products/calculator). SSH bills for network egress bytes. You are also billed as usual for the instance while it is kept active for an SSH session.
6. If your project uses [VPC Service Controls](https://docs.cloud.google.com/run/docs/securing/using-vpc-service-controls.md.txt) (VPC-SC), note that the tunneling process checks `iaptunnel.googleapis.com` as the service name, rather than `run.googleapis.com`.

### Required roles


To get the permissions that
you need to complete these steps,

ask your administrator to grant you the
following IAM roles:

- [Service Account User](https://docs.cloud.google.com/iam/docs/roles-permissions/iam.md.txt#iam.serviceAccountUser) (`roles/iam.serviceAccountUser`) on the service account
- [Cloud Run SSH Root Access](https://docs.cloud.google.com/iam/docs/roles-permissions/run.md.txt#run.sshRoot) (`roles/run.sshRoot`) on the service
- [IAP-secured Tunnel User](https://docs.cloud.google.com/iam/docs/roles-permissions/iap.md.txt#iap.tunnelResourceAccessor) (`roles/iap.tunnelResourceAccessor`) on the service
- [Cloud Run Developer](https://docs.cloud.google.com/iam/docs/roles-permissions/run.md.txt#run.developer) (`roles/run.developer`) on the service
- [Logs View Accessor](https://docs.cloud.google.com/iam/docs/roles-permissions/logging.md.txt#logging.viewAccessor) (`roles/logging.viewAccessor`) on the project
- [Browser](https://docs.cloud.google.com/iam/docs/roles-permissions/browser.md.txt#browser) (`roles/browser`) on the project
- [Service Usage Consumer](https://docs.cloud.google.com/iam/docs/roles-permissions/serviceusage.md.txt#serviceusage.serviceUsageConsumer) (`roles/serviceusage.serviceUsageConsumer`) on the project
- [IAP Policy Admin](https://docs.cloud.google.com/iam/docs/roles-permissions/iap.md.txt#iap.admin) (`roles/iap.admin`) on the project for IAP management operations


For more information about granting roles, see [Manage access to projects, folders, and organizations](https://docs.cloud.google.com/iam/docs/granting-changing-revoking-access.md.txt).


You might also be able to get
the required permissions through [custom
roles](https://docs.cloud.google.com/iam/docs/creating-custom-roles) or other [predefined
roles](https://docs.cloud.google.com/iam/docs/roles-overview#predefined).

If you aren't a member of the organization that the project is part of, Cloud Run
blocks the SSH access. To gain SSH access, add the role, `roles/compute.osLoginExternalUser`
or by using a custom role with the `compute.oslogin.updateExternalUser`
permission.

You can grant the IAP-secured Tunnel User role at the project level, to restrict
this role for a specific Cloud Run service or instance, run the
following command:

### For a specific service

```bash
gcloud beta iap tcp add-iam-policy-binding \
    --resource-type=cloud-run \
    --service=SERVICE \
    --region=REGION \
    --member=MEMBER \
    --role=roles/iap.tunnelResourceAccessor
```

Replace the following:

- `SERVICE`: the name of your service.
- `REGION`: the region that your service is deployed in.
- `MEMBER`: the identity to grant access to. This should be in the format `user|group|serviceAccount:email` ---for example, `user:test-user@gmail.com`.

### For a specific instance

```bash
gcloud beta iap tcp add-iam-policy-binding \
    --resource-type=cloud-run \
    --instance=INSTANCE \
    --region=REGION \
    --member=MEMBER \
    --role=roles/iap.tunnelResourceAccessor
```

Replace the following:

- `INSTANCE`: the name of your instance.
- `REGION`: the region that your instance is deployed in.
- `MEMBER`: the identity to grant access to. This should be in the format `user|group|serviceAccount:email` ---for example, `user:test-user@gmail.com`.

To grant the `roles/iap.tunnelResourceAccessor` at the project and region
level, see [Configure IAP access policies](https://docs.cloud.google.com/iap/docs/ssh-cloud-run.md.txt#configure-policies).

## Use SSH with Cloud Run services

Configure service-level SSH access and connect to the service using SSH.

### Configure service-level SSH access

You can allow SSH access on a specific service using the gcloud CLI
or YAML:

### gcloud

To enable access on an existing service, use the following command:

```bash
gcloud beta run services update SERVICE --ssh \
    --region=REGION
```

Replace the following:

- `SERVICE`: the name of your service.
- `REGION`: the region that your service is deployed in.

You can also allow SSH access on a service by using the `--ssh` flag when
[deploying the service](https://docs.cloud.google.com/sdk/gcloud/reference/beta/run/deploy.md.txt).

### YAML

1. If you are creating a new service, skip this step.
   If you are updating an existing service, download its [YAML configuration](https://docs.cloud.google.com/run/docs/reference/yaml/v1.md.txt):

   ```bash
   gcloud run services describe SERVICE --format export > service.yaml
   ```
2. The following example contains the YAML configuration:

   ```yaml
   apiVersion: serving.knative.dev/v1
   kind: Service
   metadata:
     name: SERVICE
     labels:
       cloud.googleapis.com/location: REGION
     annotations:
       run.googleapis.com/ssh-enabled: "true"
       run.googleapis.com/launch-stage: BETA
   spec:
     template:
       spec:
         containers:
         - image: IMAGE_URL
   ```

   Replace the following:
   - `SERVICE`: the name of your Cloud Run service.
   - `REGION`: the Google Cloud region---for example, `us-central1`.
   - `IMAGE_URL`: a reference to the container image, for example, `us-docker.pkg.dev/cloudrun/container/hello:latest`. If you use Artifact Registry, the [repository](https://docs.cloud.google.com/artifact-registry/docs/repositories/create-repos.md.txt#docker) <var translate="no">REPO_NAME</var> must already be created. The URL follows the format of `LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/PATH:TAG`
3. Create or update the service using the following command:

   ```bash
   gcloud run services replace service.yaml
   ```

   The `gcloud run services replace` command defaults to using `service.yaml` file if present.

You can disable SSH access on a per-service basis using the gcloud CLI
or YAML:

### gcloud

To disable access on the service, use the following command:

```bash
gcloud beta run services update SERVICE --no-ssh \
    --region=REGION
```

Replace the following:

- `SERVICE`: the name of your service.
- `REGION`: the region that your service is deployed in.

### YAML

1. If you are creating a new service, skip this step.
   If you are updating an existing service, download its [YAML configuration](https://docs.cloud.google.com/run/docs/reference/yaml/v1.md.txt):

   ```bash
   gcloud run services describe SERVICE --format export > service.yaml
   ```
2. The following example contains the YAML configuration:

   ```yaml
   apiVersion: serving.knative.dev/v1
   kind: Service
   metadata:
     name: SERVICE
     labels:
       cloud.googleapis.com/location: REGION
     annotations:
       run.googleapis.com/ssh-enabled: "false"
       run.googleapis.com/launch-stage: BETA
   spec:
     template:
       spec:
         containers:
         - image: IMAGE_URL
   ```

   Replace the following:
   - `SERVICE`: the name of your Cloud Run service.
   - `REGION`: the Google Cloud region---for example, `us-central1`.
   - `IMAGE_URL`: a reference to the container image, for example, `us-docker.pkg.dev/cloudrun/container/hello:latest`. If you use Artifact Registry, the [repository](https://docs.cloud.google.com/artifact-registry/docs/repositories/create-repos.md.txt#docker) <var translate="no">REPO_NAME</var> must already be created. The URL follows the format of `LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/PATH:TAG`
3. Create or update the service using the following command:

   ```bash
   gcloud run services replace service.yaml
   ```

   The `gcloud run services replace` command defaults to using `service.yaml` file if present.

### Connect to the service with SSH

To connect to a service by using SSH, use the gcloud CLI:

### Console

1. In the Google Cloud console, go to the Cloud Run **Services** page:

   [Go to Cloud Run](https://console.cloud.google.com/run/services)
2. Click a service to open the **Service details** page.

3. Select **SSH**. This opens Cloud Shell with the gcloud
   SSH command.

4. Press **Enter**.

5. If you are prompted to enter a passphrase for the SSH key, you can leave it
   empty. However, if you enter a passphrase, make sure you use
   the same passphrase for any subsequent SSH sessions from the same
   workspace.

6. When the SSH session is successfully completed, Cloud Run displays the
   following message:

       Project: PROJECT_ID
       Region: `REGION`
       Service: SERVICE
       Revision: REVISION
       Instance: INSTANCE_ID
       Container: CONTAINER
       Image: IMAGE

7. To end your SSH session, type `exit`. When the session ends, the auto-provisioned
   SSH and `mksh` binaries are removed from the container's file system. Any changes
   you made on the container persist for as long as the container is kept alive.
   Exiting the session does not restart or kill the container.

### gcloud

To connect to a service by using SSH, use the following Google Cloud CLI command:

```bash
gcloud beta run services ssh SERVICE --region=REGION --project=PROJECT_ID
```

Replace the following:

- `SERVICE`: the name of your service.
- `REGION`: the region that your service is deployed in.
- `PROJECT_ID`: the Google Cloud project ID.

If you are prompted to enter a passphrase for the SSH key, you can leave it
empty. However, if you enter a passphrase, make sure you use the same
passphrase for any subsequent SSH sessions from the same workspace.

When the SSH session is successfully completed, Cloud Run displays the
following message:

    Project: PROJECT_ID
    Region: `REGION`
    Service: SERVICE
    Revision: REVISION
    Instance: INSTANCE_ID
    Container: CONTAINER
    Image: IMAGE

To end your SSH session, type `exit`. When the session ends, the auto-provisioned
SSH and `mksh` binaries are removed from the container's file system. Any changes
you made on the container persist for as long as the container is kept alive.
Exiting the session does not restart or kill the container.

### Specify a service instance

To connect to a specific service instance by using SSH, use the Google Cloud CLI.

### Console

1. In the Google Cloud console, go to the Cloud Run **Services** page:

   [Go to Cloud Run](https://console.cloud.google.com/run/services)
2. Click the service you are interested in to open the **Service details**
   page.

3. Select **Logs**.

4. Click the log entry for your service instance to expand it.

5. Select **SSH into this instance**. This opens Cloud Shell with the
   gcloud SSH command.

6. Press **Enter**.

### gcloud

To connect to a Cloud Run service instance by using SSH, use the following Google Cloud CLI command:

```bash
gcloud beta run services ssh SERVICE --region=REGION --project=PROJECT_ID \
  --revision=REVISION \
  --instance=INSTANCE_ID
```

Replace the following:

- `SERVICE`: the name of your service.
- `REGION`: the region that your service is deployed in.
- `PROJECT_ID`: the Google Cloud project ID.
- `REVISION`: the name of the revision.
- `INSTANCE_ID`: the instance ID. To find the service instance ID, go to the **Logs** page in the **Observability** section. The instance ID is located in the `labels` field for a log entry. You can't find the instance ID if you terminate the specific instance before connecting to it.

### Specify a revision

To connect to a specific revision by using SSH, use the Google Cloud CLI.

### Console

1. In the Google Cloud console, go to the Cloud Run **Services** page:

   [Go to Cloud Run](https://console.cloud.google.com/run/services)
2. Click the service you are interested in to open the **Service details**
   page.

3. Select **Revision history**.

4. Click the ellipsis icon to the right of the revision you want to connect to
   using SSH.

5. Select **SSH into revisions**. This opens Cloud Shell with the
   gcloud SSH command.

6. Press **Enter**.

### gcloud

To connect to a Cloud Run service revision by using SSH, use the following Google Cloud CLI command:

```bash
gcloud beta run services ssh SERVICE --region=REGION --project=PROJECT_ID --revision=REVISION
```

Replace the following:

- `SERVICE`: the name of your service.
- `REGION`: the region that your service is deployed in.
- `PROJECT_ID`: the Google Cloud project ID.
- `REVISION`: the name of the revision.

## Use SSH with Cloud Run instances

Configure instance-level SSH access and connect to the instance using SSH.

### Configure instance-level SSH access

SSH is enabled by default for instances.

You can disable SSH access on a per-instance basis using the
gcloud CLI or YAML:

### gcloud

To disable access on the instance, use the following command:

```bash
gcloud beta run instances update INSTANCE --no-ssh \
    --region=REGION
```

Replace the following:

- `INSTANCE`: the name of your instance.
- `REGION`: the region that your instance is deployed in.

### YAML

1. If you are creating a new instance, skip this step.
   If you are updating an existing instance, download its [YAML configuration](https://docs.cloud.google.com/run/docs/reference/yaml/v1.md.txt):

   ```bash
   gcloud beta run instances describe INSTANCE --format export > instance.yaml
   ```
2. The following example contains the YAML configuration:

   ```yaml
   apiVersion: run.googleapis.com/v1
   kind: Instance
   metadata:
     name: INSTANCE
     labels:
       cloud.googleapis.com/location: REGION
     annotations:
       run.googleapis.com/ssh-enabled: "false"
       run.googleapis.com/launch-stage: BETA
   spec:
     containers:
     - image: IMAGE_URL
   ```

   Replace the following:
   - `INSTANCE`: the name of your Cloud Run instance.
   - `REGION`: the Google Cloud region---for example, `us-central1`.
   - `IMAGE_URL`: a reference to the container image, such as `us-docker.pkg.dev/cloudrun/container/hello:latest`.
3. Create or update the instance using the following command:

   ```bash
   gcloud beta run instances replace instance.yaml
   ```

To re-enable SSH access on an existing instance, use the gcloud CLI
or YAML.

### gcloud

To enable access on an existing instance, use the following command:

```bash
gcloud beta run instances update INSTANCE --ssh \
    --region=REGION
```

Replace the following:

- `INSTANCE`: the name of your instance.
- `REGION`: the region that your instance is deployed in.

### YAML

1. If you are creating a new instance, skip this step.
   If you are updating an existing instance, download its [YAML configuration](https://docs.cloud.google.com/run/docs/reference/yaml/v1.md.txt):

   ```bash
   gcloud beta run instances describe INSTANCE --format export > instance.yaml
   ```
2. The following example contains the YAML configuration:

   ```yaml
   apiVersion: run.googleapis.com/v1
   kind: Instance
   metadata:
     name: INSTANCE
     labels:
       cloud.googleapis.com/location: REGION
     annotations:
       run.googleapis.com/ssh-enabled: "true"
       run.googleapis.com/launch-stage: BETA
   spec:
     containers:
     - image: IMAGE_URL
   ```

   Replace the following:
   - `INSTANCE`: the name of your Cloud Run instance.
   - `REGION`: the Google Cloud region---for example, `us-central1`.
   - `IMAGE_URL`: a reference to the container image, such as `us-docker.pkg.dev/cloudrun/container/hello:latest`.
3. Create or update the instance using the following command:

   ```bash
   gcloud beta run instances replace instance.yaml
   ```

### Connect to the instance with SSH

To connect to an instance by using SSH, use the following Google Cloud CLI command:

```bash
  gcloud beta run instances ssh INSTANCE --region=REGION --project=PROJECT_ID
```

Replace the following:

- `INSTANCE`: the name of your instance.
- `REGION`: the region that your instance is deployed in.
- `PROJECT_ID`: the Google Cloud project ID.

If you are prompted to enter a passphrase for the SSH key, you can leave it
empty. However, if you enter a passphrase, make sure you use the same
passphrase for any subsequent SSH sessions from the same workspace.

When the SSH session is successfully completed, Cloud Run displays the
following message:

      Project: PROJECT_ID
      Region: `REGION`
      Instance: INSTANCE
      Revision: REVISION
      Container: CONTAINER
      Image: IMAGE

To end your SSH session, type `exit`. When the session ends, the auto-provisioned
SSH and `mksh` binaries are removed from the container's file system.

## Use the OpenSSH client to connect

To use your OpenSSH client to connect:

1. Run the [`gcloud beta run services ssh` command](https://docs.cloud.google.com/run/docs/troubleshooting/ssh.md.txt#use-ssh-service) on the target service. This initiates the SSH tunnel and generates the certificate.
2. Add the following to your SSH configuration:

       Host cloud-run-ssh
           HostName cloud-run-default
           User root
           IdentityFile /Users/USER/.ssh/google_compute_engine
           CertificateFile /Users/USER/.ssh/google_compute_engine_cert/PROJECT_ID_REGION_service_SERVICE-cert.pub
           CheckHostIP no
           HashKnownHosts no
           HostKeyAlias cloud-run-default
           IdentitiesOnly yes
           StrictHostKeyChecking no
           UserKnownHostsFile /dev/null
           ProxyUseFdpass no
           ProxyCommand /usr/local/bin/python3 -S /Users/USER/google-cloud-sdk/lib/gcloud.py beta run start-iap-tunnel --project_number=PROJECT_NUMBER --project_id=PROJECT_ID --workload_type=service --deployment_name=SERVICE/revisions/REVISION --region=REGION --iap-tunnel-url-override=wss://REGION.ssh.run.app/v4

   Replace the following:
   - `USER`: the username on your local machine.
   - `PROJECT_ID`: the Google Cloud project ID.
   - `PROJECT_NUMBER`: the Google Cloud project number.
   - `REGION`: the region that your service is deployed in.
   - `SERVICE`: the name of your service.
   - `REVISION`: the name of the revision.

   If the gcloud CLI is installed in a different folder, you might
   need to update the location in the last line.
3. Run the following command:

   ```bash
   ssh cloud-run-ssh
   ```

Your SSH certificate expires after five minutes. If you try to establish an SSH
connection with your service after five minutes, you must rerun
`gcloud beta run services ssh` to re-generate the certificate.

## SSH Access Logs

To capture audit logs for SSH key management, you must enable audit logs for the
Cloud OS Login API. See [View OS Login audit logs](https://docs.cloud.google.com/compute/docs/oslogin/view-audit-logs.md.txt).
To access SSH logs of Cloud Run resources, you must have access
to Cloud Audit Logs for that project.

The following example queries all Cloud Run services for the past
day:

```bash
gcloud logging read '
  logName="projects/PROJECT_ID/logs/cloudaudit.googleapis.com%2Fdata_access" AND
  protoPayload.serviceName="oslogin.googleapis.com" AND
  protoPayload.request.instance:"run.googleapis.com"
' --project=PROJECT_ID \
  --freshness=1d \
  --format="table(timestamp, protoPayload.authenticationInfo.principalSubject:label=USER, protoPayload.request.instance:label=CLOUD_RUN_SERVICE)"
```

## Secure and control SSH access

You can control who can use SSH and how it is restricted in your environment.

### Disable SSH access with organization policies

To set an organization policy to disable SSH, use the [custom constraint](https://docs.cloud.google.com/run/docs/securing/custom-constraints.md.txt#example_constraints) that restricts enabling SSH debugging
access on Cloud Run services.

## What's next

- Learn more about [troubleshooting Cloud Run issues](https://docs.cloud.google.com/run/docs/troubleshooting.md.txt).
- Learn more about [troubleshooting Cloud Run services](https://docs.cloud.google.com/run/docs/tutorials/local-troubleshooting.md.txt).
- Read about [known issues in Cloud Run](https://docs.cloud.google.com/run/docs/known-issues.md.txt).