summaryrefslogtreecommitdiff
path: root/topics/programming/autossh-for-keeping-ssh-tunnels.gmi
blob: a977232eb176d425d2d282f74ee1216b1fdddadb (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Using autossh to Keep SSH Tunnels Alive

## Tags
* keywords: ssh, autossh, tunnel, alive


## TL;DR

```
guix package -i autossh  # Install autossh with Guix
autossh -M 0 -o "ServerAliveInterval 60" -o "ServerAliveCountMax 5" -L 4000:127.0.0.1:3306 alexander@remoteserver.org
```

## Introduction

Autossh is a utility for automatically restarting SSH sessions and tunnels if they drop or become inactive. It's particularly useful for long-lived tunnels in unstable network environments.

See official docs:

=> https://www.harding.motd.ca/autossh/

## Installing autossh

Install autossh using Guix:

```
guix package -i autossh
```

Basic usage:

```
autossh [-V] [-M monitor_port[:echo_port]] [-f] [SSH_OPTIONS]
```

## Examples

### Keep a database tunnel alive with autossh

Forward a remote MySQL port to your local machine:

**Using plain SSH:**

```
ssh -L 5000:localhost:3306 alexander@remoteserver.org
```

**Using autossh:**

```
autossh -L 5000:localhost:3306 alexander@remoteserver.org
```

### Better option 

```
autossh -M 0 -o "ServerAliveInterval 30" -o "ServerAliveCountMax 3" -L 5000:localhost:3306 alexander@remoteserver.org
```

#### Option explanations:

- `ServerAliveInterval`: Seconds between sending keepalive packets to the server (default: 0).
- `ServerAliveCountMax`: Number of unanswered keepalive packets before SSH disconnects (default: 3).

You can also configure these options in your `~/.ssh/config` file to simplify command-line usage.