ServersKeeping Your ssh Connection Alive

Keeping Your ssh Connection Alive

ServerWatch content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.




You may have had the experience of having your SSH connection to
another machine drop if you ignore it for too long. Which can be irritating,
especially if it’s (say) your e-mail.

Tip of the Trade: Ignore your SSH connection to another machine for too long and it will drop. The SSH config option, ServerAliveInterval, remedies this for those times when you really want to maintain the link.

You can fix this using the SSH config option ServerAliveInterval. SSH
options can be set on the command-line (with the -o option), or by
editing your ~/.ssh/config or /etc/ssh_config files. Note
that SSH uses the first setting that it encounters for an option, and it reads
command-line options, then per-user config, then the global config, and
general/default ones at the end.

Add this to your ~/.ssh/config file to turn ServerAliveInterval on
for all SSH connections:

Host *
Protocol 2
ServerAliveInterval 60

The first two lines state that this section applies to every host (to apply
only to some hosts, specify a hostname rather than *) and that a SSH v2
connection should be made (SSH v2 is significantly more secure than SSH v1,
and ServerAliveInterval requires v2).

ServerAliveInterval sends a package through the encrypted channel,
after the specified number of seconds of inactivity, to the server, requesting
a response. It’s turned off by default, and you may need to experiment a
little with the value. However, once you have the value set to work with your
system, you should be able to keep your SSH sessions connected.

Note: Most systems will have TCPKeepAlive on by default. This sends a
TCP package after a period of idleness, usually two hours. This is often too
long an interval to prevent disconnection; and TCPKeepAlive packets can also
be spoofed, unlike ServerAliveInterval.

Get the Free Newsletter!

Subscribe to Daily Tech Insider for top news, trends & analysis

Latest Posts

Related Stories