Tran Nghi's Site  - Make notes and share experience

To keep ssh connection alive

This post is also available in: Vietnamese

By default, 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.. not sure!!!

You have 02 options: option #1. you can set a keep alive option in your ~/.ssh/config file on your computer’s home dir; or option #2, you can do it directly on your ssh server. I am not sure if the option #1 is working well, coz I am following the option #2.

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.


2) Add a “client-alive” directive to the instance’s SSH-server configuration file; adding below comment into /etc/ssh/sshd_config

ClientAliveInterval 60

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 | systemctl restart ssh.service

On any other Linux, though, the following is probably correct..

~$ sudo service sshd restart | systemctl restart sshd.service


~$ 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: