AutoSSH is a great tool to maintain a persistent SSH tunnel. Here is how to start AutoSSH on boot so that the tunnel can survive system reboot of the local machine.
Here we assume you can already ssh into the remote machine without typing the password. If not, see my previous post on how to set up it.
First, on your local machine, switch to root user:
$ sudo su
Second, ssh into remote machine as root so the remote machine is added to your root's known_hosts.
# ssh <user>@<remote_host>
Third, add this line to your /etc/rc.local.
autossh -N -f -i /home/<user>/.ssh/id_rsa -R 22222:localhost:22 <user>@<remote_host>
The command arguments are:
- -N: tell ssh to not execute any command, since we only use it for tunneling.
- -f: tell autossh to fall into background on start.
- -i: tell ssh to use the proper identity.
- -R 22222:localhost:22: reverse tunnel remote host's 22222 port to localhost's 22 port. So that we can use ssh -p 22222 localhost on remote host to ssh into local machine.