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
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
- Go to the Google Cloud Console
- Navigate to “Compute Engine” > “Metadata”
- Select the “SSH Keys” tab
- Click “Add SSH Key”
- Copy the contents of
~/.ssh/gcp_key.pub
and paste it into the form - 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
3. Configure SSH
Create or edit your SSH config file:
nano ~/.ssh/config
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
4. Connect to Your Instance
Now you can connect to your instance using:
ssh my-gcp-instance
If you didn’t use the SSH config file, you can connect with:
ssh -i ~/.ssh/gcp_key your_username@YOUR_INSTANCE_EXTERNAL_IP
Troubleshooting
-
Permission Denied: Ensure your public key is correctly added to GCP and that you’re using the correct private key.
-
Invalid Format: If you see “Load key: invalid format”, check that your key file has the correct permissions:
chmod 600 ~/.ssh/gcp_key
-
Connection Timed Out: Check your instance’s firewall rules to ensure SSH (port 22) is allowed.
-
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
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!
Source link
lol