Access my Pi Remotely
At times, you may want to copy files between your Raspberry Pi and another computer. There are several ways you can do this. The simplest is to use a USB drive. You can plug the drive in and copy files onto it, then eject it and plug it into your computer. However, it can also be very convenient to connect to your Raspberry Pi over the network. You can then copy files and interact with your Pi from your computer.
All the details you need, and more, are in the Raspberry Pi documentation on Remote Access. However, we will go over the basics here.
Enable SSH
The best way to connect to your Raspberry Pi is via a secure shell (ssh). This is the same shell we introduced earlier, but accessed over the network.
The Raspberry Pi comes with an ssh server built it, but it is switched off by default. In the Remote Access documentation, the instruction are in the Enabling the Server section. Basically you need to:
- Open the Raspberry Pi Configuration tool (
sudo raspi-config
from the command line for the terminal version) - Make sure you set your hostname and have a reasonable password on your account.
- Go into the interfaces section, and turn ssh on.
- Accept the changes, and close the configuration tool.
Access your Pi via SSH
Make sure both your computer and your Pi are connected to the same network. Before you connect you will need the IP address of the Pi. You can find this by hovering over the network icon at the top right of your Pi’s desktop or by running hostname -I
at the Terminal. You are looking for an address like 192.168.68.131
.
Now, to access your Pi from your computer, open the Terminal and use the ssh
command to connect. This command needs you to pass it an argument that has the username and IP address of the Pi you are connecting to. For example, if your username is florence
and my pi is at 192.168.68.131
, then you connect with this:
You should also be able to use your Pi’s hostname with the .local
address. For example, if the hostname for the Pi is multitool
then you can also access it using:
Whichever way you access it, this will prompt you to accept the connection if your computer has not accessed the Pi before and then it will ask for the user’s password. Type the password and press enter.
If this succeeds, your Terminal will now have access to the shell on your Pi! You can use any of your Unix commands to access things on the Pi.
To close the terminal use the ctrl-d
signal, or type the exit
command. This will close the secure shell, and return you to your local shell.
Copying files with sftp
Once you have ssh working, you can sue the secure file transfer protocol (sftp) to easily transfer files between your computer and your Pi, and visa versa.
At the terminal, with a local shell, you can connect to your Pi using sftp much like you access it for ssh. For example:
When this connects, you can use the following commands to quickly and easily transfer files between your computer and the Pi:
ls
will give you a director listing from the remote machine (your Pi)cd
will change the directory on the remote machineget
will copy a file from the remote machine to your local machine (Pi to computer)lls
will give you a directory listing of your local machine (your computer)lcd
will change the current directory on your local machineput
will copy a file from the local machine to the remote machine (computer to Pi)
The above image shows connecting to a Pi using sftp
and getting all the files from the Pictures
folder on the remote machine.
Accessing your Pi from VS Code
VS Code includes the ability to do remote development using ssh. To do this you need to install the Remote - SSH extension. If you have ssh working, you can play around with editing your code from your computer, while the files and terminal commands are actually run on the Raspberry Pi!
To get this working, follow the steps in the remote development using ssh guide.