Setup SSH Forward Tunneling and SSH Reverse Tunneling In The Same Line

Here’s a simple command that setups a local port tunneling via SSH and a reverse SSH Port Tunneling at the same time.

screen -dm autossh -p [22] [111.222.333.444] [-L 5555:localhost:6666] [-R 7777:localhost:22]

Put this is your rc.local file to run this on startup.

  • screen -dm creates a detached mode session other than your current one so you can have the command running after you exit the terminal
  • autossh is like SSH binary but auto reconnects if ever your connection gets terminated.
  • 22 is the SSH port, change this if you modified your server to listen on another port or your firewall does port forwarding
  • 111.222.333.444 is your target server address
  • -L 5555 is the local port number to use to forward your connection
  • localhost no explanation needed
  • 6666 the port on the other end to forward to
  • -R 7777 the port to use on the other side to connect to your home server ( your source )
  • localhost no explanation needed
  • 22 ssh port used, change this if you are listening on another port

So, if you want to connect to your source server ( home server ), all you do is connect to localhost via the port that was opened to do reverse SSH.

:> ssh [email protected] -p 7777

Leave a Reply

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