SSH: Remote control your Raspberry Pi

By Russell Barnes. Posted

Remotely control your Raspberry Pi from a PC, Linux, or Mac computer and transfer files using SSH.

SSH (also known as ‘Secure Shell’) is an encrypted networking technology that enables you to manage computers from the command line over a network.

Get a free Pi Zero W, Official Case, and Accessories with your 12-month subscription to The MagPi. Click here for more info.

SSH is handy if you want to quickly connect to a Raspberry Pi from a terminal window on another computer. It’s also ideal for lightweight distro installations that don’t have graphical interfaces, and projects that don’t have a screen (such as robots). It’s especially useful when creating Internet of Things (IoT) projects, as these may be embedded inside other hardware.

We’ve already looked at VNC (Virtual Network Computing) – back in issue 71 – and Secure Shell offers a similar service. But while VNC shares the entire desktop, SSH works from the command line.

See also: VNC: Remote Control a Raspberry Pi desktop

SSH also enables you to share files to, and from, a Raspberry Pi using SCP (secure copy) on the command line, or SFTP (SSH File Transfer Protocol).

Use SSH on a Raspberry Pi with PC, Windows and Linux

You don’t need to install any software to start using Secure Shell. Linux, macOS, and Windows 10 have the SSH command-line application installed by default.

On older Windows you will need download an SSH client; the most commonly used one is called PuTTY. Download the PuTTY software from Simon Tatham’s website.

You’ll need to use the password for your Raspberry Pi to log in. For security reasons, we strongly recommend changing the default password.

SSH uses an encrypted network, so it doesn’t send your password as plain text. More advanced users can control the encryption keys, using ssh-keygen. For now, we’ll look at setting up and using secure shell.

Step 1 Activate SSH in Raspbian

For security reasons, Secure Shell is not turned on by default in Raspbian. On your Raspberry Pi, choose Menu > Preferences > Raspberry Pi Configuration. Click on Interfaces and set SSH to Enabled. Click OK. You don’t need to restart your Raspberry Pi, and SSH will be enabled whenever you use that installation of Raspbian from that point on (be sure to have updated your password from the default, which is ‘raspberry’).

Need to set up SSH on a headless Raspberry Pi? Check the official docs.

Step 2: Activate SSH Client in Windows

Linux and macOS both support SSH out-of-the-box; skip ahead to Step 3 if you are using one of those operating systems.

Windows 10 supports SSH, but you need to activate it. Click on Search and look for ‘Manage Optional Features’. Click it in Search to open the Settings window.

Click ‘Add a feature’ and wait for the list of Optional Features to load. Scroll down the list to Open SSH Client (Beta). Click Install.

Step 3: Get your IP address

Connect your Raspberry Pi to a local network. Use wireless LAN, or connect Raspberry Pi directly to a router with an Ethernet cable. Open a Terminal window and enter the following command:

hostname

You should see:

raspberrypi

In some circumstances you can use this host name, which is handy if you don’t have the IP address, but it’s more reliable to use the IP (internet protocol) address. Enter this to get your IP address:

hostname -I

This will return four numbers separated by dots. For instance, ours is:

192.168.0.41

Write this number down. You’ll need it shortly.

Step 4: Connect via SSH

Open Command Prompt on a Windows PC, or a Terminal window in Linux or macOS.

Enter this command:

ssh pi@[IP]

Replace [IP] with the IP address of the Raspberry Pi. In our case, we enter

ssh pi@192.168.0.41

The first time you do this, you’ll get a message saying that the authenticity can’t be established, followed by a long cryptographic hash of letters and numbers. It will say, ‘Are you sure you want to continue connecting?’

Enter yes and press RETURN. You’ll be asked to enter the password for your Raspberry Pi.

Step 5: Use PuTTY on a Windows PC

