Autossh is a utility that allows you to automatically reconnect and reestablish SSH sessions. It is particularly useful for maintaining an SSH connection over a potentially unstable or unreliable network connection, such as a VPN or a mobile network. Autossh can be used to keep a remote connection alive, allowing you to run long-running commands or keep a remote terminal open for an extended period of time. It can also be used to set up a reverse SSH tunnel, allowing you to securely access a network or device behind a firewall.
Prerequisites
- Client needs to have
autossh
andssh
installed. - Server needs to accept
ssh
connections.
How to install autossh
On a Debian-based system, such as Ubuntu, you can install autossh using the apt package manager:
sudo apt update
sudo apt install autossh
On a Red Hat-based system, such as CentOS, you can install autossh using the yum package manager:
sudo yum update
sudo yum install autossh
On macOS, you can install autossh using Homebrew:
brew update
brew install autossh
On Windows, you can install autossh using the Chocolatey package manager:
choco install autossh
Alternatively, you can also install autossh from source by downloading the latest release from the project's GitHub page (https://github.com/invisible-island/autossh) and compiling it yourself. This may be necessary if you are using an older version of a operating system that is not supported by the package managers above.
Remote port forwarding with autossh
To establish a tunnel with autossh, you can use the following syntax. This will expose local port 8080 as port 9000 on the server, also, two monitoring ports will be used: 2000 and 2001.
Once your SSH connection is open, you can confirm it's working by running the command below:
Local port forwarding with autossh
To create a tunnel for forwarding a specific port on the remote server to your local machine:
autossh -M 2000 -N -L 3000:localhost:80 user@remote_server
This command creates a tunnel that forwards port 80 on the remote server to port 3000 on your local machine. You can then access the remote server's port 80 by connecting to localhost:3000
on your local machine.
How to make the autossh tunnel start automatically and survive system reboots
Troubleshooting autossh issues
- Connection timeouts: If autossh is unable to establish a connection or the connection drops, you can also try adding the
-vvv
option to increase the verbosity of ssh's output, which may help you identify the cause of the connection issue. - Authentication issues: If autossh is unable to authenticate to the remote server, make sure you are using the correct username and password, or that your SSH key is properly configured.
- Port forwarding issues: If you are using autossh to set up a tunnel or forward ports, make sure the remote server is properly configured to accept the connection and that there are no firewalls or other security measures blocking the connection, and that the port you're trying to expose is not used by another application. You can use
lsof -i :port
command.
Helpful resources
- The official documentation for autossh is a good starting point for learning about the tool and its options. You can find it here: https://www.harding.motd.ca/autossh/
- The autossh man page is also a useful resource for learning about the various options and commands available with autossh. You can access it by running the
man autossh
command on your terminal. - The OpenSSH documentation includes information about SSH tunnels and port forwarding, which can be useful when working with autossh. You can find it here: https://www.ssh.com/academy/ssh/tunneling-example