Amazon EC2 ssh 由于不活动而超时
我可以通过 SSH 向我的 EC2 实例发出命令,这些命令会记录我应该长期观察的答案。不好的是,由于我不活动,SSH 命令在一段时间后关闭,我无法再看到我的实例发生了什么。
如何禁用/增加 Amazon Linux 计算机中的超时?
错误看起来像这样:
Read from remote host ec2-50-17-48-222.compute-1.amazonaws.com: Connection reset by peer
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以在客户端主目录上的
~/.ssh/config
文件中设置保持活动选项:Amazon AWS 通常会在不活动 60 秒后断开连接,因此此选项将每隔 60 秒 ping 一次服务器50 秒,让您无限期保持联系。
注意 - 这只能在客户端设置,不能在服务器端设置。我们为服务器端提供了
ClientAliveInterval
,可以进行类似的设置。You can set a keep alive option in your
~/.ssh/config
file on the client's home dir:Amazon AWS usually drops your connection after only 60 seconds of inactivity, so this option will ping the server every 50 seconds and keep you connected indefinitely.
Note - This is to be set client side only and not server side. We have
ClientAliveInterval
for server side which can be set similarly.假设您的 Amazon EC2 实例运行 Linux(并且很可能您使用的是 SSH-2,而不是 1),则以下操作应该非常方便:
远程访问您的 EC2 实例。
将“client-alive”指令添加到实例的 SSH 服务器配置文件中。
重新启动或重新加载 SSH 服务器,使其识别配置更改。
在 Ubuntu Linux 上执行此操作的命令是..
在任何其他 Linux 上,以下内容可能是正确的..
断开连接。
<前><代码>注销
下次您通过 SSH 连接到该 EC2 实例时,那些超级烦人的频繁连接冻结/超时/丢失应该会消失。
这对 Google Compute Engine 实例也有帮助,它带有类似令人讨厌的默认设置。
警告:请注意,TCPKeepAlive 设置(也存在)与 ClientAlive 有细微的差别,但又明显不同强> 我在上面建议的设置,并且从默认值更改 TCPKeepAlive 设置实际上可能会损害您的情况而不是有帮助。
此处的更多信息:http://man.openbsd.org/?query=sshd_config
Assuming your Amazon EC2 instance is running Linux (and the very likely case that you are using SSH-2, not 1), the following should work pretty handily:
Remote into your EC2 instance.
Add a "client-alive" directive to the instance's SSH-server configuration file.
Restart or reload the SSH server, for it to recognize the configuration change.
The command for that on Ubuntu Linux would be..
On any other Linux, though, the following is probably correct..
Disconnect.
The next time you SSH into that EC2 instance, those super-annoying frequent connection freezes/timeouts/drops should hopefully be gone.
This is also helps with Google Compute Engine instances, which come with similarly annoying default settings.
Warning: Do note that TCPKeepAlive settings (which also exist) are subtly, yet distinctly different from the ClientAlive settings that I propose above, and that changing TCPKeepAlive settings from the default may actually hurt your situation rather than help.
More info here: http://man.openbsd.org/?query=sshd_config
考虑使用
screen
或byobu
,问题可能会消失。此外,即使连接丢失,您也可以通过screen -r
或byobu -r
重新连接并恢复对之前相同终端屏幕的访问。byobu 是 screen 的增强功能,并且有一组很棒的选项,例如 EC2 成本的估算。
Consider using
screen
orbyobu
and the problem will likely go away. What's more, even if the connection is lost, you can reconnect and restore access to the same terminal screen you had before, viascreen -r
orbyobu -r
.byobu is an enhancement for screen, and has a wonderful set of options, such as an estimate of EC2 costs.
我知道对于 Putty,您可以利用 keepalive 设置,这样它就会经常发送一些活动数据包,以免变得“空闲”或“陈旧”
http://the.earth.li/~sgtatham/putty/0.55/htmldoc/Chapter4.html#S4.13.4
如果您使用其他客户端,请告诉我。
I know for Putty you can utilize a keepalive setting so it will send some activity packet every so often as to not go "idle" or "stale"
http://the.earth.li/~sgtatham/putty/0.55/htmldoc/Chapter4.html#S4.13.4
If you are using other client let me know.
要增加Linux(例如RedHat)中的SSH连接超时,您可以调整SSH服务器配置文件(sshd_config)中的配置设置。操作方法如下:
查找 ClientAliveInterval 和 ClientAliveCountMax 项。如果没有指定,您可以将它们添加到文件末尾。
当您保存配置时,您必须重新启动 ssh 或 sshd 服务(这取决于 Linux 的类型)
或
ClientAliveInterval:指定服务器在此之前等待的时间间隔(以秒为单位)向客户端发送空数据包以检查连接是否仍然有效。
ClientAliveCountMax:指定服务器在断开连接之前未收到客户端响应的情况下发送 keepalive 消息的次数。
TCPKeepAlive:它发送一个空的 TCP ACK 数据包
如果您使用 Putty,请检查是否选中此连接选项
To increase the SSH connection timeout in Linux (for example RedHat), you can adjust the configuration settings in the SSH server configuration file (sshd_config). Here's how you can do it:
Look for the ClientAliveInterval and ClientAliveCountMax items. If there is not specify you can add them at the end of the file.
When you save configuration you must restart ssh or sshd service (it depends type of Linux)
OR
ClientAliveInterval: Specifies the interval (in seconds) that the server will wait before sending a null packet to the client to check if the connection is still alive.
ClientAliveCountMax: Specifies the number of times the server will send a keepalive message without receiving a response from the client before disconnecting.
TCPKeepAlive: It sends an empty TCP ACK packet
If You use Putty please check if this connection options is checked
您可以使用 Mobaxterm,带有以下设置的免费选项卡式 SSH 终端 -
请记住在更改设置后重新启动 Mobaxterm 应用程序。
You can use Mobaxterm, free tabbed SSH terminal with below settings-
remember to restart Mobaxterm app after changing the setting.
我有 10 多个自定义 AMI,全部基于 Amazon Linux AMI,而且我从未遇到过由于 SSH 连接不活动而导致的任何超时问题。我的连接保持打开状态超过 24 小时,没有运行任何命令。我认为 Amazon Linux AMI 中没有内置任何超时功能。
I have a 10+ custom AMIs all based on Amazon Linux AMIs and I've never run into any timeout issues due to inactivity on a SSH connection. I've had connections stay open more than 24 hrs, without running a single command. I don't think there are any timeouts built into the Amazon Linux AMIs.