Sami Belhareth

Connecting to a GCP VM from your local VSCode

First, you'll want to create public key with

ssh-keygen -t rsa -b 4096

You will then be prompted to give the key a name (e.g. key)

In the directory where you created the key, you will now see two new files. One is called the private key (key) and the other is called the public key (key.pub).

We now need to copy the contents of key.pub to GCP.

First, we'll need to open the VM in GCP by navigating to "Compute Instances", then click on "Edit" in the top bar to open its settings.

Then, we'll scroll down to "SSH Keys", Click "+ Add item", paste in the contents of key.pub, and save.

In VSCode, click on the icon in the bottom left corner to use the "Remote-SSH" plugin.

Then click "Connect to Host" --> "Add New SSH Host" and type in ssh -i full/path/to/key(private not public) username@external.ip.address.

The username you need to use can be found at the end of the public key, and the external IP address can be obtained from the "Network interfaces" section of the VM details page.

That's it! You've successfully connected VSCode to your GCP VM 🎉

Back to Main Page