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!
Not only pretty much my entire house is running on Home Assistant now but also Pi-hole 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:
- Open the terminal
- Enter the ssh command (
ssh username@ip-address-of-rasberry-pi
) - 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.
-- 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 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 .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 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 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:
- 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?
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 contributions are very welcome š¤š¼.
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.