DEV Community

Manoj Swami
Manoj Swami

Posted on

How to Connect to Google Cloud VM Instances Using Your Terminal

While Google Cloud Platform (GCP) offers a web-based SSH option, many developers prefer using their local terminal for a more familiar and flexible experience. This guide will walk you through the process of setting up and using SSH to connect to your GCP Compute Engine instances directly from your terminal.

Prerequisites

  • A Google Cloud Platform account with an active project
  • A running Compute Engine instance
  • gcloud CLI installed on your local machine (optional but recommended)

Step-by-Step Guide

1. Generate an SSH Key Pair

If you don't already have an SSH key pair, you'll need to generate one:

ssh-keygen -t rsa -f ~/.ssh/gcp_key -C your_username
Enter fullscreen mode Exit fullscreen mode

Replace your_username with your desired username (often your email address associated with GCP).

This command creates two files:

  • ~/.ssh/gcp_key (private key)
  • ~/.ssh/gcp_key.pub (public key)

2. Add Your Public Key to GCP

Option A: Using the Google Cloud Console

  1. Go to the Google Cloud Console
  2. Navigate to "Compute Engine" > "Metadata"
  3. Select the "SSH Keys" tab
  4. Click "Add SSH Key"
  5. Copy the contents of ~/.ssh/gcp_key.pub and paste it into the form
  6. Click "Save"

Option B: Using gcloud CLI

If you have gcloud CLI installed, you can add your key with this command:

gcloud compute project-info add-metadata --metadata-from-file ssh-keys=~/.ssh/gcp_key.pub
Enter fullscreen mode Exit fullscreen mode

3. Configure SSH

Create or edit your SSH config file:

nano ~/.ssh/config
Enter fullscreen mode Exit fullscreen mode

Add the following, replacing YOUR_INSTANCE_EXTERNAL_IP with your VM's external IP:

Host my-gcp-instance
    HostName YOUR_INSTANCE_EXTERNAL_IP
    User your_username
    IdentityFile ~/.ssh/gcp_key
Enter fullscreen mode Exit fullscreen mode

4. Connect to Your Instance

Now you can connect to your instance using:

ssh my-gcp-instance
Enter fullscreen mode Exit fullscreen mode

If you didn't use the SSH config file, you can connect with:

ssh -i ~/.ssh/gcp_key your_username@YOUR_INSTANCE_EXTERNAL_IP
Enter fullscreen mode Exit fullscreen mode

Troubleshooting

  1. Permission Denied: Ensure your public key is correctly added to GCP and that you're using the correct private key.

  2. Invalid Format: If you see "Load key: invalid format", check that your key file has the correct permissions:

   chmod 600 ~/.ssh/gcp_key
Enter fullscreen mode Exit fullscreen mode
  1. Connection Timed Out: Check your instance's firewall rules to ensure SSH (port 22) is allowed.

  2. Host Key Verification Failed: If you've recreated an instance with the same IP, you may need to remove the old host key:

   ssh-keygen -R YOUR_INSTANCE_EXTERNAL_IP
Enter fullscreen mode Exit fullscreen mode

Conclusion

Connecting to your GCP instances via terminal gives you more flexibility and integrates better with local development workflows. By following this guide, you should now be able to seamlessly SSH into your Google Cloud VM instances directly from your local terminal.

Remember to keep your private key secure and never share it. If you suspect your key has been compromised, generate a new pair immediately and update your GCP metadata.

Happy cloud computing!

Top comments (0)