Autossh is a program used to start and monitor ssh. You can restart ssh when necessary, for example, if the program crashes or the network encounters a problem. It is inspired by rstunnel (Reliable SSH Tunnel ). the autossh 1.2 method has changed: autossh uses ssh to construct an ssh redirect loop (Local to remote and remote to local), then sends test data and returns the result.
Autossh 1.3 adds a new method: You can specify the port of the remote echo service to return the test data sending result. This avoids conflicts between the handshake phase and all remote machine ports. The old loop-of-forwardings method is still available.
Install autossh on Ubuntu
Open the terminal window and run the following command
Sudo apt-get install autossh
Autossh syntax
Autossh [-V] [-M port [: echo_port] [-f] [SSH_OPTIONS]
Start the ssh channel at startup
We can use upstart to start the ssh channel in Ubuntu, just put the following autossh. conf file in the/etc/init directory.
# Autossh startup Script
Description "autossh daemon startup"
Start on net-device-up IFACE = eth0
Stop on runlevel [01S6]
Respawn
Respawn limit 5 60 # respawn max 5 times in 60 seconds
Script
Export AUTOSSH_PIDFILE =/var/run/autossh. pid
Export AUTOSSH_POLL = 60
Export AUTOSSH_FIRST_POLL = 30
Export autossh_1_time = 0
Export AUTOSSH_DEBUG = 1
Autossh-M 0-4-n user @ HOSTNAME-D 7070-o "ServerAliveInterval 60"-o "ServerAliveCountMax 3"-o BatchMode = yes-o StrictHostKeyChecking = no-I SSH_KEY_FILE_PATH
End script