How to Control Your Raspberry Pi From Anywhere Using SSH (Secure Shell) Protocol

Learn how to access your Raspberry Pi remotely using the SSH protocol, allowing you to control your device from anywhere with an internet connection. …


Updated September 14, 2023

Need help with your Raspberry Pi?
Contact Me!

Do you love silly Raspberry Pi Projects?
Check out my this YouTube Channel!


Learn how to access your Raspberry Pi remotely using the SSH protocol, allowing you to control your device from anywhere with an internet connection.

SSH (Secure Shell) is a secure protocol used for remote login and execution of commands on another machine. It allows you to connect to your Raspberry Pi from any location with an internet connection and perform tasks as if you were physically in front of it. In this guide, we’ll walk through the steps required to set up SSH access to your Raspberry Pi.

Prerequisites

  • A Raspberry Pi with a running OS (e.g., Raspbian or Ubuntu)
  • An internet connection on both devices (Raspberry Pi and computer you’re connecting from)

Step 1: Enable SSH on the Raspberry Pi

First, we need to make sure that the SSH server is enabled on your Raspberry Pi. By default, it should be disabled for security reasons. To enable it, open the terminal on your Raspberry Pi and run:

sudo raspi-config

This will launch the configuration tool. Navigate to Interfacing Options and select SSH. Enable SSH by choosing Yes, then exit and save the settings.

Step 2: Find the IP Address of Your Raspberry Pi

You’ll need to know your Raspberry Pi’s local IP address so you can connect to it from your computer. To find this, run the following command on your Raspberry Pi:

hostname -I

This will display the IP addresses assigned to your device. Make a note of the one that starts with 192.168 (e.g., 192.168.x.x).

Step 3: Connecting from Your Computer

Open a terminal or command prompt on your computer and enter the following command to connect to your Raspberry Pi using SSH:

ssh pi@<IP_ADDRESS>

Replace <IP_ADDRESS> with the IP address you obtained in Step 2. When connecting for the first time, you’ll be prompted to confirm the authenticity of the host. Type yes and press Enter.

You’ll then be asked to enter your Raspberry Pi username and password (by default, these are both pi). Once connected, you can run commands on your Raspberry Pi as if you were sitting in front of it.

Optional: Generating an SSH Key Pair for Passwordless Authentication

For added security, you can set up a key pair to authenticate yourself without the need for a password. This is especially useful when connecting from a remote location or automated scripts. To generate an SSH key pair, follow these steps:

  1. On your computer, open a terminal and run:
ssh-keygen -t rsa -b 4096
  1. Press Enter to accept the default file location (~/.ssh/id_rsa) and add a passphrase if desired.
  2. Copy the public key to your Raspberry Pi by running:
ssh-copy-id pi@<IP_ADDRESS>
  1. Once connected, append the contents of ~/.ssh/id_rsa.pub to ~/.ssh/authorized_keys on your Raspberry Pi:
cat ~/.ssh/id_rsa.pub | ssh pi@<IP_ADDRESS> 'cat >> .ssh/authorized_keys'
  1. You can now connect without a password by running ssh pi@<IP_ADDRESS> from your computer. If you set up a passphrase for the key pair, you’ll be prompted to enter it once during the connection process.

Conclusion

Now you know how to remotely access and control your Raspberry Pi using SSH. This is an essential skill for anyone working with headless devices or running scripts on their Raspberry Pi. With this setup, you can manage your device from anywhere in the world as long as it has internet access.