[SOLVED] Autossh in custom.sh works when invoked directly, but not on boot

I want to establish an autossh tunnel to my server, automatically on boot. I have a dietpi-autostart file that runs various things already, so I added the following line to it:

autossh -M 0 -o "ServerAliveInterval 30" -o "ServerAliveCountMax 2" -fN -i /home/dietpi/.ssh/id_rsa -T -R 43022:localhost:22 dietpi@myhost.com -p xxxxx

However, this does not run correctly on boot. journalctl shows this error:

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

Interestingly, if I invoke the entire custom.sh file after login manually, or copy paste just the autossh line into the shell, it runs just fine.

EDIT: Found the problem, but not the solution. custom.sh runs as root, but I need to be running autossh as “dietpi”.

EDIT: Used a systemd service, which works just fine!


1 Like

thx for sharing your solution.