Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Systemd service for autossh
Description=Keeps a tunnel to '' open
# -p [PORT]
# -l [user]
# -M 0 --> no monitoring
# -N Just open the connection and do nothing (not interactive)
ExecStart=/usr/bin/autossh -M 0 -N -q -o "ServerAliveInterval 60" -o "ServerAliveCountMax 3" -p 22 -l autossh -L 7474: -i /home/autossh/.ssh/id_rsa
Copy link

comperem commented Dec 3, 2017

Also, this link had a line that, I believe, solved the problem of restarting too quickly:

# Restart every >2 seconds to avoid StartLimitInterval failure

Copy link

guettli commented Dec 22, 2017

Since systemd cares for the restarting, I don't understand why the executable autossh is needed.

Could someone please explain this. I would like to understand the benefit of using autossh.

Copy link

Iiridayn commented Feb 8, 2018

@guettli thank you for the question, I can now remove another dependency. I'd been using autossh for so long that I didn't think about that.
@jeroenvermeulen thank you for the link - my munged version seems to be working great :).

Copy link

Could this be modified to take advantage of systemd's socket activation? I'd like to have an SSH tunnel only open when I need it (ex: when i make a request to localhost:1080).

Copy link

eldog commented Jun 3, 2018

Consider adding -o "ExitOnForwardFailure=yes" as if the client cuts the connection to the server (like power goes off), the port may still be considered in use on the server. Been having this problem when rebooting a Raspberry Pi via unplugging the USB power.

Copy link

