autossh 让你的 ssh 不掉线

[摘要] ssh是OPS及DEV同学登录服务器进行维护,部署等的必备工具。我们总会遇到长时间连接ssh后会自动断开需要重连的情况,之前我们都是使用screen来保证ssh不断线,现在有了一个更加给力的工具--autossh,autossh 是在ssh的基础之上,增加了一个监控端口,防止ssh session 过期,并可以重新连接保证不掉线,这保证我们在ssh执行远程服务器上的运行时间长的脚本时不会出现意外失联的情况。

下载地址:
download autossh-1.4c.tgz

源码安装,一次编译,到处执行:

gunzip -c autossh-1.4c.tgz | tar xvf -
cd autossh-1.4c
./configure
make
copy binary to where you wish it, or "make install" will install it under /usr/local by default.
examine autossh.host for example wrapper script and options

脚本用法

# ./autossh 
usage: autossh [-V] [-M monitor_port[:echo_port]] [-f] [SSH_OPTIONS]

    -M specifies monitor port. May be overridden by environment
       variable AUTOSSH_PORT. 0 turns monitoring loop off.
       Alternatively, a port for an echo service on the remote
       machine may be specified. (Normally port 7.)
    -f run in background (autossh handles this, and does not
       pass it to ssh.)
    -V print autossh version and exit.

Environment variables are:
    AUTOSSH_GATETIME    - how long must an ssh session be established
                          before we decide it really was established
                          (in seconds). Default is 30 seconds; use of -f
                          flag sets this to 0.
    AUTOSSH_LOGFILE     - file to log to (default is to use the syslog
                          facility)
    AUTOSSH_LOGLEVEL    - level of log verbosity
    AUTOSSH_MAXLIFETIME - set the maximum time to live (seconds)
    AUTOSSH_MAXSTART    - max times to restart (default is no limit)
    AUTOSSH_MESSAGE     - message to append to echo string (max 64 bytes)
    AUTOSSH_PATH        - path to ssh if not default
    AUTOSSH_PIDFILE     - write pid to this file
    AUTOSSH_POLL        - how often to check the connection (seconds)
    AUTOSSH_FIRST_POLL  - time before first connection check (seconds)
    AUTOSSH_PORT        - port to use for monitor connection
    AUTOSSH_DEBUG       - turn logging to maximum verbosity and log to
                          stderr

下面是使用autossh登录服务器的列子:

./autossh -M port remote-ssh-server-IP       
如:              
./autossh -M 12345 sudops@172.32.5.239

创建ssh隧道的例子:

autossh -M 12345 -N -D -v localhost:7070 sudops@172.32.5.239 -p 22

会看到如下监听端口:其中7070为本地
tcp        0      0 127.0.0.1:7070            0.0.0.0:*     LISTEN      7466/ssh            
tcp        0      0 127.0.0.1:12345           0.0.0.0:*     LISTEN      7466/ssh            
tcp        0      0 127.0.0.1:12346           0.0.0.0:*     LISTEN      7465/autossh

能够看到autossh启动了一个echo service port 12345作为监控,另外还启动了一个port+1的端口12346作为接收端口,文档描述如下:

    When no echo service port is specified, this port and the port 
    immediately above it (port# + 1) should be something nothing 
    else is using. autossh will send test data on the base monitoring 
    port, and receive it back on the port above.
 

怎么样,有意思不,赶快体验下传说中的“高大上” autossh 吧。

u2

Related Posts

python cx-Oracle 驱动安装

安装oracle驱动 cx-Oracle驱动二进制版本下载地…

sqlalchemy.exc.TimeoutError: QueuePool limit of size 5 overflow 10 reached

Python3 + Flask + mysql5.7搭建的w…

You Missed

雇佣AI员工,花钱上班:开发者的新”职场”荒诞剧

  • u2
  • 3月 1, 2026
  • 29 views
雇佣AI员工,花钱上班:开发者的新”职场”荒诞剧

OpenClaw 完整使用指南:自托管 AI Agent 的架构与实战

  • u2
  • 2月 25, 2026
  • 123 views
OpenClaw 完整使用指南:自托管 AI Agent 的架构与实战

Anthropic 推出 Claude Code Security:AI 驱动的代码安全神器

  • u2
  • 2月 24, 2026
  • 71 views

来自 OWASP 的代码安全检查工具:Dependency-Check,为你消除安全隐患

  • u2
  • 10月 16, 2025
  • 46 views
来自 OWASP 的代码安全检查工具:Dependency-Check,为你消除安全隐患

技术宅如何躲开越来越垃圾的CSDN?

  • u2
  • 4月 11, 2024
  • 33 views
技术宅如何躲开越来越垃圾的CSDN?

mysql连接失败:The driver has not received any packets from the server

  • u2
  • 3月 26, 2024
  • 35 views
mysql连接失败:The driver has not received any packets from the server