Using VSCode over SSH
Intro
Visual Studio code has a great feature which allows you to use your Windows PC to remotely edit, compile and debug code on your embedded linux platform. Visual Studio Code runs on the PC with a remote-ssh extension installed, and communicates with the platform via SSH. The SSH server component of VSCode is automatically installed on the platform when you first connect.
Steps
- Ensure you have Visual Studo Code installed on the PC.
- Install the Remote-SSH extension.
- Set up credentials
- Run
ssh-keygen.exe
in the Windows PC terminal to generate a key. If you use the default options you should end up with two files under~\.ssh
.ia_rsa
is the identity file with the private key andis_rsa.pub
contains the public key to be installed on the remote platform. - On the remote platform, create file
~/.ssh/authorized_keys
. Paste the contents ofis_rsa.pub
that was generated in the previous step into theauthorized_keys
file and save. - On the Windows PC, open the SSH config file in
~\.ssh\config
. Add details of the platform. Here’s an example;Host rpi HostName 192.168.1.117 User pi IdentityFile ~/.ssh/id_rsa
The config file is also accessible through VSCode in the Remote Explorer panel by clicking the ‘configuration’ icon.
- To verify that the keys have been correctly installed, open an ssh session from the windows PC terminal:
ssh rpi
. You should be able to connect without entering a password. You can close the SSH session usinglogout
.
- Run
- Configure remote platform setting
- In VSCode press
ctrl+,
to open settings. - Navigate to the section
Remote.SSH: Remote Platform
. You can paste this into the search bar. - Click ‘Add item’ and add an entry for your platform. In my case the item is
rpi
and the value islinux
.
- In VSCode press
- Connect to the SSH target
- Opening the Remote Explorer panel on the left, and select the target that you just configured (
rpi
in this example). Click ‘Connect to host in new window’. - Wait while connection is established and the VSCode server is automatically installed on the platform.
- Opening the Remote Explorer panel on the left, and select the target that you just configured (
- You should now be able to open files & folders on the remote platform from your windows VS Code instance! Try
File > Open File
orFile > Open Folder
. - To disconnect, hit
F1
and typeRemote: Close Remote Connection
. You can also access this by clicking the green icon in the bottom left with the angle brackets.