One reason for using autossh, rather than normal ssh, is to have incremental backoff in the restart delays when the connection fails (e.g. the destination host is temporarily down). Systemd supports a fixed restart delay (RestartSec), but does not support increasing the restart delay (yet, see systemd/systemd#6129)

Copy link

llamafilm commented Jul 25, 2018

@comperem that won't work either because you have to specify the full binary path like this. Also, I found that a SIGKILL will leave the child ssh process running. If you use SIGTERM then it gracefully stops both autossh and ssh. On my distro (Raspbian), this works:
ExecStop=/usr/bin/killall autossh

Copy link

@llamafilm @comperem This is not the best practice in case of systemd.

By default, systemd.kill will send SIGTERM to all processes and subprocesses of the control group with or without an ExecStop command. If there is one, it will be executed first(So it's no need after all).

autossh receiving SIGTERM will start to kill ssh subprocess in its way, and systemd will wait for it (by default 90s) and send SIGKILL after timeout.

In that case, no ExecStop command is needed and systemd will handle it gracefully by default. If you have no patience, tuning TimeoutStopSec is all you need.

Copy link

ttimasdf commented Sep 5, 2018
Check out my fork if needed. Added:

  • ExitOnForwardFailure.
  • Handle restart automatically. (you can even replace autossh with ssh)
  • Multiple tunnels open simultaneously.
  • Move tunnel settings (key, user, port, etc) to a separate user's ~tunnel/.ssh/config. Easier to manage and test with sudo -u tunnel ssh -v

Copy link

For completeness, you should also add:

ExecStop=kill -9 autossh

Without it systemctl stop autossh won't do anything.

I'd put instead:

ExecStop=/bin/kill $MAINPID in sshd service. This way you can cleanly stop/kill only this particular process (of possibly many instances of autossh, for example).

Copy link

I get this error

mamot@pg-1-mamot-dawn:/etc/systemd/system$ systemctl start autossh-mamot-hook.service Failed to start autossh-mamot-hook.service: Interactive authentication required. See system logs and 'systemctl status autossh-mamot-hook.service' for details.

this is my script

ExecStart=/usr/bin/autossh -M 0 -N -o "ServerAliveInterval 60" -o "ServerAliveCountMax 3" -R

any idea ?

Copy link

jgrevich commented Jul 8, 2019

@dimasmamot Interactive authentication required seems to be the key part of the error msg to me. Try executing the command first as sudo to see what interactive prompts are required. Most likely you need to add a new host key to /root/.ssh/known_hosts.

Copy link

dynamic auttossh@.service
Description=autossh: %I
Documentation=file:///usr/share/doc/autossh/README.service man:autossh(1)

ExecStart=/usr/bin/autossh $OPTIONS


Copy link

kegwell commented Mar 25, 2020

Running into a strange problem....


Description=AutoSSH tunnel

ExecStart=/usr/bin/autossh -N -M 10984 -o "PubkeyAuthentication=yes" -o "PasswordAuthentication=no" -i ~/.ssh/id_rsa -R 6666:localhost:22 ********@********** -p 22


After running daemon-reload and sudo systemctl start autossh I can see it running after issuing a ps ax | grep autossh with the exact command in the ExecStart listed above. However, when I check my remote host, nothing is listening on port 6666.

However, if I copy/paste that exact ExecStart line into a terminal and run it manually, it connects fine. I can reverse SSH to port 6666 without issue. So the command runs fine, when entered manually....but with systemd I see the service running, no errors, but no connection. Any ideas?

This is on CentOS 7....

Copy link

Strykar commented Jan 14, 2021

There's some unsound advice in the comments here, see

Copy link

Some stuff is overkill, e.g.

  • you don't need the ExecStop, systemctl stop autossh will work just fine out of the box
  • you don't need the AUTOSSH_GATETIME setting

Copy link

ScumCoder commented Jul 12, 2022

Some stuff is overkill, e.g.

* you don't need the AUTOSSH_GATETIME setting

Of course you do, without it autossh will give up if the very first connection attempt fails.

You USED to not need it, because it was implied by the -f flag. Except that systemd does not support the -f flag.

Copy link

@ScumCoder this wasn't my experience of it, I never used AUTOSSH_GATETIME but I use autossh and it does retry. It's been a while since I looked at it though, so stuff may have changed.

Copy link

jotakar commented Dec 27, 2022

Hi, I have this service for tunnel with autossh

Description=Open my Tunnel

ExecStart=autossh -M 12000 -N -f -o "PubkeyAuthentication=yes" -o "PasswordAuthentication=no" -o ServerAliveInterval=60 -o ServerAliveCountMax=3 -i /home/usrtunel/.ssh/clavetunel -R 7090:localhost:7080 -p YYY


If I run the autossh by hand, in command line, the tunnel remains open and working fine.
But with the service I found that every a while the service restart
My log shows this:

Dec 27 19:26:51 myserver autossh[24726]: starting ssh (count 1)
Dec 27 19:26:51 myserver autossh[24726]: ssh child pid is 24727
Dec 27 19:26:51 myserver autossh[24726]: received signal to exit (15)
Dec 27 19:26:57 myserver autossh[24732]: starting ssh (count 1)
Dec 27 19:26:57 myserver autossh[24732]: ssh child pid is 24733
Dec 27 19:26:57 myserver autossh[24732]: signalled to exit
Dec 27 19:28:32 myserver autossh[24897]: starting ssh (count 1)
Dec 27 19:28:32 myserver autossh[24897]: ssh child pid is 24898
Dec 27 19:28:32 myserver autossh[24897]: signalled to exit
Dec 27 19:30:08 myserver autossh[25106]: starting ssh (count 1)
Dec 27 19:30:08 myserver autossh[25106]: ssh child pid is 25107
Dec 27 19:30:08 myserver autossh[25106]: signalled to exit
Dec 27 19:31:43 myserver autossh[25278]: starting ssh (count 1)
Dec 27 19:31:43 myserver autossh[25278]: ssh child pid is 25279
Dec 27 19:31:43 myserver autossh[25278]: signalled to exit

What I see is that service stop autossh every few minutes, why? where is the error?

Copy link

@jotakar I've been using:

Description=Keep open a reverse tunnel to my computer via the DMZ server

ExecStart=/usr/bin/ssh -NT tunnel


With tunnel defined in /root/.ssh/config as

Host                    tunnel
HostName                <redacted>
User                    <redacted>
IdentityFile    ~/.ssh/id_tunnel
ProxyCommand    ssh bastion -W %h:%p
RemoteForward   <redacted port> localhost:22
ExitOnForwardFailure yes
ServerAliveCountMax 5

And the bastion host also defined in the same file as

Host           bastion
HostName       <redacted>
User           <redacted>
IdentityFile   ~/.ssh/id_tunnel
ForwardAgent yes

I also have

Host *
ServerAliveInterval 60
IdentitiesOnly yes

at the top of my /root/.ssh/config, on the off-chance that's relevant.

I've found this to be very consistent and stable, and easy to test (ssh bastion, ssh -NT tunnel) when setting it up. Perhaps removing autossh and setting it up this way might help?

Copy link

mikkorantalainen commented Mar 22, 2023

For completeness, you should also add:

ExecStop=kill -9 autossh

Without it systemctl stop autossh won't do anything.

I think it would be better idea to add


to the .service file because that will kill everything that was started (recursively) and nothing more. In addition, it will first send SIGTERM and use SIGKILL only if the process will not stop nicely.

If you randomly kill one or all autossh processes in the system, you might kill more than expected if autossh is used for other stuff, too.

See for details

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment