SSH keeps disconnecting after few minutes of inactivity
Some over-zealous routers like to drop TCP connections that are idle for too long (i.e. don't transmit any data). It might because they assume the user only uses things like HTTP, where the connection is often closed after a single query is complete.
Assuming OpenSSH, use the ClientAliveInterval
and ClientAliveCountMax
directives in sshd_config
, or equivalently ServerAliveInterval
and ServerAliveCountMax
in the client side config (~/.ssh/config
or /etc/ssh/ssh_config
) to enable protocol-level keepalive packets.
They're actually meant to detect if the remote host has gone away, but since they cause messages to be sent when the connection is otherwise idle, they also work to prevent the connection from being seen as idle by outside devices.
*AliveInterval
interval after which the client/server sends a query to the remote, and *AliveCountMax
sets the number of unanswered queries after which the the client/server drops the connection as inactive.
Something like these values should do:
ClientAliveInterval 15
ClientAliveCountMax 4