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

Need help with your Raspberry Pi?
Contact Me!

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


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:

  1. Open the terminal on your Pi and type sudo raspi-config to open the configuration tool.
  2. Navigate to “Interfacing Options” and select “SSH”.
  3. Choose “Enable” and confirm your choice by pressing Enter.
  4. 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:

  1. 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.

  2. 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 the authorized_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.

  3. 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 to PasswordAuthentication no. Save and exit the file.

  4. Restart the SSH service for changes to take effect:

    sudo systemctl restart ssh
    
  5. 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.