Linux – Autossh ask for password when running with sudo (or from upstart)

linuxsshubuntu-12.04upstart

I have an upstart job /etc/init/tunnel.conf:

description "SSH Tunnel"

start on (net-device-up IFACE=eth0)
stop on runlevel[016]

respawn

exec autossh -nNT -o ServerAliveInterval=15 -R 12345:localhost:22 myuser@myserver

When I look at the /var/log/upstart/tunnel.log:

Permission denied, please try again.
Permission denied, please try again.
Permission denied (publickey,gssapi-with-mic,password).

but if I try from terminal

autossh -R 12345:localhost:22 myuser@myserver

It connects to myserver without asking me for password (I have copied the SSH keys)

When I run it using sudo:

sudo autossh -R 12345:localhost:22 myuser@myserver

It asks me for myserver password, so I guess this is the problem I have with the upstart job. Why SSH is asking me for password we I run it as a root?

Best Answer

When autossh invoked by sudo or init process, autossh use identity/ssh-keys file provided by root user (e.g. /root/.ssh/sshkeys). When you try run autossh from terminal, maybe you use non-root user. Thus, autossh use identity/ssh-keys file provided by that user (e.g /home/non-root/.ssh/sshkeys).

To get expected behavior, you can provided identity file in tunnel.conf. To do that, modify last line to

exec autossh -nNT -i /home/non-root/.ssh/sshkeys -o ServerAliveInterval=15 -R 12345:localhost:22 myuser@myserver

More info in Autossh with Ubuntu Upstart

Related Topic