How can I keep my SSH sessions from freezing

Especially with home connections ssh connections get freezes after few minutes just for an idle timeout.
In order to avoid this is possible to setup a keep alive mechanism. To get your configuration working, make these configuration changes on the client:

Host *
    ServerAliveInterval 100
    ServerAliveCountMax 2

ServerAliveInterval: Sets a timeout interval in seconds after which if no data has been received from the server, ssh 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, or other value. This option applies to protocol version 2 only.

NULL packet Is the message sent by the server to the client. The same packet is sent by the client to the server. A TCP NULL packet does not contain any controlling flag like SYN, ACK, FIN etc. because the server does not require a reply from the client. The NULL packet is described here:

Then configuring the sshd part on the server.

ClientAliveInterval 60
TCPKeepAlive yes
ClientAliveCountMax 10000

ClientAliveInterval The server will wait 60 seconds before sending a null packet to the client to keep the connection alive

TCPKeepAlive Is there to ensure that certain firewalls don’t drop idle connections.

ClientAliveCountMax: 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 therefore will not be spoofable. The TCP keepalive option enabled by TCPKeepAlive is spoofable. The server alive mechanism is valuable when the client or server depend on knowing when a connection has become inactive.

Finally restart the ssh server

service ssh restart
service sshd restart
#depending on what system you are on.

Spread the word. Share this post!

Leave Comment

Your email address will not be published. Required fields are marked *