简体   繁体   English

由于不活动,Amazon EC2 ssh 超时

[英]Amazon EC2 ssh timeout due inactivity

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.我能够通过 SSH 向我的 EC2 实例发出命令,这些命令会记录我应该长时间关注的答案。 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.不好的是,由于我的不活动,SSH 命令在一段时间后关闭,我无法再看到我的实例发生了什么。

How can I disable/increase timeout in Amazon Linux machines?如何在 Amazon Linux 机器中禁用/增加超时?

The error looks like this:错误如下所示:

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

You can set a keep alive option in your ~/.ssh/config file on your computer's home dir:您可以在计算机主目录的 ~/.ssh/config 文件中设置保持活动选项:

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. Amazon AWS 通常会在仅 60 秒不活动后断开您的连接,因此此选项将每 50 秒 ping 服务器一次并让您无限期地保持连接。

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:假设您的 Amazon EC2 实例正在运行 Linux(并且很可能您使用的是 SSH-2,而不是 1),以下应该很容易工作:

  1. Remote into your EC2 instance.远程访问您的 EC2 实例。

     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.将“client-alive”指令添加到实例的 SSH 服务器配置文件中。

     echo 'ClientAliveInterval 60' | sudo tee --append /etc/ssh/sshd_config
  3. Restart or reload the SSH server, for it to recognize the configuration change.重新启动或重新加载 SSH 服务器,以便它识别配置更改。

    • The command for that on Ubuntu Linux would be..Ubuntu Linux上的命令是..

       sudo service ssh restart
    • On any other Linux , though, the following is probably correct..但是,在任何其他 Linux 上,以下内容可能是正确的..

       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.下次您通过 SSH 连接到该 EC2 实例时,那些超级烦人的频繁连接冻结/超时/断开应该会消失。

This is also helps with Google Compute Engine instances, which come with similarly annoying default settings.这对于 Google Compute Engine 实例也有帮助,它带有类似令人讨厌的默认设置。

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.警告:请注意,TCPKEEPALIVE设置(这也是存在的)是微妙的,但明显从ClientAlive设置的不同,我提出以上,并且从更改默认设置TCPKEEPALIVE实际上可能伤害你的情况,而不是帮助。

More info here: http://man.openbsd.org/?query=sshd_config更多信息在这里: http : //man.openbsd.org/?query=sshd_config

Consider using screen or byobu and the problem will likely go away.考虑使用screenbyobu ,问题可能会消失。 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 .此外,即使连接丢失,您也可以通过screen -rbyobu -r重新连接并恢复对之前相同终端屏幕的访问。

byobu is an enhancement for screen, and has a wonderful set of options, such as an estimate of EC2 costs. byobu 是 screen 的增强功能,并有一组很棒的选项,例如 EC2 成本估算。

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"我知道对于 Putty,您可以使用 keepalive 设置,因此它会每隔一段时间发送一些活动数据包,以免“空闲”或“过时”

http://the.earth.li/~sgtatham/putty/0.55/htmldoc/Chapter4.html#S4.13.4 http://the.earth.li/~sgtatham/putty/0.55/htmldoc/Chapter4.html#S4.13.4

If you are using other client let me know.如果您使用其他客户端,请告诉我。

You can use Mobaxterm, free tabbed SSH terminal with below settings-您可以使用 Mobaxterm,具有以下设置的免费选项卡式 SSH 终端 -

Settings -> Configuration -> SSH -> SSH keepalive

remember to restart Mobaxterm app after changing the setting.更改设置后记得重启 Mobaxterm 应用程序。

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.我有 10 多个自定义 AMI,全部基于 Amazon Linux AMI,而且我从未遇到过由于 SSH 连接不活动而导致的任何超时问题。 I've had connections stay open more than 24 hrs, without running a single command.我的连接保持打开状态超过 24 小时,而没有运行任何命令。 I don't think there are any timeouts built into the Amazon Linux AMIs.我认为 Amazon Linux AMI 中没有内置任何超时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM