用 autossh 保持 SSH 隧道存活

连接断开时自动重启 SSH 端口转发。

LinuxSSH 远程连接网络隧道低风险
运行 autossh -M 0 -N -L 8080:localhost:80 user@host,维持本地转发。

01命令

命令
$ autossh -M 0 -N -L 8080:localhost:80 user@host

02作用说明

autossh 监控 ssh 进程并在连接失败时重启,对长期隧道至关重要。-M 0 关闭旧的监控端口,改用 ssh 自身的 ServerAlive 选项。

03风险

风险低风险
使用条件sudo apt install autossh
!注意: 常驻隧道会扩大攻击面;条件允许时用 command= 和 no-pty 限制密钥权限。

04撤销

✓无需撤销——该命令不会改变持久状态。

05来源

  • Debian package: autossh
    Debian · vendor-doc · 2026-09-20
    依据: Debian packages autossh, which monitors and restarts SSH sessions and tunnels.
  • autossh
    Official project · vendor-doc · 2026-09-20
    依据: Debian packages autossh, which monitors and restarts SSH sessions and tunnels.

最后验证: 2026-09-22 · 分类: 网络

06相关方案