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 lets you 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 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, set up authentication.
Authentication verifies your identity 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
-
Install the Google Cloud CLI. After installation, initialize the Google Cloud CLI by running the following command:
gcloud initIf you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
- 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:
- In the Google Cloud console, go to the VM instances page.
- 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 by using the
gcloud compute ssh
command with the
--tunnel-through-iap flag:
Connect to the VM by running the following command:
gcloud compute ssh INSTANCE_NAME \ --project=PROJECT_ID \ --zone=ZONE \ --tunnel-through-iapReplace the following:
INSTANCE_NAME: the name of the VM that you want to connect to.PROJECT_ID: the ID of the project that contains the VM.ZONE: the name of the zone that the VM is located in.
In the Google Cloud console, 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.
IAP Desktop
To connect to a VM using IAP Desktop, do the following:
-
Install IAP Desktop on your workstation if you haven't already.
-
Open IAP Desktop. The Add projects window opens.
-
When prompted, sign in using the Google account that has access to the project with the VMs you want to connect to.
-
In the Add projects window, enter the project ID or name of the project that contains the VMs you want to connect to.
-
In the Project Explorer window, right-click the name of the VM again and select Connect to connect to the VM.
OpenSSH clients
Connect to a VM that doesn't have an external IP address by using SSH
through an IAP tunnel. You can write your own SSH
configuration file (~/.ssh/config) so that tools such as VS Code or
an OpenSSH client can connect directly.
Generate and propagate your SSH keys by connecting to your VM at least once by running the
gcloud compute sshcommand:gcloud compute ssh INSTANCE_NAME \ --project=PROJECT_ID \ --zone=ZONE \ --tunnel-through-iapReplace the following:
INSTANCE_NAME: the name of the VM that you want to connect to.PROJECT_ID: the ID of the project that contains the VM.ZONE: the zone where your VM is located.
After the connection succeeds, exit the SSH session.
Open or create the SSH configuration file on your local workstation. This file is typically located at
~/.ssh/configon Linux and macOS, or%USERPROFILE%\.ssh\configon Windows.Append a configuration entry for your VM by adding the following block to your SSH configuration file:
Host INSTANCE_NAME HostName INSTANCE_NAME IdentityFile PATH_TO_PRIVATE_KEY ProxyCommand gcloud compute start-iap-tunnel %h %p --listen-on-stdin --project=PROJECT_ID --zone=ZONE --verbosity=warning User USERNAMEReplace the following:
INSTANCE_NAME: the name of the VM that you want to connect to.PATH_TO_PRIVATE_KEY: the path to your private key file, typically~/.ssh/google_compute_engine.PROJECT_ID: the ID of the project that contains the VM.ZONE: the zone where your VM is located.USERNAME: the username on the VM, such as your Google Cloud OS Login username or the metadata username.
Connect to the VM from your local terminal or SSH client by running the following command:
ssh INSTANCE_NAME
PuTTY app
To tunnel SSH connections through a VM's internal IP address by using PuTTY, do the following:
- If you haven't already added an SSH key, add an SSH key to the VM.
- If your workstation doesn't already have the PuTTY app installed, download the PuTTY package files.
In the Google Cloud console, go to the VM instances page and find the name of the VM that you want to connect to.
- Open the PuTTY app. A connection configuration window opens.
In the Host Name (or IP address) 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@INSTANCE_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, such ascloudysanfrancisco_example_comorcloudysanfrancisco.-
INSTANCE_NAME: the name of the VM.
- In the Category menu, go to Connection > SSH > Auth > Credentials (or Connection > SSH > Auth in PuTTY versions earlier than 0.78).
- In the Private key file for authentication field, click
Browse and select the private SSH key file
(
.ppk) that corresponds to the public key that you added to the VM. - In the Category menu, go to Connection > Proxy.
- In the Proxy type section, select Local.
In the Telnet command, or local proxy command field, enter the following command:
gcloud.cmd compute start-iap-tunnel INSTANCE_NAME PORT_NUMBER --listen-on-stdin --project=PROJECT_ID --zone=ZONE
Replace the following:
INSTANCE_NAME: the name of the VM that you want to connect to.PORT_NUMBER: the port that thesshddaemon runs on. The defaultPORT_NUMBERis22.PROJECT_ID: the project that hosts the VM that you want to connect to.ZONE: the zone where the VM is located.
- Click Open to connect to the VM.
Troubleshooting
To find methods for diagnosing and resolving failed SSH connections, see Troubleshooting SSH.
What's next
- Learn how to manage access to VMs.
- Learn how to transfer files to VMs.
- Learn how SSH connections to Linux VMs work on Compute Engine.