Broken pipe 消息在 SSH 会话中意味着什么?

Pet*_*and 134 ssh

有时我的 SSH 会话会因一条Write failed: Broken pipe消息而断开连接。这是什么意思?我怎样才能让我的会话保持打开状态?

我知道screen,但这不是我要找的答案。我认为这是一个sshd配置选项。

Ger*_*ert 112

您的服务器可能会关闭闲置时间过长的连接。您可以更新客户端 ( ServerAliveInterval) 或服务器 ( ClientAliveInterval)

 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.  The
         default is 0, indicating that these messages will not be sent to
         the server.  This option applies to protocol version 2 only.

 ClientAliveInterval
         Sets a timeout interval in seconds after which if no data has
         been received from the client, sshd(8) will send a message
         through the encrypted channel to request a response from the
         client.  The default is 0, indicating that these messages will
         not be sent to the client.  This option applies to protocol
         version 2 only.
Run Code Online (Sandbox Code Playgroud)

更新您的服务器(并重新启动您的sshd

echo "ClientAliveInterval 60" | sudo tee -a /etc/ssh/sshd_config
Run Code Online (Sandbox Code Playgroud)

或客户端:

echo "ServerAliveInterval 60" >> ~/.ssh/config 
Run Code Online (Sandbox Code Playgroud)

  • @AwQiruiGuo 是的,您应该先创建目录 (`~/.ssh`)。所以`mkdir -p ~/.ssh; chmod 700 ~/.ssh; 触摸 ~/.ssh/config` (6认同)
  • 请注意,有时“*AliveInterval”还不够http://stackoverflow.com/questions/10665267/keep-ssh-session-alive-while-computer-sleep (2认同)
  • 我的 Mac 上没有“~/.ssh/config”,我必须在那里创建它还是在其他地方? (2认同)
  • 那么基本上通过修改客户端的配置就可以解决问题吗?如果我是某个服务器的管理员,并且我确实想关闭一些闲置 1 小时的 ssh 会话,该怎么办? (2认同)

Fra*_*sta 7

如果想要更长的连接周期,在客户端添加:

echo 'ServerAliveInterval 30' | tee -a ~/.ssh/config
echo 'ServerAliveCountMax 1200' | tee -a ~/.ssh/config
Run Code Online (Sandbox Code Playgroud)

ServerAliveCountMax默认情况下设置为 3。因此,一旦ServerAliveInterval将 3 小包信息发送到您的服务器,它将自动注销。将其设置为 1200 意味着此过程必须至少发生 1200 次。简而言之,您应该至少连接 30*1200 秒(10 小时)。

  • 为什么对属于当前用户或尚不存在的文件使用 sudo? (6认同)

Per*_*ids 6

另一种解决方案是使用mosh- 移动外壳。与 ssh 相比,它通过 UDP 连接并支持漫游。您可以在家开始您的会话,暂停您的笔记本电脑,将其带到工作/朋友/任何其他有互联网的地方,取消暂停您的笔记本电脑并继续工作,就好像什么也没发生一样。如果您的互联网连接状况不佳,它尤其有用:如果您的按键未到达服务器并不断尝试重新建立连接,它会显示即时反馈。

安装和设置很简单:它现在包含在所有当前的 Linux(加上一些非 Linux)发行版中,并且它通过先前的 ssh 连接协调会话初始化和身份验证。因此,如果您能够通过连接进行ssh user@server连接mosh user@server,那么如果两端都安装了 mosh 软件包,则您很可能仅通过调用就能够与 mosh 连接。

连接失败的主要原因是您必须通过 UDP 端口(默认范围:60000-61000)访问服务器才能使 mosh 工作。因此,如果服务器位于防火墙后面,如果您不能自己在其中打孔(安全隐患),那么您将很不走运。


小智 6

我花了很多时间摆弄 ServerAliveInterval 和 ClientAliveInterval 变量,但没有任何乐趣。

最后,与没有断管问题的机器进行比较后,我在底部找到了指令/etc/ssh/sshd_config

KeepAlive yes
Run Code Online (Sandbox Code Playgroud)

把它放在我的问题服务器的底部/etc/ssh/sshd_config就成功了。

已针对 Ubuntu 20.04.1 LTS、Ubuntu 16.04.7 LTS 进行验证。

  • 在我的 ssh 版本上,它是“TCPKeepAlive yes” - 谢谢! (2认同)