Access your Raspberry Pi from Anywhere with SSH
A step-by-step guide on connecting to your Raspberry Pi remotely using SSH and managing it securely. …
Updated October 30, 2023
A step-by-step guide on connecting to your Raspberry Pi remotely using SSH and managing it securely.
Step 1: Enable SSH on Your Raspberry Pi
By default, the Raspberry Pi OS does not allow remote connections via SSH. To enable it, follow these steps:
- Open the terminal on your Pi and type
sudo raspi-config
to open the configuration tool. - Navigate to “Interfacing Options” and select “SSH”.
- Choose “Enable” and confirm your choice by pressing Enter.
- Exit the config tool by selecting “Finish” and then “Yes” when prompted to reboot.
Step 2: Connect to Your Raspberry Pi Remotely Using SSH
To connect remotely, you need to know your Raspberry Pi’s IP address or hostname (if it has one). You can find the IP address by running hostname -I
on the terminal. Once you have the IP, open a terminal on your local machine and type:
ssh pi@<IP_address>
For example:
ssh pi@192.168.1.100
When prompted, enter your Raspberry Pi’s password (default is “raspberry”). This will connect you to the device via SSH and allow you to execute commands remotely.
Step 3: Configure Your Firewall and Router Settings
To ensure your remote connections are secure, update your router settings to forward port 22 (SSH’s default port) to your Pi’s IP address. This will allow incoming SSH traffic to reach your device. Consult your router’s documentation for instructions on how to do this.
Next, make sure you have a firewall enabled on your Raspberry Pi. The default firewall software is called “ufw” (Uncomplicated Firewall). To enable it and allow SSH connections, run the following commands:
sudo apt update
sudo apt install ufw
sudo ufw enable
sudo ufw allow 22/tcp
Step 4: Secure Your Connection with Public Key Authentication
Using a password to authenticate each SSH connection is insecure. Instead, use public key authentication to create a secure and convenient method of logging in remotely. To set it up, follow these steps:
Generate an SSH key pair on your local machine by running
ssh-keygen
. Press Enter three times to accept the default file location and settings.Copy the public key to your Raspberry Pi by running
cat ~/.ssh/id_rsa.pub
on your local machine, then copying the output and pasting it into a new line at the end of theauthorized_keys
file on the Pi:ssh pi@<IP_address> 'echo "<public_key>" >> ~/.ssh/authorized_keys'
Replace
<public_key>
with the output from your local machine.Edit the
sshd_config
file on the Pi to enforce public key authentication:sudo nano /etc/ssh/sshd_config
Find the line that says
#PasswordAuthentication yes
and change it toPasswordAuthentication no
. Save and exit the file.Restart the SSH service for changes to take effect:
sudo systemctl restart ssh
Now, you can connect remotely without entering a password by running
ssh pi@<IP_address>
from your local machine.
Conclusion
Now you know how to connect to your Raspberry Pi remotely using SSH and securely manage it from anywhere. With this setup, you can access your device from work or while on the go without needing a monitor or keyboard. Remember to regularly update your software and keep your firewall settings up-to-date for optimal security.