On most of Linux releases, the default is set to keep ssh connection alive forever. However, firewall setting in router could also impact the ssh connection, which mostly has connection idle timeout setting. It's quite annoying if your ssh connection get cut off for just idling for a short time. So you may want tweak your ssh configuration a bit to keep the ssh connection alive.

You can chose config either server or client side to keep ssh connection alive. Could be both, but not necessary. The ssh connection timeout calculation is based on one side configuration.

For quick example, see How to set ssh idle timeout

On server side, set the configuration:

Default value

$ grep -i alive /etc/ssh/sshd_config
#TCPKeepAlive yes
#ClientAliveInterval 0
#ClientAliveCountMax 3

The interval value indicates in seconds the server sends a alive message to client after no data has been received from client. Default is  0, not sending alive message at all.

While, CountMax indicates the max times the alive message should be sent.

Forexample:

ClientAliveInterval 15
ClientAliveCountMax 10

The ssh connection session will be terminated after 15*10=150 second

Note: not set the interval value higher than the firewall setting.

On client side

Similarly, you have the following setting in your ssh config file client side.

    Host *
       TcpKeepAlive yes
       ServerAliveInterval 0
ServerAliveCountMax 3

You can use the following 3 ways to specify the change

1. command line options(apply to session only)
2. user-specific file (~/.ssh/config, applies to the user only)
3. system-wide file (/etc/ssh/ssh_config)

For example, in command line, use

ssh -o ServerAliveInterval=60 ServerAliveCountMax=60 This email address is being protected from spambots. You need JavaScript enabled to view it. 
You session get idle timeout 60*60=3600 seconds.

Note: Command line options overwrite ~/.ssh/config setting, while in the same way, user setting will overwrite system-wide setting.

Detail Reference:

 TCPKeepAlive
             Specifies whether the system should send TCP keepalive messages
             to the other side.  If they are sent, death of the connection or
             crash of one of the machines will be properly noticed.  However,
             this means that connections will die if the route is down tem-
             porarily, and some people find it annoying.  On the other hand,
             if TCP keepalives are not sent, sessions may hang indefinitely on
             the server, leaving “ghost” users and consuming server resources.

             The default is “yes” (to send TCP keepalive messages), and the
             server will notice if the network goes down or the client host
             crashes.  This avoids infinitely hanging sessions.

             To disable TCP keepalive messages, the value should be set to
             “no”.
ClientAliveCountMax
             Sets the number of client alive messages (see below) which may be
             sent without sshd(8) receiving any messages back from the client.
             If this threshold is reached while client alive messages are
             being sent, sshd will disconnect the client, terminating the ses-
             sion.  It is important to note that the use of client alive mes-
             sages is very different from TCPKeepAlive (below).  The client
             alive messages are sent through the encrypted channel and there-
             fore will not be spoofable.  The TCP keepalive option enabled by
             TCPKeepAlive is spoofable.  The client alive mechanism is valu-
             able when the client or server depend on knowing when a connec-
             tion has become inactive.

             The default value is 3.  If ClientAliveInterval (see below) is
             set to 15, and ClientAliveCountMax is left at the default, unre-
             sponsive SSH clients will be disconnected after approximately 45
             seconds.  This option applies to protocol version 2 only.
ClientAliveInterval
             Sets a timeout interval in seconds after which if no data has
             been received from the client, sshd(8) will send a message
             through the encrypted channel to request a response from the
             client.  The default is 0, indicating that these messages will
             not be sent to the client.  This option applies to protocol ver-
             sion 2 only.
ServerAliveInterval
             Sets a timeout interval in seconds after which if no data has
             been received from the server, ssh(1) will send a message through
             the encrypted channel to request a response from the server.  The
             default is 0, indicating that these messages will not be sent to
             the server.  This option applies to protocol version 2 only.
ServerAliveCountMax
             Sets the number of server alive messages (see below) which may be
             sent without ssh(1) receiving any messages back from the server.
             If this threshold is reached while server alive messages are
             being sent, ssh will disconnect from the server, terminating the
             session.  It is important to note that the use of server alive
             messages is very different from TCPKeepAlive (below).  The server
             alive messages are sent through the encrypted channel and there-
             fore will not be spoofable.  The TCP keepalive option enabled by
             TCPKeepAlive is spoofable.  The server alive mechanism is valu-
             able when the client or server depend on knowing when a connec-
             tion has become inactive.

             The default value is 3.  If, for example, ServerAliveInterval
             (see below) is set to 15 and ServerAliveCountMax is left at the
             default, if the server becomes unresponsive, ssh will disconnect
             after approximately 45 seconds.  This option applies to protocol
             version 2 only.