Have you ever wanted to ssh to your Linux box that sits behind NAT? Now you can with reverse SSH tunneling. This document will show you step by step how to set up reverse SSH tunneling. The reverse SSH tunneling should work fine with Unix like systems.

Let’s assume that Destination’s IP is 192.168.20.55 (Linux box that you want to access).

You want to access from Linux client with IP 138.47.99.99.

Destination (192.168.20.55)

1. SSH from the destination to the source (with public ip) using command below:

ssh -R 19999:localhost:22 sourceuser@138.47.99.99

* port 19999 can be any unused port.

2. Now you can SSH from source to destination through SSH tuneling:

ssh localhost -p 19999

3. 3rd party servers can also access 192.168.20.55 through Destination (138.47.99.99).

Destination (192.168.20.55)

3.1 From Bob’s server:

ssh sourceuser@138.47.99.99

3.2 After the sucessful login to Source:

ssh localhost -p 19999

* the connection between destination and source must be alive at all time.

Tip: you may run a command (e.g. watch, top) on Destination to keep the connection active.

This is the script to add in the cronjob best all 5 min:

if [ -z "`ps | grep use[r]`" ]
then
ssh -R 18000:localhost:22 -f -N -T user@zurich.host.com -p 6022 &
echo "start autotunnel" >> tunnellog.log
else
echo "autotunnel is started" >> tunnellog.log
fi

to add the ssh key:

ssh user@192.168.59.123 -p 6022 mkdir -p .ssh

cat .ssh/id_rsa.pub | ssh user@192.168.59.123 -p 6022 ‘cat >> .ssh/authorized_keys’

don’t forget on the target:

ssh-keygen -t rsa

Client:~# cd /home/user/.ssh/
Client :~# ssh-keygen -t rsa
Anmerkung bzgl. Passphrase: 3 x RETURN drücken, da sonst diese bei Anmeldung abgefragt wird. Ggf. können Sie natürlich zwecks Sicherheit die Passphrase nutzen.

to generate the key in the server: ssh-keygen -t rsa -b 2048

ssh-copy-id root@servername.comain.ch

ssh-copy-id “user@host -p 8129”

Script um via ssh auf den eine bliebige IP und Port via ssh Tunnel zuzugreifen
#! /bin/bash
# establish a connection to customer
# use Firefox with htp://localhost:8888
ssh -L 8888:"targetIP":"targetPort" root@host.office-on-the.net -p 22