Persistent Reverse (NAT Bypassing) SSH Tunnel Access With Autossh
Persistent Reverse (NAT Bypassing) SSH Tunnel Access With Autossh
•Tutorials
Table of Contents
1. Naming convention:
2. Install the tools
3. Create your ssh-key.
4. Copy your key to the middleman machine
5. Test the connection with autossh
6. SSH back in the restricted host
7. Enable the tunnel on boot
8. Forward a website, not ssh
9. Other host inside restricted network
Situation: you are in a restricted network (company, hotel, hospital) where you have a
"server" which you want to access from outside that network. You cannot forward ports to
that machine, but you can ssh outside (to your own server). This tutorial solves this
problem.
You need another server to which you setup a persistent ssh connection with a reverse
tunnel. Then if you need to access the machine you ssh into the other server, and from
there you ssh through the tunnel to the restriced machine.
Make sure you have permission to do this from the administrators. They generally don't like
holes in the firewall/security. They don't block it for no reason.
Naming convention:
restricted machine: machine inside the restricted network middleman: machine to which
the restricted machine sets up the tunnel, and from which you access the restricted server
Install the tools
We are going to use autossh. This is in the debian/ubuntu repositories. Make sure you also
install openssh server.
ssh-keygen
(replace remy@middleman with your username and middleman ssh server. Also note how
you can give a custom port in the ssh-copy-id.)
Explanation"of options:
• remy@middleman -p 2222: ssh user remy, ssh host middleman, ssh port 2222
If this all goes well you should be logged in to the middleman host without being asked for a
password. You might get the question if you want to add the ssh key. Say yes to this.
If it does not go well, check the permissions on the ssh key (should be 600), and make sure
you have the correct values in the autossh command.
From the middleman, ssh into the restricted host via the reverse tunnel we created:
If all goes well, you should see a prompt to login to the restricted machine. Enter your
password and go. If this goes well, you can continue. If this does not work, check the values
in the command and the ssh configs. Also make sure you have executed the steps above
correctly.
• &: Execute this command but do not wait for output or an exit code. If this is not
added, your machine might hang at boot.
sudo /etc/rc.local
If you get your regular promt back without any output you've done it correct.z
This will forward all traffic to port 7777 on host middleman, via host restrictedhost, to host
host2.restrictednetwork port 22.