There have been a lot of posts written about how to connect from Visual Studio Code (VS Code) on a Windows PC to a Raspberry PI over SSH using public/private keys avoiding the need to use a password. This is another one, and I aim to be fairly terse.
The PC is running Windows 11, the Raspberry Pi is running the latest version of Raspberry Pi OS – Debian Bullseye. Note that <username> is a placeholder for your own username, the greater than symbol: > represents a Windows PowerShell prompt and the dollar sign: $ is a Linux Bash prompt.
Setup The PI
SSH needs enabling on the PI and an ~/.ssh directory needs creating in your users home directory.
On the Pi, in the terminal enable SSH with: sudo raspi-config then select 3 Interface Options > I2 SSH and choose Yes – Enable. Now create the directory where your keys will be kept: mkdir ~/.ssh
It is also useful to find the IP address of your PI with: hostname -I Mine is 192.168.2.45 and I shall being using this in the examples.
You should now be able to login to the Pi from the Windows PC, open Windows Terminal or PowerShell and use ssh <username>@192.168.2.45 answer yes to add it to the known hosts file and it will ask for your PI’s password. If your Windows username is the same as that on the PI you can login with ssh 192.168.2.45.
Setup Windows for SSH
This stage installs the SSH client on the PC, creates a keypair and copied the public key to the PI.
The OpenSSH client can be installed using Windows Settings on Windows Server 2019 and Windows 10 and above devices.
- Open Settings, select Apps > Apps & Features, then select Optional Features.
- Scan the list to see if the OpenSSH is already installed. If not, at the top of the page, select Add a feature, then:
- Find OpenSSH Client, then click Install
A full description is given on Microsofts Get started with OpenSSH page.
On the Windows PC, in your PowerShell create the directory for your keys if it doesn’t exist already mkdir ~/.ssh and then change into it cd ~/.ssh generate a keypair and add it to the ssh-agent:
> ssh-keygen -t ed25519
Generating public/private ed25519 key pair.
Enter file in which to save the key (C:\Users\<username>/.ssh/id_ed25519):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in C:\Users\<username>/.ssh/id_ed25519.
Your public key has been saved in C:\Users\<username>/.ssh/id_ed25519.pub.
The key fingerprint is:
The key's randomart image is:
|=o =.. |
|=o +o=o. . |
|= =oo== . . |
|.+o..+.. o o =|
| . o.o oS o o %o|
| . + o . o =.*|
| . . . . + |
| E |
| . |
PS C:\Users\<username>\.ssh> ssh-add ~\.ssh\id_ed25519
We now need to copy the public key to the PI:
> scp ~\.ssh\id_ed25519.pub <username>@192.168.2.45:~/.ssh/
And on the PI we need to add that public key to the authorized keys, connect into the PI ssh <username>@192.168.2.45 and:
$ cd ~/.ssh
$ touch authorized_keys
$ cat id_ed25519.pub >> authorized_keys
$ chmod 600 authorized_keys
$ rm id_ed25519.pub
the commands, touch creates a blank file if it doesn’t exist, cat appends the pub file to authorized_keys, making sure that any existing keys are preserved permissions are set with chmod and finally the pub file is deleted.
Setup VS Code
Load up VS Code and add the Remote – SSH and Remote SSH: Editing Configuration Files extensions, once restarted you will see a >< icon in the bottom left-hand corner with a green background, click that and select Edit SSH Configuration file…, choose the one in your home directory C:\Users\<username>\.ssh\config and add the following:
The Host “piComputer_192.168.2.45” is the friendly name displayed within VS Code for when you are choosing it from lists, the User id your user on the PI. Save the config file when complete.
You should now be able to connect to the PI. Click the >< icon in the bottom left-hand corner, and choose Connect To Host… A popup saying Setting Up… should appear, it may ask if the host is Linux or Windows, choose Linux, and within a few seconds it’ll be connected and ready. From within VS Code you can open a Bash Terminal – Ctrl+Shift+’
If it asks for a password, you may have missed a stage, or copied the private key to the PI instead of the public one.