Remotely Control Your Raspberry Pi Via SSH From an External Network: Beginner's Guide (Part 3)

by mahmoodmustafashilleh in Circuits > Raspberry Pi

3795 Views, 2 Favorites, 0 Comments

Remotely Control Your Raspberry Pi Via SSH From an External Network: Beginner's Guide (Part 3)

SSH From Anywhere.png
Remotely Control your Raspberry Pi via SSH from an External Network: Beginner's Guide (Part 3)

Learn how to SSH into your Raspberry Pi from any network. This is critical in IoT since you can control your device from anywhere in the world with this technique. This is an extension of the previous video where we only showed how to set up SSH on the local network. We take it a step further in this tutorial by removing that limitation.

Before reading the remainder, be sure to subscribe and support the channel if you have not!

Subscribe:

Youtube

Support:

https://www.buymeacoffee.com/mmshilleh

Hire me at UpWork to build your IoT projects:

https://www.upwork.com/freelancers/~017060e77e9d8a1157

Supplies

  • A Raspberry Pi with Raspbian OS installed
  • Access to your home router
  • An internet connection
  • Basic familiarity with the command line

Enable SSH on Your Raspberry Pi

Before you can remotely access your Raspberry Pi, you need to ensure SSH is enabled:

  • Connect your Raspberry Pi to a monitor and a keyboard.
  • Open the terminal from the desktop.
  • Enter sudo raspi-config in the terminal.
  • Navigate to 'Interfacing Options', then 'SSH', and select 'Enable'.
  • Exit the configuration tool.

Setting Up a Static IP Address

A static IP address ensures that your Raspberry Pi is always accessible at the same address on your local network, which is crucial for reliable remote access. It's important to choose an IP address that is on the same subnet as your router to ensure proper network communication.

Open a terminal on your Raspberry Pi and edit the DHCP client configuration file by typing:

sudo nano /etc/dhcpcd.conf

Scroll to the end of the file and add your static IP configuration:

# Example static IP configuration:
interface wlan0
static ip_address=192.168.1.100/24 # Ensure this IP is on the same subnet as your router's IP
static routers=192.168.1.254
static domain_name_servers=192.168.1.254 8.8.8.8
  • Replace wlan0 with eth0 if you are using an Ethernet connection.
  • The IP address 192.168.1.100 should be in the same IP range as your router (for example, if your router is 192.168.1.254, then 192.168.1.XXX is appropriate).
  • Save and exit by pressing CTRL+X, then Y, and Enter to confirm.

Reboot your Raspberry Pi to apply the changes:

sudo reboot

This adjustment emphasizes the need to ensure the IP address is on the same subnet as the router, which is crucial for the proper operation of the network.

Configure Port Forwarding on Your Router

To access your Raspberry Pi from outside your local network, you'll need to set up port forwarding:

  • Access your router’s admin panel by entering its IP address in a browser.
  • Locate the ‘Port Forwarding’ section.
  • Add a new rule to forward an external port (e.g., 2222) to the internal IP address of your Raspberry Pi (192.168.1.100) on port 22 (SSH).
  • Save your settings.

If any confusion, see YouTube video above for more details on this, as it can vary from router to router.

Connecting Remotely

Now that everything is set up, you can connect to your Raspberry Pi from anywhere:

  • From any SSH-capable device, open your SSH client.
  • Connect using your external IP and the port you forwarded:
ssh pi@your-external-ip -p 2222
  • Replace your-external-ip with your actual external IP address.
  • Enter your Raspberry Pi's credentials when prompted.
  • Also replace pi with your Raspberry Pi username sign-in.

Security Tips

  • Change the default password of the pi user to enhance security.
  • Consider setting up key-based authentication for SSH.
  • Regularly update your Raspberry Pi to keep it secure.

Conclusion

Setting up your Raspberry Pi with a static IP and enabling remote SSH access is a great way to maximize its utility and securely manage it from anywhere. Whether you’re using it for personal projects or as a part of your smart home setup, these steps will ensure a reliable and secure connection. Please subscribe to the channel if you want to learn more about IoT, Raspberry Pi, and more! Feel free to ask questions.