11

I have set up a tunnel via autossh.

This works:

autossh -M 33201 -N -i myIdFile -R 33101:localhost:22 autossh@myhost.com

I would like to run autossh in background. Seems easy using the -f option.

This does not work, however:

autossh -f -M 33201 -N -i myIdFile -R 33101:localhost:22 autossh@myhost.com

Autossh runs in the background fine, but the ssh connection seems to fail every time. In /var/syslog I see multiple occurences of:

autossh[3420]: ssh exited with error status 255; restarting ssh

What am I doing wrong? A wild guess is it has something to do with the authentication via key file. How can I debug this (adding -v to the ssh options does not seem to log anywhere).

Edit: I got some ssh logs using the -y option

/usr/bin/ssh[3484]: debug1: Next authentication method: publickey
/usr/bin/ssh[3484]: debug1: Trying private key: /home/myuser/.ssh/id_rsa
/usr/bin/ssh[3484]: debug1: Trying private key: /home/myuser/.ssh/id_dsa
/usr/bin/ssh[3484]: debug1: Trying private key: /home/myuser/.ssh/id_ecdsa
/usr/bin/ssh[3484]: debug1: No more authentication methods to try.
/usr/bin/ssh[3484]: fatal: Permission denied (publickey).
autossh[3469]: ssh exited with error status 255; restarting ssh

So it seems autossh does not accept my identiy file (-i myIdFile) when using the -f option. Why is that?

(autossh 1.4c on Raspian)

  • Why use autossh at all? You can use systemd for the "restart on failure stuff". I created a gist with my solution: gist.github.com/guettli/… – guettli Dec 22 '17 at 11:44
26

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.

  • awesome, this works. – henning77 Oct 10 '13 at 12:27
  • You saved my day! – arno_v Jul 7 '14 at 14:23
  • 2
    The -N option is important, otherwise you'd get "ssh exited with status 0; autossh exiting". Thanks! – user30747 Jul 29 '17 at 0:04
  • Confirmed, I too just had to add the path to the "id_rsa" file like so: autossh -M 19001 -fN -y -i /home/pi/.ssh/id_rsa (thanks @jmidgren) – Rich Oct 31 '18 at 10:51
4

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 autossh@myhost.com &
  • nohup works for me, even without specifying the key file. – valadil Apr 3 '14 at 14:57
  • nohup also worked for running autossh under runit in Alpine Linux – Stuart Cardall Aug 14 '16 at 22:52
1

Add the following parameters to SSH to bypass "Are you sure you want to continue connecting (yes/no)?"

-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no

The final command would be in the following format:

autossh -f -M $BASE_PORT -N -R $LOCAL_PORT:$LOCALHOST:$REMOTE_PORT $USER@$SERVER -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.