How to use the command autossh (with examples)

How to use the command autossh (with examples)

Autossh is a command-line tool used to run, monitor, and restart SSH connections. It is specially designed to maintain persistent tunnels for port forwarding. Autossh can be used with all the SSH flags while providing additional functionalities to automatically reconnect and keep the SSH connection alive. It is a handy tool for ensuring stable and uninterrupted SSH connections.

Use case 1: Start an SSH session, restarting when a monitoring port fails to return data

Code:

autossh -M monitor_port "ssh_command"

Motivation: This use case is helpful when you need to establish an SSH session and ensure its continuous operation. By monitoring a specific port for data, Autossh can detect when the connection fails and automatically restart it without manual intervention.

Explanation:

  • -M monitor_port: Specifies the monitoring port to check for data. If no data is received on this port, Autossh will restart the SSH session.
  • "ssh_command": The SSH command to be executed when the connection is established.

Example output:

autossh started
SSH session established
Monitoring port 1234 for data
Data received on port 1234
Continuing SSH session

Use case 2: Forward a local port to a remote one, restarting when necessary

Code:

autossh -M monitor_port -L local_port:localhost:remote_port user@host

Motivation: This use case is useful when you want to establish a local-to-remote port forwarding tunnel using SSH. Autossh ensures that the tunnel remains active by monitoring the specified port and restarting the SSH connection if needed. It enables seamless communication between local and remote ports.

Explanation:

  • -M monitor_port: Specifies the monitoring port to check for data. If no data is received on this port, Autossh will restart the SSH session.
  • -L local_port:localhost:remote_port: Specifies the local port, remote address, and remote port for port forwarding.
  • user@host: The username and hostname of the remote server.

Example output:

autossh started
SSH session established
Monitoring port 1234 for data
Data received on port 1234
Port forwarding established: local_port -> localhost:remote_port

Use case 3: Fork autossh into the background before executing ssh and don’t open a remote shell

Code:

autossh -f -M monitor_port -N "ssh_command"

Motivation: This use case is helpful when you want to run Autossh as a background process, without attaching a remote command or shell. It allows you to establish the SSH connection and keep it alive in the background, without interactive access to the remote server.

Explanation:

  • -f: Forks Autossh into the background.
  • -M monitor_port: Specifies the monitoring port to check for data. If no data is received on this port, Autossh will restart the SSH session.
  • -N: Instructs SSH not to execute any remote command after login.
  • "ssh_command": The SSH command to be executed when the connection is established.

Example output:

autossh started in the background
SSH session established
Monitoring port 1234 for data
Data received on port 1234
SSH connection active in the background without executing any remote command

Use case 4: Run in the background, with no monitoring port, and send SSH keep-alive packets every 10 seconds to detect failure

Code:

autossh -f -M 0 -N -o "ServerAliveInterval 10" -o "ServerAliveCountMax 3" "ssh_command"

Motivation: This use case is suitable when you want to run Autossh in the background without monitoring a specific port. Instead, it sends SSH keep-alive packets to detect connection failures. By configuring the interval and count of keep-alive packets, you can fine-tune the monitoring mechanism.

Explanation:

  • -f: Forks Autossh into the background.
  • -M 0: Disables monitoring of a specific port.
  • -N: Instructs SSH not to execute any remote command after login.
  • -o "ServerAliveInterval 10": Sets the interval at which keep-alive packets should be sent (10 seconds in this example).
  • -o "ServerAliveCountMax 3": Specifies the maximum number of consecutive unanswered keep-alive packets before considering the connection failed.
  • "ssh_command": The SSH command to be executed when the connection is established.

Example output:

autossh started in the background
SSH session established
Sending keep-alive packet 1
Sending keep-alive packet 2
Sending keep-alive packet 3
No response from remote server after 3 attempts, restarting SSH connection

Use case 5: Run in the background, with no monitoring port and no remote shell, exiting if the port forward fails

Code:

autossh -f -M 0 -N -o "ServerAliveInterval 10" -o "ServerAliveCountMax 3" -o ExitOnForwardFailure=yes -L local_port:localhost:remote_port user@host

Motivation: This use case is useful when you want to ensure that the port forwarding tunnel is established successfully. Autossh runs in the background without monitoring a specific port and exits if the port forward fails. It provides a reliable way to establish port forwarding and alerts you if any issues occur during the connection.

Explanation:

  • -f: Forks Autossh into the background.
  • -M 0: Disables monitoring of a specific port.
  • -N: Instructs SSH not to execute any remote command after login.
  • -o "ServerAliveInterval 10": Sets the interval at which keep-alive packets should be sent (10 seconds in this example).
  • -o "ServerAliveCountMax 3": Specifies the maximum number of consecutive unanswered keep-alive packets before considering the connection failed.
  • -o ExitOnForwardFailure=yes: Instructs SSH to exit if the port forwarding fails.
  • -L local_port:localhost:remote_port: Specifies the local port, remote address, and remote port for port forwarding.
  • user@host: The username and hostname of the remote server.

Example output:

autossh started in the background
SSH session established
Port forwarding failed, exiting Autossh

Use case 6: Run in the background, logging autossh debug output and ssh verbose output to files

Code:

AUTOSSH_DEBUG=1 AUTOSSH_LOGFILE=path/to/autossh_log_file.log autossh -f -M monitor_port -v -E path/to/ssh_log_file.log ssh_command

Motivation: This use case is useful when you need to troubleshoot SSH connections and analyze the debug information. By enabling debug output for Autossh and verbose output for SSH, you can log all the relevant information to specific files. It provides valuable insights into the connection and helps in diagnosing any issues.

Explanation:

  • AUTOSSH_DEBUG=1: Sets the environment variable to enable debug output from Autossh.
  • AUTOSSH_LOGFILE=path/to/autossh_log_file.log: Specifies the log file path for Autossh debug output.
  • -f: Forks Autossh into the background.
  • -M monitor_port: Specifies the monitoring port to check for data. If no data is received on this port, Autossh will restart the SSH session.
  • -v: Enables verbose output for SSH.
  • -E path/to/ssh_log_file.log: Specifies the log file path for SSH verbose output.
  • "ssh_command": The SSH command to be executed when the connection is established.

Example output:

autossh started in the background
SSH session established
Debug information logged to autossh_log_file.log
Verbose output logged to ssh_log_file.log

Conclusion:

In this article, we explored various use cases of the Autossh command. Autossh is a versatile tool that enhances SSH connections by providing automatic monitoring, restarting, and persistence of tunnels. These examples demonstrate different ways to utilize Autossh based on specific requirements, ensuring stable and reliable SSH connections.

Related Posts

How to use the command ppmtoeyuv (with examples)

How to use the command ppmtoeyuv (with examples)

The ppmtoeyuv command is used to convert a PPM image to a Berkeley YUV file.

Read More
How to use the command 'mkswap' (with examples)

How to use the command 'mkswap' (with examples)

The ‘mkswap’ command is used to set up a Linux swap area on a device or in a file.

Read More
How to use the command 'toolbox init-container' (with examples)

How to use the command 'toolbox init-container' (with examples)

This article provides examples of how to use the ’toolbox init-container’ command.

Read More