89

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
hyper-neutrino
  • 5,272
  • 2
  • 29
  • 50
Roberto
  • 11,557
  • 16
  • 54
  • 68

6 Answers6

186

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.

Aravind Nair
  • 13
  • 2
  • 5
mauriciomdea
  • 2,954
  • 3
  • 15
  • 10
  • 10
    this setting is on the client side, as opposed to the AWS server, right? Would be worth mentioning in the answer. Also, are you sure about the 60 seconds? It certainly takes far longer than that for my connections to time out. – CupawnTae Sep 28 '14 at 12:54
  • 5
    Yes, it's on the client side, on your ssh config on your *nix computer. I'll update the answer, thanks! For me it's actually 120 seconds, but I have a coworker working in another region and it timeouts in 60 seconds for him. Don't ask me why, I don't work at Amazon! That's why I said 'usually' 60 seconds. I thought it prudent to recommend 50 seconds because 1. it will work for almost everyone and 2. it's not going to thrash your network connection, it's just a ping. You could put 10 seconds and it will still be light enough to not fall into some kind of server ban. – mauriciomdea Sep 29 '14 at 23:17
  • Thanks a lot. It was pain to reconnect all the time just after 1 minute of inactivity. It helped me. – Satish Patel Dec 11 '16 at 03:23
  • 4
    Perfect Answer! To expand, the file should be `chmod 644 .ssh/config` if its not already created by the system. – Tony-Caffe May 02 '17 at 16:09
  • 1
    This answer explains this further with two more useful settings: https://unix.stackexchange.com/questions/3026/what-do-options-serveraliveinterval-and-clientaliveinterval-in-sshd-config-d – Nawaz Feb 05 '21 at 20:39
  • Does this setting get propagated through an ssh proxy? – Chris Wolf Nov 29 '21 at 15:25
74

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

naki
  • 932
  • 6
  • 11
  • 1
    Or just: echo 'ClientAliveInterval 60' >> /etc/ssh/sshd_config Or uncomment #ClientAliveInterval – DimiDak Sep 30 '20 at 19:27
  • @DimiDak Have you actually tried the command that you suggest? Probably like yourself, I always just use nano to do such edits; but the normative recipe-styled format of answers on Stack Overflow called for instructions that are copy-pasteable, and thus I made the same mistake as you did in the untested, original copy-pasteable [ie., not using an interactive text editor app] version of my instructions. The old comments to this answer that seem to have vanished (??) explained this; and the revision history of this answer [https://stackoverflow.com/posts/24360827/revisions] does also, somewhat. – naki Sep 30 '20 at 20:18
  • What's wrong with --> echo 'ClientAliveInterval 60' >> /etc/ssh/sshd_config ? – DimiDak Sep 30 '20 at 20:27
  • On centos7 AMI the command to restart `sshd` is `systemctl reload sshd` – Chris Wolf Nov 29 '21 at 15:20
10

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.

Iterator
  • 20,250
  • 12
  • 75
  • 111
  • Is there a good tutorial for either of these? Neither appears to come preinstalled on the EC2 hosts, and from what I have read you're supposed to run them on the server. – John Salvatier Nov 13 '13 at 00:58
  • 4
    I prefer @brandnewcode's `ServerAliveInterval`, below. I have been using tmux, which is similar to screen. An unintended benefit of tmux is a clock on the status bar which updates every minute, keeping the connection open. tl;dr: `tmux` to start a session, and `tmux a` to re-attach. – dannyman Apr 02 '15 at 20:30
  • 8
    This should not be marked as the correct answer as it answers a different question. – Austin Richardson Mar 21 '16 at 22:48
  • One of the best solution is what mauriciomdea have given. No need to reconnect. Works like charm. – Satish Patel Dec 11 '16 at 03:20
  • To me that is not a solution to ssh losing it's connection. It is a workaround for it. Gnu screen is useful to keep logs available upon disconnect, but it doesn't keep the connection running. – Dolf Andringa Jul 13 '20 at 05:12
9

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.

0

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.

panky sharma
  • 2,029
  • 28
  • 45
  • Most perfect and acceptable answer and not sure why no one voted this answer. Thanks @Panky it is solving my problem with extreme easy way as you suggested. – prashant thakre Apr 24 '22 at 19:56
  • Team follow this approach only, no need to do any changes on EC2 instances. – prashant thakre Apr 24 '22 at 19:57
  • This suggestion did not work for me. My MobaXterm is version 21.3 build 4736 and I am connecting to AWS EC2 but after around 2 minutes it disconnects even though the keepalive checbox was selected and MobaXterm was restarted. – Ahmad Aug 30 '22 at 16:12
-12

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.

AlanZ
  • 135
  • 2
  • I think it's sufficient to say that more people are getting timed out than not and one possible reason why you're not getting timed-out is because you have a client setting for your ssh which pings the remote peer occasionally to keep the disconnect/reset from occurring on your connections. – Jim Dec 03 '15 at 18:58