Ssh – Is autossh redundant with systemd

autosshdebiansystemd

When creating a persistent reverse SSH tunnel, is autossh useful on a system running systemd? Typically, autossh is run by a service with the option -M set to zero which disables monitoring of the link. This means that ssh has to exit before autossh will restart it. From the man page:

Setting the monitor port to 0 turns the monitoring function off, and
autossh will only restart ssh upon ssh's exit. For example, if you are
using a recent version of OpenSSH, you may wish to explore using the
ServerAliveInterval and ServerAliveCountMax options to have the SSH
client exit if it finds itself no longer connected to the server. In
many ways this may be a better solution than the monitoring port.

It seems that the systemd service itself is capable of doing this with a service file that contains these options:

Type=simple
Restart=always
RestartSec=10

So is autossh redundant when run by a systemd service? Or is it doing other things that help to keep the SSH connection up?

Thanks.

Best Answer

Since trying both is relatively easy, test the reliability of both approaches and confirm for yourself.

I think you'll find that autossh is better suited for the job. autossh designed to run in the foreground, while systemd is designed primarily for background services that aren't attached to a particular TTY.

Also, autossh has at least one feature specific to the task:

Periodically (by default every 10 minutes), autossh attempts to pass traffic on the monitor forwarded port. If this fails, autossh will kill the child ssh process (if it is still running) and start a new one;

So autossh is doing more than keeping a process running, it's confirming that the ssh connection is actually working.

Related Question