我在工作场所的计算机上设置了一个 ssh 服务器,这样我就可以远程工作,而且它大部分工作得很好。
但是有时(大约每隔一周一次),ssh 连接在我使用它时会突然停止。发生这种情况时,服务器将永远不会再次接受任何 ssh 连接,直到我手动重新启动它。
编辑:更准确地说,它不会“拒绝”连接,它只会永远卡住
我确信对于这种情况必须有一些解决方法,所以每次发生这种情况时,我都不必亲自站在我的电脑前。你对此有什么建议吗?
这是 ssh 连接失败时的详细输出:
OpenSSH_7.2p2 Ubuntu-4ubuntu2.10, OpenSSL 1.0.2g 1 Mar 2016
debug1: Reading configuration data /home/hedi/.ssh/config
debug1: /home/hedi/.ssh/config line 6: Applying options for inubushiPC
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to 133.11.35.15 [133.11.35.15] port 22.
debug1: Connection established.
debug1: identity file /home/hedi/.ssh/id_rsa type 1
debug1: key_load_public: No such file or directory
debug1: identity file /home/hedi/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/hedi/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/hedi/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/hedi/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/hedi/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/hedi/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/hedi/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.2p2 Ubuntu-4ubuntu2.10
症状与 Linux VPS 内存不足并关闭交换时非常相似。通常是由于某些进程消耗了太多内存,以至于系统无法提供更多内存,但进程并没有崩溃,只是卡住了。任何新的 SSH 连接(甚至其他 TCP 连接)都需要一些内存来初始化,但内存不足,而且初始化永远不会完成(虽然我不知道其中的深层原因)。
当您的机器发生这种情况时,您可以通过一些监控解决方案来确认实际情况。最简单的方法可能是atop,它可以定期记录机器的状态(procs 的 CPU/RAM 使用...)并将其写入文件。您可以在下次重新启动后查阅此文件,以确保原因是内存不足,以及哪些进程使用了过多的内存。缺点通常是只能访问机器上的日志,即只能在机器出现故障并(手动)重新启动后才能对其进行分析。您可以将它与一些将日志发送到其他机器的日志记录解决方案结合使用,但这涉及更多。
实际防止此类故障的解决方案不太明确:
在确定导致内存不足的进程后,您可以限制其资源使用,使其不会再次消耗所有内存。这样做的细节可能取决于实际的过程/应用程序,并且值得在另一个问题上进行扩展(尽管您可能会找到很多通用指南)。
打开交换(也有很多指南)是另一种选择,尽管我无法确定它的有效性。
另一种方法,也是最后的手段,是https://stromberg.dnsalias.org/~strombrg/fallback-reboot/,据称该实用程序允许您在找不到其他方法访问它时远程重新启动机器。免责声明:我以前从未真正使用过它,而且我与该实用程序没有任何关系,只是听说过一些关于它的好消息。使用风险自负!