SSH with PuTTY on a PC On older PCs you’ll need to install PuTTY. Download the putty.exe file and click Run. The PuTTY Configuration window appears with basic options. Enter the IP address of your Raspberry Pi in the ‘Host Name (Or IP Address)’ field. Don’t change the ‘Port’ field. Click Open. You will get a PuTTY ‘Security Alert’ field. Click Yes. The terminal window displays ‘login as:’ Enter pi and press RETURN. Now enter the password for your Raspberry Pi.

Step 6: On Raspberry Pi

You will now see your usual command line replaced with pi@raspberrypi: ~$. You are now logged in and working on the command line from your Raspberry Pi. Enter ls and you’ll see Desktop, Downloads, Documents, and the other unique Raspberry Pi folders and files. You can create, edit, move, and work with files as if you were using a terminal on your Raspberry Pi.

Step 7: Visual interfaces

There are limitations to SSH. A plain SSH connection can’t open programs that use a graphical interface, so you should learn to use command-line alternatives (such as nano or vim instead of Leafpad for text editing). See How to Edit Text files on Raspberry Pi.

On Linux computers, you can forward your X session over SSH, to allow the use of graphical applications, by using the -Y flag:

ssh -Y pi@[IP]

Now you are on the command line as before, but you have the ability to open up graphical windows. For example, typing:

scratch &

…will open up Scratch in a graphical window.

Note that X11 is no longer present on Macs with macOS, so you have to download and install it separately to access graphical windows over SSH.

Step 8: Copy files

It’s not easy to share files using Secure Shell from the command line, but it’s possible and a handy skill to learn. If you’re still accessing Raspberry Pi, enter exit at the command line to return to your local shell:

exit

With SSH turned on, you can copy files from your source computer to the Raspberry Pi using SCP. We’ve downloaded The MagPi issue #83 PDF file to our Downloads folder. We’re going to copy it to the MagPi directory on our Raspberry Pi.

Use the scp command followed by the path to the source file, and then the destination location. The destination is pi@[IP] followed by a colon ‘:’ and the path to the destination location.

scp [source] pi@[IP]:[destination]

Replace the [source] and [destination] parts with the paths to the file and desired destination folder, and add your Raspberry Pi’s IP address.

For instance, copying the PDF file on our machine looks like this:

scp Downloads\MagPi83.pdf pi@192.168.0.41:/home/pi/MagPi

This copies The MagPi issue #83 PDF from the Downloads folder on our Windows PC to the MagPi directory on Raspberry Pi.

Notice that the directory path uses backslash (‘\’) for Windows source and slash (‘/’) for the Linux destination (if you’re using a Mac or Linux machine, it’ll be a slash in both source and destination.)

Step 9: Copying files

 Use SFTP (Secure File Transfer Protocol) in a program such as FileZilla to transfer files to and from your Raspberry Pi via a graphical interface

If you copy a lot of files back and forth from a computer to Raspberry Pi, then it’s a good idea to set up SFTP using a dedicated FTP program, such as FileZilla.

Install FileZilla on your computer (it is available for Linux, Windows, and macOS).

Open FileZilla and choose File > Site Manager.

Now click New Site and you can give the site a custom name, such as ‘Raspberry Pi’.

Change the Protocol to ‘SFTP – SSH File Transfer Protocol’.

Enter ‘raspberrypi’ in the Host field, ‘pi’ in the User field, and your Raspberry Pi password in the Password field.

Click Connect and you will connect remotely to the Raspberry Pi. You will see all your Raspberry Pi files in the right-hand ‘Remote’ side of the display. On the left, in the ‘Local Site’ section will be all the files on your computer. Double-click directory folders in either side to navigate into them, and use the ‘..’ folders at the top of each list to navigate back up to the parent folder.

Double-click a file in Local Site to send it to the Remote section, and vice versa. Follow the FileZilla tutorial if you need more guidance.

Subscribe and never miss an issue

 Get a free Pi Zero W with every twelve-month print subscription

Get a a brand new Raspberry Pi Zero W, a case for it, and a selection of adapter cables with a twelve-month print subscription to The MagPi!

More articles from The MagPi magazine

Subscribe