HostOnNet Blog

ssh tunnel with autossh

Looking for Linux Server Admin or WordPress Expert? We can help.

autossh is a program to start a copy of ssh and monitor it, restarting it as necessary should it die or stop passing traffic.

To install autossh on Ubuntu, run

sudo apt install autossh -y

To start SSH tunnel using autossh, run

autossh -M 20000 -f -N -p SSH_PORT USER@SERVER_IP -D 7070

Example

autossh -M 20000 -f -N -p 3333 [email protected] -D 7070

Once you run the command you will have a local sock5 proxy on 127.0.0.1 port 7070.

-p 3333 is the SSH port

-M 20000 is port used by autossh to monitor.

Monitoring

-M Specify Monitoring port. autossh with use Monitoring port and Monitoring Port + 1 and send a test data to this port, see if it can revive on other end. If not it restart ssh.

Disable Monitoring

You can disable monitoring by specifying -M 0, this will restart ssh if ssh client exit.

Starting AutoSSH on Boot in Ubuntu

Here is the ssh-tunnel script i use. I start this script on boot with Ubuntu > Unity Dash > Startup Applications

boby@hon-pc-01:~$ cat bin/ssh-tunnel 
#!/bin/bash

/usr/bin/killall autossh
#/usr/bin/autossh -M 20000 -f -N -p 3333 [email protected] -D 7070
/usr/bin/autossh -M 0 -o "ExitOnForwardFailure yes" -o "ServerAliveInterval 30" -o "ServerAliveCountMax 3" -f -N -p 3333 [email protected] -D 7070

boby@hon-pc-01:~$ 

Posted in Linux

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.