autossh — Resilient SSH Tunnels

If you like this project, consider supporting me on Buy Me a Coffee ☕️


tags:

autossh is a program to start an SSH session and monitor it, automatically restarting the SSH session if it drops. It is commonly used for maintaining persistent SSH tunnels (port forwarding) across unreliable networks. It wraps the standard ssh command and adds automatic reconnection logic.

📚 Official Docs / Официальная документация: autossh(1)

🔧 Basic Tunneling

Local Port Forwarding

autossh -M 0 -N -L 8080:127.0.0.1:80 <USER>@<HOST>  # Forward local 8080→remote 80 / Переслать локальный 8080→удалённый 80
autossh -M 0 -N -L 3306:localhost:3306 <USER>@<HOST>  # MySQL tunnel / MySQL туннель
autossh -M 0 -N -L 5432:localhost:5432 <USER>@<HOST>  # PostgreSQL tunnel / PostgreSQL туннель
autossh -M 0 -N -L 0.0.0.0:8080:localhost:80 <USER>@<HOST>  # Bind to all interfaces / Привязать ко всем интерфейсам

Remote Port Forwarding

autossh -M 0 -N -R 2222:127.0.0.1:22 <USER>@<HOST>  # Forward remote 2222→local 22 / Переслать удалённый 2222→локальный 22
autossh -M 0 -N -R 8080:localhost:80 <USER>@<HOST>  # Expose local web server / Выставить локальный веб сервер
autossh -M 0 -N -R 0.0.0.0:9000:localhost:9000 <USER>@<HOST>  # Bind to all remote interfaces / Привязать ко всем удалённым интерфейсам

Dynamic Port Forwarding (SOCKS)

autossh -M 0 -N -D 1080 <USER>@<HOST>          # SOCKS proxy on port 1080 / SOCKS прокси на порту 1080
autossh -M 0 -N -D 0.0.0.0:1080 <USER>@<HOST>  # SOCKS proxy on all interfaces / SOCKS прокси на всех интерфейсах

🔄 Persistent Tunnels

Monitoring Options

autossh -M 0 -N -L 8080:localhost:80 <USER>@<HOST>  # Disable monitoring port (recommended) / Отключить порт мониторинга (рекомендуется)
autossh -M 20000 -N -L 8080:localhost:80 <USER>@<HOST>  # Use monitoring port 20000 / Использовать порт мониторинга 20000

ServerAliveInterval

autossh -M 0 -N -o "ServerAliveInterval=30" -o "ServerAliveCountMax=3" -L 8080:localhost:80 <USER>@<HOST>
# Check every 30s, fail after 3 attempts

ExitOnForwardFailure

autossh -M 0 -N -o "ExitOnForwardFailure=yes" -L 8080:localhost:80 <USER>@<HOST>
# Exit if port forwarding fails

📊 Monitoring & Debugging

Verbose Mode

autossh -M 0 -N -v -L 8080:localhost:80 <USER>@<HOST>  # Verbose / Подробный
autossh -M 0 -N -vv -L 8080:localhost:80 <USER>@<HOST>  # Very verbose / Очень подробный

Environment Variables

export AUTOSSH_DEBUG=1                        # Enable debug / Включить отладку
export AUTOSSH_LOGFILE=/var/log/autossh.log   # Log to file / Логировать в файл
export AUTOSSH_POLL=60                        # Poll interval / Интервал опроса
export AUTOSSH_GATETIME=0                     # No wait before first connection / Не ждать перед первым соединением

Check Connection

ps aux | grep autossh                         # Check if running / Проверить работает ли
netstat -tlnp | grep 8080                     # Check port / Проверить порт
ss -tlnp | grep 8080                          # Alternative / Альтернатива

🔧 Systemd Integration

Create Systemd Service

# /etc/systemd/system/autossh-tunnel.service
[Unit]
Description=AutoSSH Tunnel
After=network.target

[Service]
Type=simple
User=<USER>
Environment="AUTOSSH_GATETIME=0"
ExecStart=/usr/bin/autossh -M 0 -N -o "ServerAliveInterval=30" -o "ServerAliveCountMax=3" -o "ExitOnForwardFailure=yes" -L 8080:localhost:80 <USER>@<HOST>
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target

Manage Service

sudo systemctl daemon-reload                  # Reload systemd / Перезагрузить systemd
sudo systemctl start autossh-tunnel           # Start service / Запустить сервис
sudo systemctl enable autossh-tunnel          # Enable on boot / Включить при загрузке
sudo systemctl status autossh-tunnel          # Check status / Проверить статус
sudo journalctl -u autossh-tunnel -f          # Follow logs / Следовать за логами

🌟 Real-World Examples

Database Access

