Posted on
Software

autossh: Automatically restart SSH sessions

Author
  • User
    Linux Bash
    Posts by this author
    Posts by this author

Keeping SSH Sessions Alive with Autossh

SSH (Secure Shell) is a protocol extensively used for secure remote operations in a range of professional and personal environments. Sometimes, maintaining a persistent SSH connection is crucial, especially when connectivity issues like network instability or random disconnections occur. That’s where autossh comes in handy. Autossh is a simple program designed to start, monitor, and restart SSH sessions automatically when they crash or are inadvertently stopped. This functionality makes autossh particularly beneficial for maintaining persistent tunnels or keeping a session active indefinitely.

In this blog post, we'll delve into why autossh is useful, how to install it across various Linux distributions, and give a brief overview of setting it up.

Why Autossh?

Here are a few scenarios where autossh can be incredibly useful:

  • Persistent File Syncing: Keeping a stable connection while syncing large files over SSH.

  • Remote Monitoring: Ensuring a continual connection to remote hosts for monitoring software or hardware.

  • Long-Running Work: Whether it's lengthy computations in research that require connections to remain active for computations and data retrieval.

Installing Autossh

Debian/Ubuntu Systems

For those using Debian-based systems like Ubuntu, installation is straightforward with apt. Open your terminal and enter:

sudo apt update
sudo apt install autossh

Fedora Systems

For users on Fedora or other systems that use dnf as the package manager:

sudo dnf update
sudo dnf install autossh

openSUSE Systems

For those on openSUSE, ‘zypper’ is the default package manager:

sudo zypper refresh
sudo zypper install autossh

Configuring Autossh

After installation, configuring autossh requires defining the parameters around which you wish to maintain your SSH connections:

  1. Basic Usage: The most straightforward use of autossh is to just keep a bare SSH connection. The command structure resembles regular SSH:

    autossh -M 0 [your_username]@[remote_host]
    

    Here, -M 0 tells autossh to not use a monitoring port but instead rely on SSH "ServerAliveInterval" options to keep the connection alive.

  2. Setting Up Port Forwarding: If you’re forwarding ports (perhaps for database connections or application access), configure it like so:

    autossh -M 0 -f -N -L 9090:localhost:5432 [your_username]@[remote_host]
    

    This setup forwards the local 9090 port to the remote host's 5432 port over SSH. The -f sends autossh to the background just before command execution, and -N tells SSH that no commands will be sent over the link.

  3. ServerAlive Options: For enhanced connection stability, you might want to add ServerAliveInterval and ServerAliveCountMax settings in your SSH configuration file:

    Host *
       ServerAliveInterval 60
       ServerAliveCountMax 2
    

    This setting ensures that SSH sends a message through the encrypted channel every 60 seconds, and if 2 consecutive messages go unacknowledged, SSH assumes the connection is dead and drops the session.

Helpful Tips

  • Monitor Port: While using the -M 0 option makes setup simpler, using a dedicated monitor port (by setting autossh without '-M 0') can sometimes help in more complex networking environments.

  • Connection Checks: Always verify firewall or security group settings if connections do not establish as expected.

Autossh provides a critical utility for maintaining ongoing operational tasks over SSH, reducing the potential for disruptions caused by unstable network conditions. By easily setting up and deploying autossh across various systems, administrators and developers can ensure their remote tasks run smoother and more resiliently.