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.
Method 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.
- A
browser-based terminal will open, connecting you directly to the instance.
Method 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).
- gcloud
compute ssh [INSTANCE_NAME] --zone=[ZONE]
- To connect to the Linux instance,
use the command:
Step 3: Connect via gcloud CLI using gcloud SDK
·
Process:
o
Create Google Linux compute engine.
o
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
using gcloud auth login.
ü Set
the default Google Cloud project for your gcloud commands using gcloud config set project [PROJECT_ID].
Ø Replace
[PROJECT_ID] with the ID of your Google Cloud project.
ü 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).
§ 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:
o
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.
o
Navigate
to Compute engine > Metadata.
§
Click
the SSH Keys.
§ Click Add SSH Key.
ü Select
Add Item.
Ø Copy the Public Key.
o Copy the External/ Internal IP of the compute engine.
o
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 4: Connect via ssh using own SSH Key
defined in the Compute Engine
·
Process:
o
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.
o
Create Google Linux compute engine.
§ In
the Security section:
ü Select
Add Item, under Add manually generated SSH keys.
ü Copy the Public Key.
o
Copy the External/ Internal IP of the
compute engine.
o
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:
o
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
o
Copy the External/ Internal IP of the
compute engine.
o
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. Enter the username to
connect to the instance.
§ The
Linux terminal will open and prompt you for a password. Enter the password to
connect to the instance.