Reverse SSH Tunneling Between Two Linux Hosts

Last Updated on

Reverse SSH tunneling allows you to access a host by traversing a firewall / nat router by creating an outbound session from the obfuscated machine.   SSH is a very useful and powerful tool when comes time to access a remote machine or server securely. If you have a machine behind a firewall or NAT router – we can use this method to create a tunnel to a remote server;  from which you can ssh to a port on the remote server to access a local shell session on the server behind the network obstruction.

Install AutoSSH with the following:

1
sudo apt install autossh

AutoSSH can be run from systemd, rc.local, chron or any other method you prefer.

1
autossh -M 10900 -fN -o "PubkeyAuthentication=yes" -o "StrictHostKeyChecking=false" -o "PasswordAuthentication=no" -o "ServerAliveInterval 60" -o "ServerAliveCountMax 3" -R remote.host.com:16001:localhost:22 root@remote.host.com

In the above 16001 is used as the port on the remote server which you will connect to, this can be changed to your liking.  After autossh is running, you can connect to your tunnel (from the target server, so connect via ssh from the client):

1
ssh -p 16001 root@localhost

Note that with this example, passwordless ssh is already configured.  Test your connection prior to setting up autossh!

Maybe try making it interesting and mounting an ssh host as a mount point via fstab.

Lima

About the author

Lima is the visual nautical indicator for "stop instantly."

Comments

  1. How do you setup on remote.host.com the echo/reply service on port 10900/10901 ? As far I understand, to use the M option on autossh, you should provide a remote echo service, isn’t ? And why you setup your own echo service on port 10900/10901 instead of using the standard echo service that runs on port 7 ? What are the advantages doing that ?
    Another question is related to the option N. Mine does not have that option. What it does ?
    best regards,

    1. If you are tunnelling and using an older version of ssh that does not support the -N flag, you should upgrade (your version has security flaws).

Leave a Reply

%d bloggers like this: