Today, many IT workers need to connect to servers from far away. They use SSH tunnels to do this securely. But sometimes, the network isn’t stable, and the connection can drop. This is where Autossh can help. Autossh makes sure your SSH connection stays up, even if the network goes down for a bit.
For example, imagine you are working from home and need to connect to a server at your office. If the internet at your home has problems, the SSH connection can break. Autossh will automatically reconnect you if this happens.
In this article, we will show you step-by-step how to install and set up Autossh on a CentOS computer. This will help you keep your SSH connections stable and reliable.
Step 1: Install Autossh
Autossh packages are available under default repositories on various popular Linux systems like Redhat, Ubuntu and Debian. To install it, log in to your system and install package using default package mananger.
- RHEL-based systems
sudo yum install autossh
- Debian-based systems
sudo apt install autossh
Step 2: Create a SSH Tunnel
Before using the Autossh, we need to create an SSH tunnel to a remote server that we want to connect to. In this example, we will create a tunnel that forwards local port 80 to port 8000 on the remote server. This will allow us to access application running on server on port 8000 directly by accessing localhost in browser.
To create the tunnel, open a terminal and run the following command:
ssh -L 80:localhost:8000 remote-user@remote-server
This command will establish an SSH connection to the remote server and create a tunnel that forwards traffic from port 80 on your local machine to port 8000 on the remote server.
Once the SSH tunnel is established, you can test it by opening a web browser and navigating to http://localhost. If everything is set up correctly, you should see the default web page for the remote server.
Step 3: Use Autossh to Manage the SSH Tunnel
Now that we have an SSH tunnel active and working, we can use Autossh to automatically restart the tunnel if it is disconnected or interrupted by any cuase.
To use Autossh, we need to modify the SSH command used to create the tunnel. Here’s an example:
autossh -M 0 -o "ServerAliveInterval 30" -o "ServerAliveCountMax 3" -N -f -L 80:localhost:8000 remote-user@remote-server
This command is like the SSH command we used to make the tunnel before, but it has extra options to let Autossh manage the tunnel.
- The
-M 0
option tells Autossh to use a built-in port to check if the SSH tunnel is still connected. - The
-o "ServerAliveInterval 30"
and-o "ServerAliveCountMax 3"
options tell Autossh to send a signal every 30 seconds to keep the connection alive. If three signals in a row fail, Autossh will try to reconnect. - The
-N -f
options tell SSH to create the tunnel in the background without running any remote commands.
Step 5: Verify Autossh
To test Autossh, disconnect your internet or stop the SSH tunnel manually by pressing CTRL + C. Autossh should see that the tunnel is disconnected and restart it automatically.
You can check if the tunnel restarted by going to http://localhost in your web browser.
Step 6: Automate Autossh
To ensure that Autossh starts automatically when the server boots up, we need to create a systemd service.
Create a new file called autossh.service in the /etc/systemd/system directory using the following command:
sudo nano /etc/systemd/system/autossh.service
Add the following contents to the file:
[Unit]
Description=Autossh Service
After=network.target
[Service]
User=
ExecStart=/usr/bin/autossh -M 0 -o "ServerAliveInterval 30" -o "ServerAliveCountMax 3" -N -f -L 8080:localhost:80 remote-user@remote-server
Restart=always
RestartSec=10s
[Install]
WantedBy=multi-user.target
Replace `
Save and close the file.
Next, reload systemd and start the Autossh service with the following commands:
sudo systemctl daemon-reload
sudo systemctl start autossh.service
Also enable the Autossh service to start automatically on system boot:
sudo systemctl enable autossh.service
Conclusion
Autossh is a great tool for keeping SSH connections active, especially when dealing with unstable networks. With Autossh, you don’t have to worry about losing your SSH connection when the network drops. By following the steps in this article, you can install and set up Autossh on your Linux server. This will help you make sure that your SSH tunnels stay up and running, even if the network goes down. Autossh will automatically reconnect, keeping your work uninterrupted.