Keeping SSH Sessions Alive February 3, 2006
Posted by Carthik in administration, snippets, ubuntu.trackback
Scott Merrill writes in to say:
I noticed recently that ssh connections from my Ubuntu laptop to my
Debian server would time out and disconnect if I left the connection
idle for a long-ish period of time. This really annoyed me, because my
Debian desktop does not exhibit this behavior when connecting to the server.I added the following line to
/etc/ssh/ssh_config:
ServerAliveInterval 5That seems to have fixed the problem, and my laptop can now remain
connected, though idle, to my server. Maybe this will help someone
else, too.
What this does, essentially is every 5 seconds, the client sends a small keep-alive packet to the server to make it look like the ssh connection is being actively used. The reason for Scott’s timeout could be a NAT firewall that seeks to minimize the nember of active connections to reduce its memory footprint, or to improve performance for other clients. Most firewalls and networks let you keep your connections alive for as long as you wish, but some may act up, and that’s when you can use Scott’s trick.
Tip: Change 5 to 240 or 300, so that instead of every 5 seconds, the keep-alive signal is sent only once in 4 or 5 minutes
Sorry it took me so long to post this useful tip, Scott.







The point of dropping the connection is a security one, though. I let mine time out after idling for that reason. Just sayin’.
I’m using gnu screen with a status line that displays the time, and as this is refreshed every minute, its enough.
[...] This post from Scott Merril explains how to keep SSH sessions alive. [...]
On the Ubuntu SSH server side (sshd_config) I inserted these lines:
ClientAliveInterval 60
ClientAliveCountMax 60
and got pretty much the same result, although it says 60min, it doesn’t disconnect (time out) ssh clients. It really annoyed me before…
hey, great stuff……any idea how i can resume a ssh session…
im no expert, so i’d thought you’d be able to harness your contacts and resevoir of knowledge.
ie i start a ssh session to my exclusive torrent box, and start some torrents via bitttorrent-ncurses, then either leave the sessions, or maybe my ssh client machine dies.
how do i resume a ssh session.
woops. how do i resume the session, and keep an eye on the progress of the torrents.
or are programs that are executed by a remote ssh client always killed upon client exiting the session?
airtonix: apt-get install screen
(then lookup the man page for it on how to detach and re-attach sessions)
That man page is really for looking up known information. I’d suggest taking a look at McPherson’s tutorial (originally from kuro5hin): http://jmcpherson.org/screen.html
[...] El caso es que se me ha ocurrido que esto debería estar ya pensado en SSH, por ejemplo enviando un «Keep-Alive», y efectivamente es así. En «Keeping SSH connections alive» lo he encontrado. Y parece que funciona. Related posts in t8i References in t8i [...]
[...] Keeping SSH Sessions Alive « Ubuntu Blog (tags: linux ubuntu howto) [...]
You can also make this change in your home directories .ssh folder.
In the file, replace “username” with your name, create a text file if it doesn’t exist:
/home/username/.ssh/config
make the following entries:
serveraliveinterval 60
serveralivecountmax 10
Adjust as you see fit.
Is there any way to specify a idle/timeout setting when initiating the ssh client session to the remote server? I would like to not modify the default behavior of SSH, just a single instance from time to time. If possible, please post here would be a nice addition.
[...] Technical Cons. – Initial connections and DNS lookups are rubbish. It takes several seconds for DNS queries to complete. New ssh connections take about 5 seconds to come up. – It’s NATed. You don’t seem to get a real IP number and you can’t make inbound connections. – It dropped ssh connections like nobodie’s business. This was becoming a real issue until I realised why it was happening; you just need to set an ssh keep alive. [...]
[...] Keeping SSH Sessions Alive « Ubuntu Blog [...]
[...] agressive NAT gateway which is timing out your connections as fast as it can. You just need to set an ssh keep alive. – There seems to be a transparent http proxy involved. You can’t access an http svn [...]
[...] Quelle: ubuntu.wordpress.com [...]
Q. How to add a status line to “screen” that displays the time?
A. Add this to ~/.screenrc
hardstatus alwayslastline “%D-%M-%Y %c”
[...] References:Keeping SSH Sessions Alive [...]
Hey Scott, thanks for the tip. This was really annoying!!!!
Hi guys I would like to have all ssh connections time out after 5 minutes of inactive connection
Triniboy: see Jack’s comment (number 11).
Scott (and Carthik): thanks for the tip, this was exactly what I needed!