# MySQL tunnel / MySQL
autossh -M 0 -N -L 3306:127.0.0.1:3306 <USER>@<DB_SERVER>
# Connect: mysql -h 127.0.0.1 -P 3306

# PostgreSQL tunnel / PostgreSQL
autossh -M 0 -N -L 5432:localhost:5432 <USER>@<DB_SERVER>
# Connect: psql -h 127.0.0.1 -p 5432

# MongoDB tunnel / MongoDB
autossh -M 0 -N -L 27017:localhost:27017 <USER>@<DB_SERVER>
# Connect: mongosh --host 127.0.0.1 --port 27017

Reverse Tunnel for Remote Access

# From local machine
autossh -M 0 -N -R 2222:localhost:22 <USER>@<JUMP_HOST>

# From jump host
ssh -p 2222 <LOCAL_USER>@localhost

SOCKS Proxy for Browsing / SOCKS

# Start SOCKS proxy
autossh -M 0 -N -D 1080 <USER>@<PROXY_HOST>

# Configure browser
# SOCKS5: localhost:1080

# Or use with curl
curl --socks5 localhost:1080 https://api.example.com

Multi-Hop Tunnel

# Via bastion host
autossh -M 0 -N -L 8080:internal-server:80 -J <USER>@<BASTION> <USER>@<INTERNAL>

# Alternative with ProxyJump
autossh -M 0 -N -o "ProxyJump=<USER>@<BASTION>" -L 8080:localhost:80 <USER>@<INTERNAL>

Docker API Access

# Tunnel Docker socket
autossh -M 0 -N -L 2375:localhost:2375 <USER>@<DOCKER_HOST>

# Use Docker
export DOCKER_HOST=tcp://localhost:2375
docker ps

Kubernetes API Access

# Tunnel K8s API
autossh -M 0 -N -L 6443:localhost:6443 <USER>@<K8S_MASTER>

# Use kubectl
kubectl --server=https://localhost:6443 get pods

Web Development Preview

# Expose local dev server
autossh -M 0 -N -R 8080:localhost:3000 <USER>@<PUBLIC_SERVER>

# Access from: http://<PUBLIC_SERVER>:8080

VNC Tunnel / VNC

# Tunnel VNC
autossh -M 0 -N -L 5900:localhost:5900 <USER>@<VNC_SERVER>

# Connect with VNC client
vncviewer localhost:5900

Redis Tunnel / Redis

# Tunnel Redis
autossh -M 0 -N -L 6379:localhost:6379 <USER>@<REDIS_SERVER>

# Connect
redis-cli -h 127.0.0.1 -p 6379

Multiple Tunnels in One Connection

autossh -M 0 -N \
  -L 3306:localhost:3306 \
  -L 5432:localhost:5432 \
  -L 6379:localhost:6379 \
  <USER>@<SERVER>

SSH Config Integration

# ~/.ssh/config
Host tunnel
  HostName <HOST>
  User <USER>
  LocalForward 8080 localhost:80
  ServerAliveInterval 30
  ServerAliveCountMax 3
  ExitOnForwardFailure yes

# Use with autossh
autossh -M 0 -N tunnel

💡 Best Practices

🔧 SSH Config Options

Option Description (EN / RU)
ServerAliveInterval Keepalive interval / Интервал keepalive
ServerAliveCountMax Max failed keepalives / Макс неудачных keepalive
ExitOnForwardFailure Exit if forwarding fails / Выйти если переадресация не удалась
LocalForward Local port forward / Локальная переадресация
RemoteForward Remote port forward / Удалённая переадресация
DynamicForward SOCKS proxy / SOCKS прокси

📋 Common Use Cases

Use Case Flag
Database access -L 3306:localhost:3306
Web preview -R 8080:localhost:3000
SOCKS proxy -D 1080
Reverse shell -R 2222:localhost:22
VNC access -L 5900:localhost:5900

⚠️ Security Notes

On this page

linux autossh — Resilient SSH Tunnels 🔧 Basic Tunneling Local Port Forwarding Remote Port Forwarding Dynamic Port Forwarding (SOCKS) 🔄 Persistent Tunnels Monitoring Options ServerAliveInterval ExitOnForwardFailure 📊 Monitoring &amp; Debugging Verbose Mode Environment Variables Check Connection 🔧 Systemd Integration Create Systemd Service Manage Service 🌟 Real-World Examples Database Access Reverse Tunnel for Remote Access SOCKS Proxy for Browsing / SOCKS Multi-Hop Tunnel Docker API Access Kubernetes API Access Web Development Preview VNC Tunnel / VNC Redis Tunnel / Redis Multiple Tunnels in One Connection SSH Config Integration 💡 Best Practices 🔧 SSH Config Options 📋 Common Use Cases ⚠️ Security Notes 📚 Documentation Links