AUTOSSH(1)
π AUTOSSH(1) β bestie, your ssh session is getting a bodyguard π
Okay so this is the autossh(1) page from the General Commands Manual, and it is literally the friend who keeps your ssh sessions alive. No cap. Letβs get into it π
β¨ NAME β¨
autossh β monitor and restart ssh sessions π«Ά
β¨ SYNOPSIS β¨
autossh [-V] [-M port[:echo_port]] [-f] [SSH_OPTIONS]
πΈ DESCRIPTION πΈ
autossh is a program that starts a copy of ssh and monitors it, restarting it as necessary if it dies or stops passing traffic. Itβs giving βI got you, bestieβ energy π₯
The original idea and the mechanism came from rstunnel (Reliable SSH Tunnel). With version 1.2 of autossh, the method changed: autossh uses ssh to construct a loop of ssh forwardings β one from local to remote, one from remote to local β and then sends test data that it expects to get back. The idea is thanks to Terrence Martin β¨
With version 1.3, a new method was added (thanks to Ron Yorston): a port may be specified for a remote echo service that will echo back the test data. This avoids the congestion and the aggravation of making sure all the port numbers on the remote machine do not collide. The loop-of-forwardings method remains available for situations where using an echo service may not be possible π
π CONTROLLING SSH π
π SSH exits π
autossh tries to distinguish the manner of death of the ssh process it is monitoring and act appropriately. The rules are:
- If the
sshprocess exited normally (for example, someone typed"exit"in an interactive session),autosshexits rather than restarting; - If
autosshitself receives aSIGTERM,SIGINT, or aSIGKILLsignal, it assumes that it was deliberately signalled, and exits after killing the childsshprocess; - If
autosshitself receives aSIGUSR1signal, it kills the childsshprocess and starts a new one; - Periodically (by default every 10 minutes),
autosshattempts to pass traffic on the monitor forwarded port. If this fails,autosshwill kill the childsshprocess (if it is still running) and start a new one; - If the child
sshprocess dies for any other reason,autosshwill attempt to start a new one.
π¦ Startup behaviour π¦
If the ssh session fails with an exit status of 1 on the very first try, autossh
- will assume that there is some problem with syntax or the connection setup, and will exit rather than retrying;
- There is a
"starting gate"time. If the firstsshprocess fails within the first few seconds of being started,autosshassumes that it never made it"out of the starting gate", and exits. This is to handle initial failed authentication, connection, etc. This time is 30 seconds by default, and can be adjusted (see theAUTOSSH_GATETIMEenvironment variable below). IfAUTOSSH_GATETIMEis set to0, then both behaviours are disabled: there is no"starting gate", andautosshwill restart even ifsshfails on the first run with an exit status of1. The"starting gate"time is also set to0when the-fflag toautosshis used.
π Continued failures π
If the ssh connection fails and attempts to restart it fail in quick succession, autossh will start delaying its attempts to restart, gradually backing farther and farther off up to a maximum interval of the autossh poll time (usually 10 minutes). autossh can be "prodded" to retry by signalling it, perhaps with SIGHUP (kill -HUP). π
π Connection setup π
As connections must be established unattended, the use of autossh requires that some form of automatic authentication be set up. The use of RSAAuthentication with ssh-agent is the recommended method. The example wrapper script attempts to check if there is an agent running for the current environment, and to start one if there isnβt π§
It cannot be stressed enough that you must make sure ssh works on its own, that you can set up the session you want before you try to run it under autossh π«Ά
If you are tunnelling and using an older version of ssh that does not support the -N flag, you should upgrade (your version has security flaws). If you canβt upgrade, you may wish to do as rstunnel does, and give ssh a command to run, such as "sleep 99999999999" β
π OPTIONS π
-M port[:echo_port]β specifies the base monitoring port to use. Without the echo port, this port and the port immediately above it (port + 1) should be something nothing else is using.autosshwill send test data on the base monitoring port, and receive it back on the port above. For example, if you specify"-M 20000",autosshwill set up forwards so that it can send data on port20000and receive it back on20001.
Alternatively, a port for a remote echo service may be specified. This should be port 7 if you wish to use the standard inetd echo service. When an echo port is specified, only the specified monitor port is used, and it carries the monitor message in both directions.
Many people disable the echo service, or even disable inetd, so check that this service is available on the remote machine. Some operating systems allow one to specify that the service only listen on the localhost (loopback interface), which would suffice for this use.
The echo service may also be something more complicated: perhaps a daemon that monitors a group of ssh tunnels.
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.
-fβ causesautosshto drop to the background before runningssh. The-fflag is stripped from arguments passed tossh. Note that there is a crucial difference between-fwithautossh, and-fwithssh: when used withautossh,sshwill be unable to ask for passwords or passphrases. When-fis used, the"starting gate"time (seeAUTOSSH_GATETIME) is set to0.-Vβ causesautosshto display its version number and exit.
π ENVIRONMENT π
Other than the flag to set the connection monitoring port, autossh uses environment variables to control features. ssh seems to be still collecting letters for options, and this seems the easiest way to avoid collisions π
AUTOSSH_DEBUGβ If this variable is set, the logging level is set toLOG_DEBUG, and if the operating system supports it,syslogis set to duplicate log entries tostderr.AUTOSSH_FIRST_POLLβ Specifies the time to wait before the first connection test. Thereafter the general poll time is used (seeAUTOSSH_POLLbelow).AUTOSSH_GATETIMEβ Specifies how longsshmust be up before we consider it a successful connection. The default is 30 seconds. Note that ifAUTOSSH_GATETIMEis set to0, then not only is the gatetime behaviour turned off, butautosshalso ignores the first run failure ofssh. This may be useful when runningautosshat boot.AUTOSSH_LOGLEVELβ Specifies the log level, corresponding to the levels used bysyslog; so0-7with7being the chattiest.AUTOSSH_LOGFILEβ Specifies thatautosshshould use the named log file, rather thansyslog.AUTOSSH_MAXLIFETIMEβ Sets the maximum number of seconds that the program should run. Once the number of seconds has been passed, thesshchild will be killed and the program will exit.AUTOSSH_MAXSTARTβ Specifies how many timessshshould be started. A negative number means no limit on the number of timessshis started. The default value is-1.AUTOSSH_MESSAGEβ Append message to echo message sent when testing connections.AUTOSSH_NTSERVICEβ (Cygwin only.) When set to"yes",autosshsets up to run as an NT service undercygrunsrv. This adds the-Nflag forsshif not already set, sets the log output tostdout, and changes the behaviour onsshexit so that it will restart even on a normal exit.AUTOSSH_PATHβ Specifies the path to thesshexecutable, in case it is different than the path compiled in.AUTOSSH_PIDFILEβ Writeautosshpid to specified file.AUTOSSH_POLLβ Specifies the connection poll time in seconds; default is600seconds. UnlessAUTOSSH_FIRST_POLLis used, the first poll time will set to match the poll time. If the poll time is less than twice the network timeouts (default 15 seconds) the network timeouts will be adjusted downward to1/2the poll time.AUTOSSH_PORTβ Sets the connection monitoring port. Mostly in casesshappropriates-Mat some time. But because of this possible use,AUTOSSH_PORToverrides the-Mflag. A value of0turns the monitoring function off.
π ENVIRONMENT π (yes, again bestie, this one is OpenSSH-flavoured)
There are two particular OpenSSH options that are useful when using autossh : ExitOnForwardFailure=yes on the client side to make sure forwardings have succeeded when autossh assumes the connection is setup properly. ClientAliveInterval on the server side to make sure the listening socket is closed on the server side if the connection closes on the client side πΈ
π AUTHOR π
autossh was written by Carson Harding.
π¦ SEE ALSO π¦
ssh(1), ssh_config(5,), sshd_config(5,), ssh-add(1), ssh-agent(1), ssh-keygen(1), cygrunsrv(1).
xoxo, from the Debian manual page β Mar 18, 2018 πβ¨
π¦ where this page comes from
- alpine 3.24: autossh-doc 1.4g-r3 Β· MIT
- arch rolling: autossh 1.4g-4 Β· custom
- epel 9: autossh 1.4g-9.el9 Β· BSD
- fedora 43: autossh 1.4g-19.fc43 Β· LicenseRef-Callaway-BSD
- fedora 44: autossh 1.4g-21.fc44 Β· LicenseRef-Callaway-BSD
- nixos 26.05: autossh 1.4g
- nixpkgs unstable: autossh 1.4g
- Copyright (c) 2002-2018 Carson Harding. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms