Remote SSH with Visual Studio Code
It’s been exactly one year since I set up my Raspberry Pi and I couldn’t be more happy with that!
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:
- Open the terminal
- Enter the ssh command (
- I’m prompt to enter the password
- I don’t know any of my passwords and so I need to open the password manager tool
- Find the password for the Pi
- Copy the password
- Go back to the terminal and paste the password
- 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 SSH protocol. 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.
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 Remote - SSH 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 this guide before proceeding.
Third (and optional), you can save the SSH config in a custom
config file inside the
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 ssh-copy-id 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@example.com
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:
- Open Visual Studio Code
- Click to access the Pi with SSH
- I’m in!
I hope that's helpful and interesting to you. 👋🏼
Did you know you can help me with this page?
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.