You might also want to work on your project when traveling or not at home, and you might need to interface with the Pi remotely to do so. This guide goes over how to connect to the Raspberry Pi through SSH remotely, from anywhere in the world.
-
When you connect to your network's router, it assigns your device a local IP within your network. It likely has the shape of
192.168.1.XXX
or10.0.0.XXX
. This IP is an address for every device connected to your network, and is assigned by your router every time you connect to the network. It is essentially used to track down who is who and where to send the info and from where. -
Your router also has an IP address as it is a device in your network, and it is usually the first one (
192.168.1.1
or10.0.0.1
). -
These types of IP addresses are only local, meaning when you type
192.168.1.XXX
in your address bar in a browser, you are talking to whatever device in your network that got assigned the IP address192.168.1.XXX
. If you are in a different network, however, (connected to a different router), typing the same will connect you to a different device, that has the same local IP address in the new network you are in.
-
In contrast, each router (or network) has a public IP address. This public IP is used to connect to networks anywhere in the world through the internet. It would then make sense that to connect to one device somewhere in the world, you would first need the public IP address to connect to the network where this device is located, and then the local IP address to connect to the particular device in the network.
-
If having trouble visualizing this, consider the following analogy. Imagine you want to talk to a specific person, whose name is
Luke
.Luke
's name, in this case, is the person'slocal IP
. You can findLuke
's in many houses, anywhere in the world. So first, you attempt to call theLuke
's Family's house, whosephone number
is equivalent to thepublic IP address
. Then, you would askLuke
's parent (the router) to transfer you to whomever you want to talk to, in this case,Luke
(local IP
), and then you get to talk directly toLuke
. -
This works the same way for networks. In this case, when connecting to your public IP from somewhere in the world, you will specify a port number. You will have also previously told the router that this particular port number routes straight into your Pi. And so connecting to your public IP address through this port will have the router route you straight into whichever address your Pi has locally.
-
Services, like google.com, have servers (that you connect to when you visit them) that might constantly change, or have multiple.
-
If, instead of connecting to google.com, you had to connect to an IP address that brings you to Google's servers, you would constantly have to keep track of which IP is currently the right one, and alternatively, it would be really hard to remember hundreds of long number strings for every service or website you frequent.
-
To get around this, your browser uses a DNS service, which is essentially the internet's phone book. It is a publicly available list that relates each literal website address (such as google.com or facebook.com) to a public IP address. When you type in something like "google.com", your browser asks the DNS where "google.com" leads, and the DNS returns a public IP (like "73.454.324.12"), which your browser connects to download the website.
-
Any time Google needs to change their servers, they change the Public IP Address in the DNS that corresponds to the address: google.com, so you, as a user, don't need to worry about keeping track of IP Addresses.
-
Although it would be possible to connect to your router anywhere in the world by just typing your public IP address (which you can find by, while being connected to the network, typing in Google "what is my public IP address"), this address might change due to your Internet Service Provider, or by you moving, or many other reasons.
-
For that reason, it is a good idea to use a DNS service (registering your public IP as a literal website address, so that you or others can easily navigate to it using a browser).
-
For this, we will use DuckDNS, which is a free DNS service.
Important
This step is not necessary, but recommended.
-
Go to DuckDNS's website.
-
Sign in, or create an account if you don't have one.
-
On the "Domains" page, put a name for your domain, and click "Add domain".
-
On the domain listing, under current IP, enter your public IP.
[!NOTE] You can look up on Google "what is my public IP", but you have to be connected to your network to do this.
-
Click on Update IP.
-
You have successfully set up a domain for your own network! Now, typing in
https://whateverdomainyouused.duckdns.org
on any browser will take you straight to your network![!NOTE] If you try to do that now, you will most likely either not see anything, or see your router's login page, as we haven't told your router what to do with new connections yet.
We can now connect to our network, by using either our public IP, or our newly established domain address. However, the router still does not know to route us to the Pi. This step will guide you through doing so through port forwarding.
-
Connect to your router.
[!NOTE] Type
192.168.1.1
or10.0.0.1
in a browser's address bar, depending on your router's architecture, while being connected to your network. -
Input your username and password.
[!NOTE] If you haven't changed them, they will have a default user and password. You can find that by googling online your router's model, or it is sometimes found on a tag underneath the router itself.
-
Every router is different, but look for a port forwarding, or port pass-through, or port opening, or port trigger setting in your NAT settings.
[!NOTE] Here, you will find rules that tell your router how to handle specific connections form outside networks.
-
Add a rule to tell your router to send you to your Pi when connecting to a specific port number:
-
Select
TDC_UDP
as the protocol.[!NOTE] If unable, you might need to add two rules, one for
TDC
and one forUDP
. -
For the external port, pick one of your choice, like
10000
. This is the port you will use to be forwarded to your Pi. -
For the local IP address, pick your Pi's local IP address.
[!WARNING] It is strongly recommended having set up a static IP address for your Pi. For a guide on how to do so, visit 2.1 - Set up a permanent IP Address.
-
For the internal port, pick port
22
.[!NOTE] This is the local port usually utilized for SSH.
-
-
Save, and restart the router.
Note
You can be connected to the same network (we will just be coming in from an outside connection).
-
On a command window, run:
-
ssh [WHATEVER_USERNAME_YOU_SET_UP_FOR_SSH_OR_DEFAULT]@[EITHER_PUBLIC_IP_OR_YOUR_DOMAIN_ADDRESS] -p [WHATEVER_EXTERNAL_PORT_NUMBER_YOU_SET]
. -
It should look something like this:
ssh [email protected] -p 10000
.
-
-
If the Pi asks you for your password, congratulations!!! You just set up a remote connection.
-
If you see a connection refusal:
-
It means either the Pi is not set up to SSH, or you are connecting to something other than the Pi.
-
Make sure that the port 10000 is going to your Pi's IP address.
-
-
If you see a connection timeout:
-
It means you are not reaching any device.
-
Make sure your domain settings are correct.
-
-
To verify if it should be working, you can always try to connect locally:
- Run:
ssh [WHATEVER_USERNAME_YOU_SET_UP_FOR_SSH_OR_DEFAULT]@[PI_LOCAL_IP] -p [WHATEVER_EXTERNAL_PORT_NUMBER_YOU_SET]
in a terminal. - Do not keep the crochets
[]
when changing the values.
- Run:
-
Install necessary packages.