If you need to keep a SSH connection alive, e.g., when you won't be entering any commands for awhile after logging into the remote system via SSH, you can use the
ServerAliveInterval. You can specify the interval in seconds which will be used by the SSH client to send keepalive packets with
-o ServerAliveInterval xwhere x is the frequency for sending the keepalive packets. E.g., if I wanted the SSH client to send keepalive packets every minute (60 seconds) to the remote SSH server, I could use a command like the one below when establishing the SSH session:
$ ssh -o ServerAliveInterval=60 email@example.com
By using this option, you should be able to reduce the likelihood that your SSH connection will get dropped after a certain amount of time due to no activity for the session.
You can also use the
parameter with ServerAliveInterval to drop the connection, if the SSH
client hasn't received a response from the server to the prior "heartbeat"
signal when the time comes to send another keepalive packet. E.g.,
-o ServerAliveInterval=60 -o ServerAliveCountMax=1 firstname.lastname@example.org
would result in the connection being dropped if the client was awaiting
a response to even one outstanding keepalive packet.
There is also a
TCPKeepAlive option in
That option is used to recognize when a connection is no longer active due
to some problem such as the SSH client application crashing or a prolonged
network outage. If the SSH server never recognizes that the client is no
longer communicating with it, it will continue to allocate resources,
such as memory, for the connection. The option is turned on by default
in the OpenSSH configuration file
will see the following line in that file:
You don't need to uncomment the line by removing the pound sign, since "yes" is the default value. The option causes Transmission Control Protocol (TCP) to periodically transmit keepalive messages. If it doesn't receive responses within the expected time, it returns an error to the sshd process, which will then shut down the connection. The purpose of this option is to prevent half-dead connections building up over time and consuming more and more system resources as the number grows. The keepalive interval is typically in the order of hours rather than minutes to minimize the network load for the server. If the keepalive period was made shorter, that would affect all TCP connections on the system, not just the SSH ones, potentially increasing the network load unnecessarily and also causing connections to be dropped even for transient issues, such as a short and temporary network issue.
The TCPKeepalive option is for dealing with longer term issues for a connection rather than the loss of connectivity due to firewall, proxying, or Network Address Translation (NAT) timeouts. You can specify the option on the command line at the SSH client end as follows:
$ ssh -o TCPKeepAlive=yes email@example.com
SSH, The Secure Shell: The Definitive
By: Daniel Barrett, Richard Silverman, Robert Byrnes