autossh in background does not work
Solution 1:
It seems like when autossh drops to the background (-f option) it is changing the working directory, meaning relative paths do not work any longer. Or more specific: By entering the absolute path of your id file you will probably succeed.
I re-created the scenario by creating a key with no password at a non-default location:
~/$ mkdir test
~/$ cd test
~/test$ ssh-keygen -f test_id_rsa
I simply hit enter twice to generate a key that is not protected by a password.
I copied the new key to my server (which allows password authentication currently):
~/test$ ssh-copy-id -i test_id_rsa user@server
First I confirmed the key was working with regular ssh, then using autossh like you:
~/test$ ssh -i test_id_rsa user@server
~/test$ autossh -M 13000 -N -i test_id_rsa user@server
^C
They both worked fine, so I recreated the problem you had:
~/test$ autossh -f -M 13000 -N -i test_id_rsa user@server
This did not work and the following was written to /var/log/syslog
:
autossh[2406]: ssh exited prematurely with status 255; autossh exiting
By changing the path of the keyfile to be absolute, it worked though:
~/test$ autossh -f -M 13000 -N -i /home/user/test/test_id_rsa user@server
No errors in /var/log/syslog
.
Solution 2:
Not sure what is going on with the -f but you could also nohup it:
nohup autossh -M 33201 -N -f -i myIdFile -R 33101:localhost:22 [email protected] &