Remote Development on a Raspberry Pi with SSH — How To Set Up

If you want to devel­op your soft­ware for rasp­ber­ry pi, you may need to access parts of the hard­ware. With Visu­al Stu­dio Code (VS Code) you can direct­ly deploy your soft­ware on the rasp­ber­ry pi with­out any per­for­mance loss­es by using your Win­dows machine and a remote SSH con­nec­tion to your rasp­ber­ry pi. I pre­pared a step-for-step instruc­tion to set up a remote devel­op­ment con­nec­tion in VS Code with a rasp­ber­ry pi:

1. Login to your rasp­ber­ry pi

2. Cre­ate a .ssh fold­er in /home/<username>:

 mkdir ~/.ssh

3. Open on your Win­dows machine a ter­mi­nal and run fol­low­ing command:

ssh-keygen

It cre­ates a pub­lic SSH key which is stored under c:\users\<username>\.ssh. This key is used to iden­ti­fy this machine as a trust­ed source.

4. Open an edi­tor on the rasp­ber­ry pi:

sudo nano

5. Copy & paste the gen­er­at­ed con­tent of the .pub file from your Win­dows com­put­er into the new opened edi­tor on your rasp­ber­ry pi

6. Save the file with the file­name “authorized_keys”

7. Move the gen­er­at­ed file into the ~/.ssh folder:

move authorized_keys ~/.ssh

Now we need to install OpenSSH for Win­dows. Fol­low the instruc­tions on https://docs.microsoft.com/de-de/windows-server/administration/openssh/openssh_install_firstuse

After instal­la­tion, make sure that you are able to con­nect to your rasp­ber­ry pi using the Win­dows pow­er shell:

ssh <username>@raspberrypi

It is impor­tant that you do not have to enter a pass­word. Oth­er­wise some­thing went wrong with the SSH configuration.

10. Open VS Code on your Win­dows machine and install the exten­sion “Remote Devel­op­ment

11. Restart VS Code

12. Run from the com­mand palette (CTRL-Shift+P):

Remote-SSH: Connect to Host…

13. Enter your SSH address <username>@raspberrypi (e. g. pi@raspberrypi)

14. VS Code should now con­nect to the rasp­ber­ry pi. You may need to select which host you want to con­nect to. Select “Lin­ux”.

After the auto­mat­ic instal­la­tion of some depen­den­cies, now VS Code should dis­play a green bar in the low­er left-hand corner:

Now you are ready to go!

Note:

  • If you use exten­sions, you have to install them again for this SSH user
  • You can find detailed infor­ma­tion about remote devel­op­ment using SSH on https://code.visualstudio.com/docs/remote/ssh#_getting-started
  • You can also use xDe­bug to debug PHP Code (just ask me for detailed information)
  • Test­ed with Rasp­bian Stretch 9.11 and VS Code 1.43.2

Leave a Reply

Your email address will not be published. Required fields are marked *