Remote development on a raspberry pi with ssh and VSCode
Remote development is a really cool and useful feature of VSCode. It makes developing on a remote raspi feeling like you were developing locally.
You can develop on your mac, linux or windows system really directly on your raspberry pi.
You can see the principle of the communication here:
This does not only feel better since you can use VSCode but you can also save ressources since
you don’t need to install a desktop on your raspi
Although there are many ways to share code between two hosts, if you want a really direct way to interact this method is great. And VScode has a great remote development extension for this.
The extension connects with ssh to the remote host and establishes a tunneled connection. Since it uses ssh-keys to authenticate you don’t need to enter passwords at all while you’re still connected in a secure way.
The look and feel is the same as if you would develop locally.
You can see in the image below that VScode is showing a remote folder in the titlebar of the window (saying [SSH: raspi4]) and in the bottom left of the statusbar stating: SSH: raspi4.
What you need to do:
On the raspberry
1) create a new development user on the remote raspi
Login as pi (or your admin user on the raspi) and choose some <username>
sudo useradd <username>sudo usermod -aG sudo <username>
Create a new password:
sudo passwd <username>
Switch to the new user
su -<username>
create a .ssh folder in /home/<username>
mkdir ~/.ssh
On your development system
2) Create a ssh key
ssh-keygen
(just press <Enter> for everything. for local development don’t use a passphrase otherwise you will have to enter the passphrase everytime. .. probably security overkill on your LAN)
- You can find the new keypair (pub and private in /home/<usernam>/.ssh
- Show the id_rsa.pub with less
less /home/<username>/.ssh/id_rsa.pub
4) On the raspi open an editor (e.g. nano or vim)
5) Copy & paste the .pub file content (you see with less) to the raspi authorized_keys
6) save the file as: /home/username/.ssh/authorized_keys
Install the extension:
7) Install the remote development extension pack in VSCode on your development system.
8) Run Remote-SSH: Connect to Host… from the Command Palette (CTRL+SHift+P)
- Connect as username@host
9) After you see that the connection is established just choose open file or open folder to open your development directory on the raspberry.
And devdelop directly on the raspberry pi
That’s it.
And of course you can use method to develop remotely on any linux system.
MacOS should also work. I am a bit unsure about windows but see hope since windows 10 ;)