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驱动二进制版本下载地…

Read more

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

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

Read more

You Missed

担心Todesk,向日葵有漏洞和数据泄露?跟我来体验下完美开源替代工具:RustDesk!

  • u2
  • 3月 20, 2026
  • 44 views

具有自我进化能力的国产大模型:MiniMax M2.7

  • u2
  • 3月 19, 2026
  • 95 views
具有自我进化能力的国产大模型:MiniMax M2.7

当AI学会读心术:MindPower框架带来的震撼与思考

  • u2
  • 3月 16, 2026
  • 59 views

AI并没有简化软件工程:它只是让糟糕的工程更容易实现了

  • u2
  • 3月 14, 2026
  • 58 views

当ChatGPT遭遇QuitGPT!

  • u2
  • 3月 13, 2026
  • 67 views

AI安全护栏:保护还是束缚?一场不对称的战争

  • u2
  • 3月 11, 2026
  • 69 views