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:
1 | $ sudo su
|
Second, ssh into remote machine as root so the remote machine is added to your root's known_hosts.
1 | # ssh <user>@<remote_host>
|
Third, add this line to your /etc/rc.local.
1 | 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.