Amazon EC2 ssh 由于不活动而超时

发布于 2024-12-01 09:54:27 字数 261 浏览 1 评论 0 原文

我可以通过 SSH 向我的 EC2 实例发出命令,这些命令会记录我应该长期观察的答案。不好的是,由于我不活动,SSH 命令在一段时间后关闭,我无法再看到我的实例发生了什么。

如何禁用/增加 Amazon Linux 计算机中的超时?

错误看起来像这样:

Read from remote host ec2-50-17-48-222.compute-1.amazonaws.com: Connection reset by peer

I am able to issue commands to my EC2 instances via SSH and these commands logs answers which I'm supposed to keep watching for a long time. The bad thing is that SSH command is closed after some time due to my inactivity and I'm no longer able to see what's going on with my instances.

How can I disable/increase timeout in Amazon Linux machines?

The error looks like this:

Read from remote host ec2-50-17-48-222.compute-1.amazonaws.com: Connection reset by peer

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

第七度阳光i 2024-12-08 09:54:27

您可以在客户端主目录上的 ~/.ssh/config 文件中设置保持活动选项:

ServerAliveInterval 50

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:

ServerAliveInterval 50

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.

郁金香雨 2024-12-08 09:54:27

假设您的 Amazon EC2 实例运行 Linux(并且很可能您使用的是 SSH-2,而不是 1),则以下操作应该非常方便:

  1. 远程访问您的 EC2 实例。

    ssh -i .pem ;
    
  2. 将“client-alive”指令添加到实例的 SSH 服务器配置文件中。

    echo 'ClientAliveInterval 60' | sudo tee --附加 /etc/ssh/sshd_config
    
  3. 重新启动或重新加载 SSH 服务器,使其识别配置更改。

    • Ubuntu Linux 上执行此操作的命令是..

      sudo 服务 ssh 重新启动
      
    • 任何其他 Linux 上,以下内容可能是正确的..

      sudo 服务 sshd 重新启动
      
  4. 断开连接。

    <前><代码>注销

下次您通过 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:

  1. Remote into your EC2 instance.

    ssh -i <YOUR_PRIVATE_KEY_FILE>.pem <INTERNET_ADDRESS_OF_YOUR_INSTANCE>
    
  2. Add a "client-alive" directive to the instance's SSH-server configuration file.

    echo 'ClientAliveInterval 60' | sudo tee --append /etc/ssh/sshd_config
    
  3. Restart or reload the SSH server, for it to recognize the configuration change.

    • The command for that on Ubuntu Linux would be..

      sudo service ssh restart
      
    • On any other Linux, though, the following is probably correct..

      sudo service sshd restart
      
  4. Disconnect.

    logout
    

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

难以启齿的温柔 2024-12-08 09:54:27

考虑使用 screenbyobu,问题可能会消失。此外,即使连接丢失,您也可以通过 screen -rbyobu -r 重新连接并恢复对之前相同终端屏幕的访问。

byobu 是 screen 的增强功能,并且有一组很棒的选项,例如 EC2 成本的估算。

Consider using screen or byobu 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, via screen -r or byobu -r.

byobu is an enhancement for screen, and has a wonderful set of options, such as an estimate of EC2 costs.

花开雨落又逢春i 2024-12-08 09:54:27

我知道对于 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.

倒数 2024-12-08 09:54:27

要增加Linux(例如RedHat)中的SSH连接超时,您可以调整SSH服务器配置文件(sshd_config)中的配置设置。操作方法如下:

sudo nano /etc/ssh/sshd_config

查找 ClientAliveInterval 和 ClientAliveCountMax 项。如果没有指定,您可以将它们添加到文件末尾。

ClientAliveInterval 10
ClientAliveCountMax 6
TCPKeepAlive yes

输入图片此处描述

当您保存配置时,您必须重新启动 ssh 或 sshd 服务(这取决于 Linux 的类型)

sudo service sshd restart

sudo service ssh restart

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:

sudo nano /etc/ssh/sshd_config

Look for the ClientAliveInterval and ClientAliveCountMax items. If there is not specify you can add them at the end of the file.

ClientAliveInterval 10
ClientAliveCountMax 6
TCPKeepAlive yes

enter image description here

When you save configuration you must restart ssh or sshd service (it depends type of Linux)

sudo service sshd restart

OR

sudo service ssh restart

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
enter image description here

浅唱々樱花落 2024-12-08 09:54:27

您可以使用 Mobaxterm,带有以下设置的免费选项卡式 SSH 终端 -

Settings -> Configuration -> SSH -> SSH keepalive

请记住在更改设置后重新启动 Mobaxterm 应用程序。

You can use Mobaxterm, free tabbed SSH terminal with below settings-

Settings -> Configuration -> SSH -> SSH keepalive

remember to restart Mobaxterm app after changing the setting.

守护在此方 2024-12-08 09:54:27

我有 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文