自动重连的 SSH 隧道工具
autossh 监控 SSH 连接并在断开时重启,常用于反向隧道、端口转发和跳板。它本身不改变 SSH 认证,只是加了一层守护。
autossh -M 0 -N -L 8080:127.0.0.1:80 user@host
autossh -M 0 -N -R 9000:127.0.0.1:3000 user@host
autossh -M 0 -o 'ServerAliveInterval 30' -o 'ServerAliveCountMax 3' -N -L 5432:db:5432 user@host
-M 0 关闭监控端口,依赖 ServerAliveInterval/ServerAliveCountMax 检测断线。ss -lntp。-o ExitOnForwardFailure=yes,避免端口绑定失败后仍保持“假连接”。Restart=always;不要只靠 -f 后台化。GatewayPorts/防火墙配合;否则只监听 127.0.0.1。