This is an extract from a guide I wrote a while back.
Connecting to your Raspberry Pi from “outside” may not be as straightforward as you’d expect.
With connecting, I mean of course using the ssh protocol, which has been the most common way to log into *nix systems for ages.
If you are using GPRS or 3G to connect your Pi to the internet, chances are that it is impossible to establish an ssh session to your Pi.
However, initiating an ssh session from the Pi itself to a computer elswhere on the internet, should in most cases not be a problem. In fact, once you have established a connection to another machine from your Pi via ssh, you can use this connection to connect back to your Pi.
This can be done by what is called reverse ssh port forwarding, a technique that at first eyesight looks very complicated and difficult to get ones head aroud.
I will now go through the steps that you need to perform to be able to log in to your Raspberry Pi from anywhere.
The first thing you need, is a Linux server. I highly recommend Amazon EC2, where you can get a server instance for free (well, almost). I do not cover the details on how to configure it, in this document.
Setting up the server
sudo vi /etc/ssh/sshd_config
Add the following line to the file:
GatewayPorts clientspecified
Then, restart the ssh daemon:
sudo vi /etc/init.d/sshd restart
The command above may give an error, because this procedure differs amongst Linux distributions. You may also try
sudo vi /etc/init.d/ssh restart
Next, you need to allow for incoming connections on TCP port 2222. In Amazon EC2, this is done by editing the security group used by your instance.
Setting up your Raspberry Pi
If you are running an Amazon EC2 Linux instance, you have a .pem-file, which was generated when you created your EC2 instance. This file contains your private key, and must be used when you log in to your EC2 instance.
Open an ssh connection to your Raspberry Pi.
Go to the /home/pi/.ssh
folder. If you do not have this folder, create it.
On your host machine, copy the content of your .pem
-file to the clipboard.
On your Raspberry Pi, type the following:
vi ec2.pem
Then press i, and paste the content of the clipboard into the file, by pressing CTRL+V, or right-click and Paste. Then save the file, by first pressing ESC, then :qw
followed by ENTER.
Next, type the following:
chmod 400 ec2.pem
Next, we need to create the ssh config file. If not present, create it in /home/pi/.ssh
folder, with the name config.
This example is assuming that you are running an Amazon EC2 instance. You will need the ip-address of your instance, to proceed. I highly recommend assigning an elastic IP address to your instance.
Into the config file, paste the following (change the ip address to that of your own Amazon EC2 instance):
host *.amazonaws.com 54.124.234.5
user ec2-user
StrictHostKeyChecking no
UserKnownHostsFile /dev/null
CheckHostIP no
IdentityFile ~/.ssh/ec2.pem
Save the file.
If everything is configured correctly, we should now be able to connect our Raspberry Pi to our server.
ssh -XfN2R 0.0.0.0:2222:localhost:22 [email protected]
The command should succeed without any error messages.
The following script is more convenient. It checks if a connection already exists, and spawn one if not. You could execute the script periodically from crontab, to ensure that the connection is always up.
#!/bin/sh
CMD="ssh -XfN2R 0.0.0.0:2222:localhost:22 [email protected]"
pgrep -f -x "$CMD" > /dev/null 2>&1 || $CMD