Expose Raspberry Pi to External Networks

Expose Raspberry Pi to External Networks

14/02/2023

I’m looking to use my Raspbery Pi when I’m away from home and couldn’t find a solid tutorial on how to so here is my own.

I’ll be using SSH to access the Raspberry Pi. My setup is just the Raspberry Pi connected to the internet via WiFi with SSH enabled

Enable SSH

Log into the Pi and enter config more (you’ll need to have sudo access):

sudo raspi-config

Select Interface Options, then select SSH and enable it - super easy! Note your Pi’s local IP address (via ifconfig) which will look something like 192.168.0.XXX

Try logging in from a computer connected to the same network via:

ssh user@192.168.0.XXX

Enter your password and you should be in! Make sure to change it from the default since you’ll be exposing the Pi to the big scary world.

I find it helpful to set a static IP address to prevent your local IP address from changing. I wrote a tutorial here.

Change Router Settings

Next, we are going to forward the Raspberry Pi’s 22 port.

Log into the admin page of your router. Again, make sure you are on the same network as both the Raspberry Pi and your router. Usually, there are admin login details on the router. You access the admin page by the local IP on your web browser. E.g. go to 192.168.0.1 on Chrome and log in.

Go to your advanced settings and check if there is a port forwarding section. This will differ from router to router but the general underlying idea is the same.

There are 3 things that you’ll need to think about. The (local) IP address of the Raspberry Pi, the local port (22) and the external port that we will forward the traffic to - I chose 8088.

Add a new rule with these parameters. There may be other fields that are available. For example, I have a Virgin Media router that offered a start and end port to enable a whole range of ports.

For the protocol, select TCP.

Verify the Port Forwarding

While on the Raspberry Pi check your external IP address by querying this website:

curl icanhazip.com

To check that the port forwarding has worked, connect to another network and try to ssh into your Raspberry Pi. Instead of the local IP address, now use the external IP address.

ssh user@XXX.XXX.XXX.XXX -p 8088

But my IP address changes!

ISPs usually have dynamic IPs for your router meaning that your public IP will likely change over time. To get around this, you can use the free DDNS service Duck DNS to create domain for you.

You then update the mapping between the current public IP address of the router to the domain that Duck DNS have given you.

Set up your free subdomain and follow the instructions on their site. You’ll be creating a cron job on the Pi that will update the public IP every so often.