If you're connecting to a server via SSH over a larger network distance, or via certain firewalls, your connection can suddenly say "connection timed out", but a reconnect will work again right away. So it is not a server issue. But why did it abort in the first place?

The problem is that SSH is very sensitiive to lost packets, or TCP connection timeouts. 

This can happen if one of your packets is routed via a different path over a WAN or the internet, and it can also happen if there is a heavyweight firewall in the middle of your connection. I.e. a Check Point firewall could be configured to drop "inactive" TCP sessions after a while.

There is a safe fix for this behaviour that as always worked for me.

You have to enable "ClientAliveInterval" on the SSH server system. By default this setting is set to 0 which means it will not send periodic heartbeats over the SSH connection. Once you enable these, the sessions will no longer timeout. 

  • On your SSH server, find the ssh daemon configuration file. 
  • On Linux this will be /etc/ssh/sshd_config 
  • On BSD this will be either /etc/ssh/sshd_config or /usr/local/etc/ssh/sshd_config
  • On UNIX it might be called /etc/opt/ssh/sshd_config.

In the file, look for "ClientAliveInterval" - if it's missing your distro's SSH packagers left it out, but the option will work anyway since it is part of SSH.

Set it to a value like 60 to have a heartbeat sent every 60 seconds

ClientAliveInterval 60

Then restart your SSH Daemon using

service sshd restart  # On RedHat

or

stop ssh && start ssh # on Ubuntu

or 

/etc/rc.d/ssh restart # on FreeBSD

After this, ALWAYS try connecting in a second SSH session to make sure the restart was successful and SSH is still working.