Reverse SSH Port Forwarding

The way the network configuration has been done at my office makes it impossible to do a port forwarding to access a specific machine outside the network. I figured out that it could be possible to perform a reverse ssh port forwarding.

1. You need a server machine that will accept ssh connections : Install ubuntu(because its the easiest to install) on a machine at home. Make sure you do a port forwarding on your router if you have to and map a dynamic dns from on that machine.

apt-get install openssh

2. The host machine at my office(Linux Based) would need to verify the connection to the outside server every time because the IP address of the office is dynamic and the server at your place is not on all the time.

Write a small script in /home/username that would do the following:

COMMAND=”ssh -N -f -R 2210:localhost:22”
pgrep -f -x “$COMMAND” > /dev/null 2>&1 || $COMMAND

Add that script to your crontab(runs it every 5 minutes)

*/5 * * * * /bin/sh /home/username/

3. Perform RSA key pairs

Because the two machines need to authenticate before communicating with each other, we will need to make an automatic login via rsa public/private key pairs.

ssh-keygen -t rsa (Run this on the host machine)

when all steps of the keygen are done,(Press enter until its over, no need to input anything)

cd ~/.ssh/


And you’re done. You should now be able to login to the host machine by login in on the server machine and running “ssh -p 2210 localhost” when logged in as root.

Gulshan Yashveer Beejan

Gulshan holds a BSc(Hons) Computer Science with Distributed systems from the City University of London. He is currently working at DSOGroup(Mauritius) as a Linux Administrator on SaaS Solutions. More about Gulshan

One thought on “Reverse SSH Port Forwarding

Leave a Reply

Your email address will not be published. Required fields are marked *