Connect Your Google Cloud VM on Your Terminal

  • Home
  • Connect Your Google Cloud VM on Your Terminal

Hello folks

I wanted to share a quick tip with you all. I’ve noticed many of you are connecting to Google Cloud VM SSH through the browser, which can be a real-time-suck.

Did you know you can connect directly from your computer’s terminal? It’s super easy and takes less than 2 minutes!

Here’s how:

Step 1: Create Key pair

First, create a private and public key pair on your computer. To do this, open your terminal and run:

Or, for more customization:

ssh-keygen -t rsa -b 2048 -f ./key-name -C "your_email@example.com"

it will ask you to enter a passcord. you can just enter if you don’t want to add it. replace key-name with any name you want.

it will create 2 files. for my example the files are as follows,

key-name -> Private key

key-name.pub -> Public Key

Works on Windows, Ubuntu, Linux, and Mac OS!

Step 2: Add Public key to Google Cloud VM meta

Now that we have our private and public key pair, let’s add the public key to your Google Cloud VM metadata.

In order to do that.

  1. Login to Google Cloud
  2. Navigate to Compute Engine
  3. Click on ‘Metadata’ under Settings
  4. Select the ‘SSH Keys’ tab

Then Click the Edit link from the top nav menu as see you In the following image

Then you will see an option to ADD ITEM, an input field will show up when you click the button. copy and paste the content of the key-name.pub file. and Press SAVE

Step 3: Connect to your VM using SSH

You are almost done now. just connect the VM to your terminal

Open Command prompt/PowerShell/Terminal as per your OS and run this command

ssh -i <path-to-your-private-key-dir>/key-name username@<server-public-ip>

it may ask you to enter the code which you are using on key creation.

Done ๐ŸŽ‰. you can connect to the VM successfully

If you are geting any error related to the file permission.

You can just run sudo chmode 600 <path-to-your-private-key-dir>/key-name

Conclusion

Now you can to connect Google Cloud VM using SSH on your machine. I hope this helps you.

author

By Habil M

Software Engineer

Date

2024-12-08T14:30:12