Home 关于ssh超时自动断开连接的问题
Post
Cancel

关于ssh超时自动断开连接的问题

服务器ssh连接,自动关闭,伴随的错误日志。

1
client_loop: send disconnect: Broken pipe

需要不停重连,非常麻烦。

本质是ssh中有支持这样的功能。可以在服务器端或客户端进行设置。

服务器端

/etc/ssh/sshd_config文件中,如设置

1
2
ClientAliveInterval 60
ClientAliveCountMax 3

表示服务器每60秒和client检测一次通信,若client不存在,就断开连接。 MaxCount是允许的client未响应次数,两个设置一同生效,就是要client超时60*3=180秒, 服务器就会主动断开连接。

ClientAliveInterval默认为0,不发送。

客户端

客户端也有相对应的选项 ServerAliveIntervalServerAliveCountMax, 一般配置在 ~/.ssh/config中。

ServerAliveInterval : Sets a timeout interval in seconds after which if no data has been received from the server, ssh(1) will send a message through the encrypted channel to request a response from the server.

ServerAliveCountMax : Sets the number of server alive messages (see below) which may be sent without ssh(1) receiving any messages back from the server.
If this threshold is reached while server alive messages are being sent, ssh will disconnect from the server, terminating the session. The default value is 3.

参考示例:

1
2
3
4
5
6
7
8
Host mydebian
  HostName    (这里为服务器IP地址)
  Port        (这里为服务器ssh端口)
  User        (登录用户名)
  IdentityFile  (path/to/private/key/file)
  ServerAliveCountMax 3          
  ServerAliveInterval 60

对所有设置生效,直接编辑 /etc/ssh/ssh_config 文件:

1
2
3
Host *         
    ServerAliveCountMax 3
    ServerAliveInterval 60
This post is licensed under CC BY 4.0 by the author.