Remote Access Your Raspberry Pi From Anywhere With SSH

Learn how to securely connect your Raspberry Pi to the internet and access it remotely using SSH. This tutorial will show you step-by-step instructions for setting up remote access, connecting from an …


Updated August 29, 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 securely connect your Raspberry Pi to the internet and access it remotely using SSH. This tutorial will show you step-by-step instructions for setting up remote access, connecting from another device, and securing your connection with an SSH key pair.

Setting Up Remote Access on Your Raspberry Pi

  1. Enable SSH by default on your Raspberry Pi:
    • Start by updating the package index and installing the openssh-server package:
      sudo apt update && sudo apt install openssh-server
      
    • After installation, enable the ssh service to start automatically on boot:
      sudo systemctl enable ssh
      
    • Start the ssh service:
      sudo systemctl start ssh
      
    • Check if the ssh service is running:
      sudo systemctl status ssh
      
  2. Configure your router to forward incoming SSH traffic to your Raspberry Pi:
    • Log in to your router’s admin interface and navigate to its port forwarding settings.
    • Add a new rule to forward TCP port 22 (the default SSH port) to the internal IP address of your Raspberry Pi.
  3. Get your Raspberry Pi’s public IP address:
    • You can use a website like icanhazip or run curl ifconfig.me in the terminal to find your public IP address.
  4. Set up an SSH key pair for secure authentication:
    • Generate a new SSH key pair on your local machine (if you don’t have one already):
      ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
      
    • Copy the public key to your Raspberry Pi:
      ssh-copy-id pi@raspberrypi.local
      

      Replace pi with your username and raspberrypi.local with the hostname or IP address of your Raspberry Pi.

Connecting to Your Raspberry Pi From Another Device

  1. Open a terminal on your local machine (Mac, Linux, or Windows Subsystem for Linux).
  2. SSH into your Raspberry Pi using its public IP address and username:
    ssh pi@your_raspberry_pi_ip_address
    

    Replace pi with your username and your_raspberry_pi_ip_address with the IP address you obtained earlier.

  3. You will be prompted to confirm the fingerprint of the Raspberry Pi’s SSH key. Type “yes” and press Enter.
  4. Enter your password when prompted, and you should now have remote access to your Raspberry Pi.

Securing Your Connection with an SSH Key Pair

  1. If you haven’t already generated an SSH key pair on your local machine, follow the steps in Setting Up Remote Access > 4 above.
  2. Disable password authentication for SSH:
    • Open /etc/ssh/sshd_config on your Raspberry Pi:
      sudo nano /etc/ssh/sshd_config
      
    • Find the line that says PasswordAuthentication yes and change it to PasswordAuthentication no.
    • Save and exit the file.
  3. Restart the ssh service for the changes to take effect:
    sudo systemctl restart ssh
    
  4. Now, when you connect to your Raspberry Pi from another device, you will only be able to authenticate with your SSH key pair. This adds an extra layer of security and makes it more difficult for unauthorized users to access your Raspberry Pi.