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. 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
72951 次浏览

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.

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.

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.

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

You can set a keep alive option in your ~/.ssh/config file on your computer'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.

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.