Connect to Linux VMs using Identity-Aware Proxy


This document describes how to connect to a virtual machine (VM) instance through its internal IP address, using Identity-Aware Proxy (IAP) TCP forwarding.

IAP TCP forwarding enables you to establish an encrypted tunnel over which you can forward SSH connections to VMs. When you connect to a VM that uses IAP, IAP wraps the SSH connection inside HTTPS before forwarding the connection to the VM. Then, IAP checks if the you have the required IAM permissions and if you do, grants access to the VM.

If you need to connect to a VM that doesn't have external IP addresses and you can't use IAP, review the other methods listed in Connection options for internal-only VMs.

Before you begin

  • Create a firewall rule to enable connections from IAP.
  • If you haven't already, then 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 by selecting one of the following options:

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

    Console

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

    gcloud

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

      gcloud init
    2. Set a default region and zone.

Supported operating systems

These connection methods are supported for all public Linux images that are available on Compute Engine. For Fedora CoreOS images, you must set up SSH access before you can use these methods.

Connect to VMs

To connect to a VM, complete the steps in one of the following tabs.

Console

Tunnel SSH connections through a VM's internal IP address using SSH-in-Browser by doing the following:

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

    Go to VM instances

  2. In the list of virtual machine instances, click SSH in the row of the instance that you want to connect to.

gcloud

Tunnel SSH connections through a VM's internal IP address using the gcloud compute ssh command with the --tunnel-through-iap flag:

    In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  1. Connect to the VM by running the following command:

    gcloud compute ssh VM-NAME \
        --tunnel-through-iap
  2. Replace VM_NAME with the name of the VM that you want to connect to.

IAP Desktop

To connect to a VM using IAP Desktop, do the following:

  1. Install IAP Desktop on your workstation if you haven't already.

  2. Open IAP Desktop. The Add projects window opens.

  3. When prompted, sign in using the Google account that has access to the project with the VMs you want to connect to.

  4. In the Add projects window, enter the project ID or name of the project that contains the VMs you want to connect to.

  5. In the Project Explorer window, right-click the name of the VM again and select Connect to connect to the VM.

PuTTY app

Tunnel SSH connections through a VM's internal IP address using PuTTY, by doing the following:

  1. Add an SSH key to the VM if you haven't already.
  2. If your workstation doesn't already have the PuTTY app installed, download the PuTTY package files.
  3. In the Google Cloud console, go to the VM Instances page and find the name of the VM that you want to connect to.

    Go to VM Instances

  4. Open the PuTTY app. A connection configuration window opens.
  5. In the Host Name field, enter the username associated with the SSH key, and the name of the VM that you want to connect to. Use the following format:

    USERNAME@VM_NAME

    Replace the following:

    • USERNAME: your username. If you manage your SSH keys in metadata, the username is what you specified when you created the SSH key. For OS Login accounts, the username is defined in your Google profile. For example, cloudysanfrancisco_example_com or cloudysanfrancisco.
    • NAME: the name of the VM.
  6. In the Category menu, navigate to Connection > SSH > Auth.
  7. In the Private key file for authentication field, select the private SSH key file that corresponds to the public key you added to the VM.
  8. In the Category menu, navigate to Connection > Proxy.
  9. In the Proxy type section, select Local.
  10. In the Telnet command, or local proxy command field, enter the following command:

    gcloud.cmd compute start-iap-tunnel VM_NAME PORT_NUMBER --listen-on-stdin --project=PROJECT_ID --zone=ZONE

    Replace the following:

    • VM_NAME: the name of the VM that you want to connect to.
    • PORT_NUMBER: the port that the sshd daemon runs on. The default PORT_NUMBER is 22.
    • PROJECT_ID: the project that hosts the VM that you want to connect to.
    • ZONE: the zone where the VM is located.
  11. Click Open to connect to the VM.

Troubleshooting

To find methods for diagnosing and resolving failed SSH connections, see Troubleshooting SSH.

What's next