Connect to a Cloud VM host using VS Code with a Remote SSH extension

On your VS Code install an extension Remote – SSH from Microsoft

Set up your SSH Key:

using your terminal navigate to ~/.ssh

Place your public key on the vm in ~/.ssh/authorized_keys

Make sure permissions are correct:

chmod 700 ~/.ssh

chmod 600 ~/.ssh/authorized_keys

Add the VM to your SSH config
Edit ~/.ssh/config on your laptop:

Host myvm
HostName YOUR_VM_IP_OR_DNS
User YOUR_USERNAME
IdentityFile ~/.ssh/<your private key pair>

ie.

HostName <your public ip> 
Host gcp-vm
User thecloudadoption_blog
IdentityFile ~/.ssh/gcp_vscode
IdentitiesOnly yes

For GCP VMs, SSH Key format:

Generate a SSH key: -> with a comment at the end like below(VM  username)
ssh-keygen -t ed25519 -f ~/.ssh/gcp_vscode -C "thecloudadoption_blog"

Then display the public key, copy on the VM in GCP console

cat ~/.ssh/gcp_vscode.pub

It will look something like:

ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIB... thecloudadoption_blog

Google Cloud accepts that format directly.

Then you can add the private key on your local config file as explained above.

Connect from VS Code

Open VS Code

Press Ctrl+Shift+P

Run: Remote-SSH: Connect to Host…

Pick myvm or your VM name

VS Code opens a window connected to the VM, and you can edit files there directly

Run python Code within VS Code:

Install the python plugin for the VM-> Extensions-> python.

Select the Python interpreter

Press:

⌘⇧P

Type:

Python: Select Interpreter

Choose something like:

/usr/bin/python3

or your virtual environment:

/home/thecloudadoption_blog/venv/bin/python

If no interpreters appear, we’ll need to install Python or point VS Code at it.

Unknown's avatar

Author: Labanish

I am in the field of IT, currently working as a cloud architect. I have experience living and working abroad, spent some years in Romania, then relocated to the UK for work. In my spare time, you will find me biking or hiking, exploring new cities or watching my favorite sport, F1. The views expressed here are mine and do not associate to any organization. Laban.

One thought on “Connect to a Cloud VM host using VS Code with a Remote SSH extension”

Leave a Reply

Discover more from The Cloud Adoption Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading