← retour aux snippets

autossh: maintenir un tunnel SSH persistant

Relancer automatiquement un tunnel SSH en cas de coupure, avec monitoring natif.

bash network #ssh#autossh#tunnel

objectif

Maintenir un tunnel stable sans scripts maison, utile pour des forwards ou des proxys dynamiques.

code minimal

AUTOSSH_GATETIME=0 AUTOSSH_POLL=30 autossh -M 0 -N -o ServerAliveInterval=30 -o ServerAliveCountMax=3 -L 127.0.0.1:8080:127.0.0.1:80 user@bastion.data.pm

utilisation

# socks5 dynamique persistant
AUTOSSH_GATETIME=0 autossh -M 0 -N -D 1080 user@bastion.data.pm

variante(s) utile(s)

# service systemd simple (ExecStart)
# ExecStart=/usr/bin/env AUTOSSH_GATETIME=0 AUTOSSH_POLL=30 autossh -M 0 -N -D 1080 user@bastion.data.pm

notes

  • -M 0 désactive le port de monitoring; on privilégie ServerAlive*.
  • AUTOSSH_GATETIME=0 force le redémarrage immédiat.