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!
Hey, thx for info! I was scanning through manpage and dind’t notice the option right a way.
Here’s the commanline I use:
# ssh -o ServerAliveInterval=10
sets 10 sec keep-alive-ping interval for this particular connection.
Awesome! Thanks for the tip.
very very usefull tip, thanks!
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.
sikiÅŸ,
Look into this command:
ssh -o TCPKeepAlive user@host
I’m not sure what the defaults are when using it but it’ll basically do the same thing as mentioned in this post.
Thank you for the opportunity you gave
[...] The Ubuntu Blog has a nice lil’ article about keeping SSH sessions alive. [...]
[...] vÃa Posted by rubensa Filed in GNU/Linux, Recetas Leave a Comment » [...]
This works for sshfs and sftp and ssh port forwarding as well
[...] this tip is for a Linux client (found here –> Link); from February of 2006 it’s still pertinent today and what tipped me off to where I might [...]
ayın yıldız filmi
Hey remove this porn link crap!!! Post #32 is bullshit spam porn link!!!
The point of dropping the connection is a security one, though. I let mine time out after idling for that reason. Just sayin’.
airtonix: apt-get install screen
[...] http://embraceubuntu.com/2006/02/03/keeping-ssh-sessions-alive/ This entry was posted in Linux. Bookmark the permalink. Post a comment or leave a trackback: [...]
evden eve But I believe that resolves a few questions for me personally. Anyone else agree?This works for sshfs and sftp and ssh port forwarding as well ankara
[...] used exactly as stated in ssh’s manpages. Last, I had a problem with the SSH session dying, possibly due to lack of activity as in this article. I used the ‘-o ServerAliveInterval=’ command line option and that seemed to make it [...]
Tip: when the session does freeze, hit enter, followed by tilde (~), followed by a dot (.). That doesn’t resume the session though.
[...] workflow has a lot of 5 minute breaks from SSH, too. After some searching the ‘net, I found a reference to setup SSH so that it transmits a keepalive packet to keep my connection alive. That got me [...]
eskişehir satılık evler uygun fiyata satılık ev ilanları.
Do you publish anything witout first seeing what is written in it?
Do not do it or there will be always kinds of ads here.
By the way, I am thankful for the tip. It had been 11 minutes idle and I checked still the session is up. It should have gone so far without this fix (I used 180 secs).
Put this in a script on the server then add the script to ~/.bashrc with & to run in background:
while true
do
echo -en “\x0″
sleep 60
done
I have the same problem.ergo baby carrier sale
nice informative tips for me thanks
İYİ
run ‘top’ when you are done with your window! Problem solved. You can keep an eye on the box too with that.
teşekürler bilgi için elinize sağlık
antalya ev ilaçlama
hello and thank you…
fyi this works great for Krusader on Ubuntu Natty.
[...] workflow has a lot of 5 minute breaks from SSH, too. After some searching the ‘net, I found a reference to setup SSH so that it transmits a keepalive packet to keep my connection alive. That got me [...]