How to connect to a GCP instance via SSH

Introduction

Secure Shell (SSH) is a protocol used to securely connect to remote systems over a network. SSH keys are a more secure method for authenticating SSH connections compared to traditional username/password authentication. When working with cloud platforms like Google Cloud Platform (GCP), SSH keys play a critical role in accessing and managing virtual machines (VMs). This guide walks through the process of generating SSH keys, adding them to GCP, and connecting to VMs using different methods, as well as forwarding SSH keys and troubleshooting common issues.

Generating SSH Keys

SSH keys are pairs of cryptographic keys (private and public) used to authenticate a user. Follow these steps to generate SSH keys:

  1. On Linux/Mac OS:

    • Open a terminal.
    • Run the following command to generate a key pair:
      bash
      ssh-keygen -t rsa -b 2048 -C "[email protected]"
    • When prompted:
      • Enter a file name for the key pair or press Enter to use the default (~/.ssh/id_rsa).
      • Set a passphrase for added security (optional).
    • The private key will be saved as id_rsa and the public key as id_rsa.pub.
  2. On Windows (using PuTTYgen):

    • Download and install PuTTYgen.
    • Open PuTTYgen and select RSA with 2048 bits.
    • Click Generate, move your mouse randomly in the window to generate randomness.
    • Save the private key (.ppk) and public key (.pub) to a secure location.
    • Copy the public key text to use later.

Using ssh-keygen (Linux and Mac OS X)

Linux and Mac OS have built-in SSH clients.

  1. Open a terminal.
  2. Use the following command:
    bash
    ssh -i ~/.ssh/id_rsa USERNAME@VM_EXTERNAL_IP

    Replace USERNAME with your GCP username and VM_EXTERNAL_IP with the external IP of your instance.

  3. Accept the connection when prompted.

Using PuTTY

  1. PuTTY is a popular SSH client for Windows users.
    1. Convert the Private Key:
      • If you created your private key using OpenSSH, convert it to PuTTY’s .ppk format using PuTTYgen.
      • Open PuTTYgen, click Load, select your private key (id_rsa), and save it as .ppk.
    2. Configure PuTTY:
      • Open PuTTY.
      • In the Host Name field, enter:
        css
        USERNAME@VM_EXTERNAL_IP

        Replace USERNAME with your GCP username and VM_EXTERNAL_IP with the external IP of your instance.

      • Go to Connection > SSH > Auth and browse to your .ppk private key.
      • Click Open to connect.

Adding SSH Key to GCP

To add your SSH key to a GCP instance:

  1. Log in to the GCP Console and select your project.
  2. Navigate to Compute Engine > VM Instances.
  3. Click Edit for the desired instance.
  4. Paste your public SSH key (.pub file content) into the SSH Keys section.
  5. Update the comment at the end of the key to bitnami if required.
  6. Click Save.

How to connect to a GCP instance via SSH

GCP offers an in-browser SSH tool that doesn’t require additional software.

  1. Open the Google Cloud Console.
  2. Navigate to Compute Engine > VM Instances.
  3. Click the SSH button next to your VM.
  4. A new browser tab will open, connecting you to the instance securely.

Connecting with PuTTY

To connect using PuTTY on Windows:

  1. Launch PuTTY and enter your instance’s external IP in the Host Name field.
  2. Go to Connection > SSH > Auth and load your .ppk file.
  3. Click Open to start the session.

Connecting with SSH on Linux and Mac OS X

Linux and Mac OS have built-in SSH clients.

  1. Open a terminal.
  2. Use the following command:
    bash
    ssh -i ~/.ssh/id_rsa USERNAME@VM_EXTERNAL_IP

    Replace USERNAME with your GCP username and VM_EXTERNAL_IP with the external IP of your instance.

  3. Accept the connection when prompted.

Forwarding SSH Keys

SSH key forwarding allows you to access a remote server and use your local SSH keys to authenticate to another server from that remote session.

  1. Enable Forwarding Locally:

    • Start the SSH agent:
      bash
      eval $(ssh-agent -s)
    • Add your key:
      bash
      ssh-add ~/.ssh/id_rsa
    • Connect to the remote server with forwarding enabled:
      bash
      ssh -A USERNAME@VM_EXTERNAL_IP
  2. Forwarding Using PuTTY:

    • In PuTTY, go to Connection > SSH > Auth > Agent Forwarding and enable forwarding.

Linux and Mac OS X

    1. Add your private key to the SSH agent:
         ssh-add ~/.ssh/my_gcp_key
    1. Connect to the host and forward the key:
ssh -A bitnami@SERVER-IP

Troubleshooting Common Issues

If you encounter issues, consider the following tips:

  • Ensure the correct SSH key is added to your instance.
  • Verify the key file permissions are set to 600.
  • Check the firewall rules to allow SSH traffic (port 22).

Conclusion

SSH keys provide a secure and efficient way to access GCP instances. By following the steps in this guide, you can generate SSH keys, add them to GCP, and connect to your instances using various tools. Whether you prefer using a browser, PuTTY, or native SSH on Linux/Mac, the flexibility of SSH allows you to manage your infrastructure securely. Additionally, understanding SSH forwarding and troubleshooting common issues ensures seamless operations when working with cloud environments

For more details, visit the official GCP documentation.

 

 

Related articles

AWS EMR Interview Questions

25 AWS EMR Interview Questions and Answers Basic AWS EMR Questions What is Amazon EMR? Amazon Elastic MapReduce (EMR) is...

Architecting Multi-Region High Availability Applications on AWS

🌍 Architecting Multi-Region High Availability Applications on AWS: A Complete Guide In today’s always-on world, ensuring your applications are...

How to Secure AWS IAM Roles in Production 2026

How to Secure AWS IAM Roles in Production 2026 Securing AWS IAM roles in production is no longer optional...

Infrastructure as a Service (IaaS)

Infrastructure as a Service (IaaS) Infrastructure as a Service (IaaS) is one of the foundational layers of cloud computing,...