【问题标题】:How to fix issues with 'The SSH connection was unexpectedly closed by the remote end'?如何解决“远程端意外关闭 SSH 连接”的问题?
【发布时间】:2017-01-03 16:46:27
【问题描述】:

我的配置:

  • 专用服务器 (Ubuntu 16.04 LTS) 仅用于 Jenkins (2.7.1),
  • 超过 100 多个 Jenkins 作业,每个作业都向 AWS (Vagrantfile) 调用 vagrant 实例,
  • 每个作业(配置脚本)可能需要 1-2 小时才能运行,
  • 大部分服务器配置文件(如 SSH)都有默认系统配置。

当我同时运行多个 Jenkins 实例时,它们更有可能因以下错误而失败:

00:00:00.774 + vagrant up --no-provision --destroy-on-error --provider=aws
00:00:09.635 Bringing machine 'MT-aws' up with 'aws' provider...
...
00:01:16.498     MT-aws: Running: inline script
...
00:01:26.415 ==> MT-aws: + echo
00:01:26.415 ==> MT-aws: + sleep 20
00:01:26.427 The SSH connection was unexpectedly closed by the remote end. This
00:01:26.427 usually indicates that SSH within the guest machine was unable to
00:01:26.427 properly start up. Please boot the VM in GUI mode to check whether
00:01:26.427 it is booting properly.
00:01:26.625 Build step 'Execute shell' marked build as failure

事实:

  • 配置脚本在随机位置失败(失败前没有特定代码),
  • 服务器没有过载,并且有足够的空闲 RAM 和 Gbit 网络访问权限,
  • 并行运行的作业越多,失败的可能性就越大,
  • 单独重新运行同一个作业通常可以正常工作,
  • /etc/ssh/ssh_config 中的默认设置,对于 Jenkins,没有 ~/.ssh/config

如何解决上述 SSH 被意外关闭的问题?

我是否需要增加一些 SSH 超时设置或其他什么?

【问题讨论】:

    标签: linux amazon-web-services jenkins ssh vagrant


    【解决方案1】:

    打开您的/etc/ssh/sshd_config 文件:

    # vi /etc/ssh/sshd_config
    

    修改设置如下:

    ClientAliveInterval 30
    ClientAliveCountMax 5
    

    在哪里,

    ClientAliveInterval:以秒(30)为单位设置超时间隔,在此之后如果没有收到来自客户端的数据,sshd 将通过加密通道发送消息以请求来自客户端的响应。默认为 0,表示这些消息将 不会发送给客户端。此选项仅适用于协议版本 2。

    ClientAliveCountMax:设置客户端活动消息的数量 (5),这些消息可以在 sshd 未从客户端接收任何消息的情况下发送。如果在发送客户端活动消息时达到此阈值,sshd 将断开客户端连接,终止会话。

    关闭并保存文件,然后重启sshd,例如:

    # /etc/init.d/ssh restart
    

    或:

    # service sshd restart
    

    另一个选项是在客户端(您的工作站)的ssh_config 文件中启用ServerAliveInterval,例如

    # vi /etc/ssh/ssh_config
    

    然后按如下方式追加/修改值:

    ServerAliveInterval 30
    ServerAliveCountMax 5
    

    在哪里,

    ServerAliveInterval:以秒为单位设置超时间隔,如果在此之后没有从服务器接收到数据,ssh 将通过加密通道发送消息以请求服务器响应。

    在上面的示例中,ServerAliveInterval 设置为 15,ServerAliveCountMax 保留为 3,如果服务器无响应,ssh 将在大约 45 秒后断开连接。同样,此选项仅适用于协议版本 2。

    【讨论】:

      【解决方案2】:

      另一种方法,正如Chris Roberts 所建议的那样,是将 SSH keep_alive 行添加到 Vagrantfile,例如

      config.vm.ssh.keep_alive = true
      

      默认情况下,这将每 5 秒发送一次 SSH 保持活动数据包以保持连接活动。

      请参阅:config.ssh related settings 了解更多信息。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-06-20
        • 2013-10-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-02-07
        • 2013-01-25
        • 2021-01-25
        相关资源
        最近更新 更多