DEV Community

Cover image for Different Methods to Connect to a GCP Linux Compute Engine Instance
Ahmad Majeed Zahoory
Ahmad Majeed Zahoory

Posted on

Different Methods to Connect to a GCP Linux Compute Engine Instance

Connect to a GCP Linux Compute Engine Instance

Google Cloud Platform (GCP) offers a robust and scalable infrastructure, and one of its core services is the Compute Engine, which provides virtual machines (VMs) running on Google’s infrastructure.

Connecting to a Linux-based Compute Engine instance is a common task for developers and system administrators.

This blog will guide you through the process, step by step.

Step 1: Connect via Google Cloud Console
Process:

  • Create Google Linux compute engine.

  • Navigate to the VM instances page: VM Instances.

  • Locate your Linux VM and click the SSH button in the corresponding row.

  • Click on Open in browser window.

Image-1

  • A browser-based terminal will open, connecting you directly to the instance.

Image-2

Step 2: Connect via gcloud CLI using CloudShell
Process:

  • Create Google Linux compute engine.
  • Open the Cloud shell terminal.
  • To connect to the Linux instance, use the command:
gcloud compute ssh [INSTANCE_NAME] --zone=[ZONE]
- Replace [INSTANCE_NAME] with the name of your instance and [ZONE] with its zone (e.g., us-central1-a)
Enter fullscreen mode Exit fullscreen mode
  • The Linux terminal will open, establishing a direct connection to the instance.

Image-3

Step 3: Connect via gcloud CLI using gcloud SDK
Process:

  • Create Google Linux compute engine.

  • Follow these steps to connect to a Linux instance:

  • Install the Google Cloud SDK on your workstation.

  • Open the command line interface in your workstation.

  • Authenticate to google using:

gcloud auth login
Enter fullscreen mode Exit fullscreen mode
  • Set the default Google Cloud project using:
gcloud config set project [PROJECT_ID]
- Replace [PROJECT_ID] with the ID of your Google Cloud project
Enter fullscreen mode Exit fullscreen mode
  • To connect to the Linux instance, use the command:
gcloud compute ssh [INSTANCE_NAME] --zone=[ZONE]
- Replace [INSTANCE_NAME] with the name of your instance and [ZONE] with its zone (e.g., us-central1-a)
Enter fullscreen mode Exit fullscreen mode
  • The Linux terminal will open, establishing a direct connection to the instance.

Step 4: Connect via ssh using own SSH Key defined in the Project metadata
Process:

  • On your local machine, generate an SSH key pair.
  • Use the key comment as the login name for Linux Compute Engine.
  • Save public key and private key, on your local machine.

Image-4

  • Navigate to Compute engine > Metadata.
  • Click the SSH Keys --> Click Add SSH Key --> Select Add Item.
  • Copy the Public Key.

Image-5

  • Copy the External/ Internal IP of the compute engine.

Image-6

  • Follow these steps to connect to a Linux instance:
  • Launch PuTTY or another compatible tool.
  • Expand Connection > SSH > Auth and select credentials.
  • Click Browse and locate your private key file.

Image-7

  • The Linux terminal will open and prompt you for a username. Enter the username to connect to the instance.

Image-8

Step 4: Connect via ssh using own SSH Key defined in the Compute Engine
Process:

  • On your local machine, generate an SSH key pair.
  • Use the key comment as the login name for Linux Compute Engine.
  • Save public key and private key.
  • Create Google Linux compute engine.
  • In the Security section:
  • Select Add Item, under Add manually generated SSH keys.
  • Copy the Public Key.

Image-9

  • Copy the External/ Internal IP of the compute engine.
  • Follow these steps to connect to a Linux instance:
  • Launch PuTTY or another compatible tool.
  • Expand Connection > SSH > Auth and select credentials.
  • Click Browse and locate your private key file.
  • The Linux terminal will open and prompt you for a username. Enter the username to connect to the instance.

Step 5: Connect via ssh using own username and password
Process:

  • Create Google Linux compute engine.
  • In the Advanced section:
  • Copy the script below (adjust it according to your Linux distribution) under Startup script.
$sec_pass = ConvertTo-SecureString -String "lab-password@123" -AsPlainText -Force
New-LocalUser -Name gcpadmin -PasswordNeverExpires -Password $sec_pass
Add-LocalGroupMember -Group Administrators -Member gcpadmin
Enter fullscreen mode Exit fullscreen mode

Image-10

  • Copy the External/ Internal IP of the compute engine.
  • Follow these steps to connect to a Linux instance:
  • Launch PuTTY or another compatible tool.
  • The Linux terminal will open and prompt you for a username and password. Enter the username and password to connect to the instance.

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay