This page captures notes I took while experimenting with reverse ssh and ssh port forwarding.
These instructions are adapted from this tutorial. They show the steps necessary to connect to a Raspberry Pi behind a series of firewalls by making a reverse SSH connection.
Notes:
- The raspberry pi is on my local network at 10.10.10.219 with username 'pi'
- My internet-connected server is a Digital Ocean Droplet at IP 45.55.12.52 with username 'trout'
Steps
- On the RPi I run this command:
ssh -R 19999:localhost:22 [email protected]
This will open an SSH connection between the RPi and the server. It will prompt me for the password to 'trout', and then log me in under that user on the server.
- Open a separate SSH terminal directly to the server. From there, enter this command:
ssh -p 19999 pi@localhost
This will open an SSH connection between the server and the RPi inside its network. It will prompt me for the password to user 'pi'.
- As long as the connection in step 1 remains established, I can open and close connections using step 2.
Instructions below are pieced together from the following tutorials:
- https://toic.org/blog/2009/reverse-ssh-port-forwarding/
- https://askubuntu.com/questions/50064/reverse-port-tunnelling
Steps:
- Enable Gateway ports in the SSHd server.
sudo nano /etc/ssh/sshd_config
- At the bottom add this:
GatewayPorts clientspecified
- Save the file and restart sshd with this command:
/etc/init.d/ssh restart
-
On the RPi, establish a reverse SSH connection with port forwarding with this command:
ssh -R 45.55.12.52:8000:localhost:80 [email protected]
This forwards port 80 on the RPi to port 8000 on the server (45.55.12.52). It will request the password for user 'trout'. -
Access the webpage being served by the RPi on port 80 by calling http://45.55.12.52:8000
This solution comes from this Stack Overflow thread:
-
Setup the GatewayPorts in the sshd_config file.
-
Setup a port 80 forwarding on the RPi to port 8080 on Droplet server:�
ssh -R 107.170.227.211:8080:localhost:80 [email protected]
-
Add these lines to the nginx default file:
server {
listen 80;
server_name rpi.christroutner.com;
location / {
proxy_pass http://christroutner.com:8080;
}
}
-
Check the syntax of the file with this command:
sudo nginx -t
-
If the syntax checks out, reboot the nginx service:
sudo service nginx restart
-
Point a browser at rpi.christroutner.com
- Create a user on the server. Assuming user 'user-ssh' and server with IP 104.236.184.95
- On RPi: 'ssh-keygen' to generate key
- On RPi: 'ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]'
- Now log into server with 'ssh [email protected]' and a password should not be needed.
- Setup a subdomain on christroutner.com that points to an RPi. Proxy pass ports 80, 443, and 22.