Remote SSH with Visual Studio Code

Remote SSH with Visual Studio CodePhoto by
By Hemerson Carlin惻
3 min read惻

Itā€™s been exactly one year since and I couldnā€™t be more happy with that!

Not only pretty much my entire house is running on now but also plays an important part on my network infrastructure.

But over the last year there's one task I've been doing repetitive times over and over: accessing the Raspberry Pi remotely with SSH.

It becomes super boring having to run ssh username@ip-address-of-rasberry-pi all the time to access the Pi, not to mention I need to enter my password too. And so a simple ā€œone linerā€ command is actually translated into 8 steps:

  1. Open the terminal
  2. Enter the ssh command (ssh username@ip-address-of-rasberry-pi)
  3. Iā€™m prompt to enter the password
  4. I donā€™t know any of my passwords and so I need to open the password manager tool
  5. Find the password for the Pi
  6. Copy the password
  7. Go back to the terminal and paste the password
  8. Iā€™m in!

Donā€™t get me wrong, the terminal is great but I also like some ā€œeasy to useā€ environments too.

Enter SSH key + Visual Studio code

An SSH key is an access credential in the . Its function is similar to that of user names and passwords, but the keys are primarily used for automated processes and for implementing single sign-on by system administrators and power users.

-- https://www.ssh.com/academy/ssh/key

Combined with Visual Studio code, I get the best of both worlds: remote access made easy + the visual environment I was looking for.

There are a few things that need to be set up and thatā€™s what this blog post is about.

Firstly, make sure you have extension installed in your Visual Studio code. This is what makes everything possible once SSH is set up.

Secondly, we need an SSH key. I usually follow GitHubā€™s documentation for these, so make sure to follow before proceeding.

Third (and optional), you can save the SSH config in a custom config file inside the .ssh folder. This is useful if you work with multiple SSH logins such as GitHub, GitLab, enterprise git (like the one from your employer), Raspberry Pi (such as the one described in this blog post) and more.

My config file looks something like this:

# GitHub
Host github.com
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/<name_of_ssh_file_for_github>

# GitLab
Host gitlab.com
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/<name_of_ssh_file_for_gitlab>

# Raspberry Pi
Host <ip-address-of-raspberry-pi>
  AddKeysToAgent yes
  UseKeychain yes
  User <username>
  IdentityFile ~/.ssh/<name_of_ssh_file_for_accesing_raspberry_pi>

# Enterprise git
Host <host_of_enterprise_git>
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/<name_of_ssh_file_for_enterprise_git>

Lastly, itā€™s time to install the SSH key on Raspberry Pi so the key becomes an authorized key. For that, weā€™re going to be using the command like so:

ssh-copy-id -i ~/.ssh/<name_of_ssh_file_for_accesing_raspberry_pi> <username>@<ip-address-of-raspberry-pi>

Assuming the ssh key name is id_raspberry_pi, the username is pi and the ip address of the Raspberry Pi is 192.168.30.9, the ssh-copy-id command would be:

ssh-copy-id -i ~/.ssh/id_raspberry_pi [email protected]

Thatā€™s it! You should be good to access the Pi or any other remote machine. And the 8 and yet painful steps have been narrowed down by only 3:

  1. Open Visual Studio Code
  2. Click to access the Pi with SSH
  3. Iā€™m in!

I hope that's helpful and interesting to you. šŸ‘‹šŸ¼

Did you know you can help me with this page?

If you see something wrong, think this page needs clarification, you found a typo or any other suggestion you might have feel free to open a PR and I will take care of the rest.

My entire site is available to edit on GitHub and all are very welcome šŸ¤™šŸ¼.

mersocarlin

Hemerson Carlin, also known as mersocarlin, is passionate and resourceful full-stack Software Engineer with 10+ years of experience focused on agile development, architecture and team building.

This is the space to share the things he likes, a couple of ideas and some of his work.

Previous Blog Posts