jump to navigation

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 5

That 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.

Comments»

1. BEG - February 3, 2006

The point of dropping the connection is a security one, though. I let mine time out after idling for that reason. Just sayin’.

2. fred - February 3, 2006

I’m using gnu screen with a status line that displays the time, and as this is refreshed every minute, its enough.

3. Samokk is a Geek » Keeping SSH sessions alive - February 4, 2006

[…] This post from Scott Merril explains how to keep SSH sessions alive. […]

4. krampo - February 8, 2006

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…

5. airtonix - February 12, 2006

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.

6. airtonix - February 12, 2006

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?

7. elixxir - February 15, 2006

airtonix: apt-get install screen
(then lookup the man page for it on how to detach and re-attach sessions)

8. bvankuik - March 6, 2006

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

9. diego sevilla’s weblog » Conexiones SSH que se cortan - March 8, 2006

[…] El caso es que se me ha ocurrido que esto debera 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 […]

10. Little Fucking Ray of Sunshine » Blog Archive » links for 2007-02-08 - February 10, 2007

[…] Keeping SSH Sessions Alive « Ubuntu Blog (tags: linux ubuntu howto) […]

11. Jack Suzuki - June 1, 2007

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.

12. Dean - December 4, 2007

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.

13. Tony McCrae - I’m a Tourist - Get me out of Here! » Blog Archive » Review: Three’s wireless broadband thing-a-ma-gig - December 16, 2007

[…] 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. […]

14. Linux T&T : Keeping SSH Sessions Alive - April 8, 2008

[…] Keeping SSH Sessions Alive « Ubuntu Blog […]

15. I’m a Tourist - Get me out of Here! » Blog Archive » Review: Three’s wireless broadband thing-a-ma-gig - May 26, 2008

[…] 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 […]

16. Christoph Hüffelmann, der Blog » Blog Archive » SSH keep alive time - July 10, 2008

[…] Quelle: ubuntu.wordpress.com […]

17. Stephan Wehner - July 28, 2008

Q. How to add a status line to “screen” that displays the time?

A. Add this to ~/.screenrc

hardstatus alwayslastline “%D-%M-%Y %c”

18. open sshd time out problem « My everyday fixes for linux and other networking troubles - July 29, 2008

[…] References:Keeping SSH Sessions Alive […]

19. Fabio - September 2, 2008

Hey Scott, thanks for the tip. This was really annoying!!!!

20. triniboy - September 15, 2008

Hi guys I would like to have all ssh connections time out after 5 minutes of inactive connection

21. Dysprositos - December 17, 2008

Triniboy: see Jack’s comment (number 11).

Scott (and Carthik): thanks for the tip, this was exactly what I needed!

22. TEHEK - July 12, 2009

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.

23. Anthony Honstain - September 9, 2009

Awesome! Thanks for the tip.

24. Mauro Morales - September 18, 2009

very very usefull tip, thanks!

25. sikiş - September 20, 2009

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.

26. Jeremy - November 20, 2009

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.

27. Sağlık Bilgilendirme - April 2, 2010

Thank you for the opportunity you gave

28. ChrisMar035 — Keep ssh sessions from timing out - May 20, 2010

[…] The Ubuntu Blog has a nice lil’ article about keeping SSH sessions alive. […]

29. Mantener viva la sesión ssh « El Blog de rubensa - June 14, 2010

[…] vía Posted by rubensa Filed in GNU/Linux, Recetas Leave a Comment » […]

30. Anon - July 13, 2010

This works for sshfs and sftp and ssh port forwarding as well

31. SSH session timeout or Putty session timeout, Network error: Software caused connection abort | RafaelWolf.com - September 22, 2010

[…] 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 […]

32. porno - September 23, 2010

ayın yıldız filmi

Ron Jeremy - December 30, 2010

Hey remove this porn link crap!!! Post #32 is bullshit spam porn link!!!

33. porno sikiş - September 27, 2010

The point of dropping the connection is a security one, though. I let mine time out after idling for that reason. Just sayin’.

34. sex sikiş - September 28, 2010

airtonix: apt-get install screen

35. Prevent SSH Timeout on Remote Server via Client Configuration - October 6, 2010

[…] https://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: […]

36. ankara keçiören nakliyat - December 4, 2010

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

37. SSH port forwarding from VPS to home server - January 6, 2011

[…] 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 […]

38. Jessy de Bruyn - February 10, 2011

Tip: when the session does freeze, hit enter, followed by tilde (~), followed by a dot (.). That doesn’t resume the session though.

39. blogTrav » Keeping My SSH Connections Alive on Verizon 4G LTE - April 10, 2011

[…] 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 […]

40. eskişehir satılık ev - April 15, 2011

eskişehir satılık evler uygun fiyata satılık ev ilanları.

41. piri reis - May 18, 2011

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.

42. piri reis - May 18, 2011

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).

43. neil - May 26, 2011

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

44. ergo baby carrier - May 28, 2011

I have the same problem.ergo baby carrier sale

45. gabblet - July 18, 2011

nice informative tips for me thanks

46. ukrayna vizesi - July 30, 2011

İYİ

47. Leny - July 31, 2011

run ‘top’ when you are done with your window! Problem solved. You can keep an eye on the box too with that.

48. arkadaş - August 5, 2011

teşekürler bilgi için elinize sağlık

49. antalya böcek ilaçlama - August 7, 2011

antalya ev ilaçlama

50. webchat - September 11, 2011

hello and thank you…

51. Master Ryan - October 16, 2011

fyi this works great for Krusader on Ubuntu Natty.

52. Keeping My SSH Connections Alive on Verizon 4G LTE | blogTrav - April 10, 2013

[…] 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 […]

53. what causes hives - November 28, 2013

I do not even know how I ended up here, but I thought this post was great.
I don’t know who you are but certainly you are going to a famous blogger if you aren’t already 😉
Cheers!

Lourdes - December 14, 2013

Great blog here! Additionally your web site a lot up fast!
What web host are you the usage of? Can I am getting your affiliate link on your host?
I desire my web site loaded up as fast as yours lol

54. oreo 5s - November 28, 2013

if you are looking to acquire a remarkable, nice and interesting oreo 5s…you’ve noticed all of them! the actual typical sparkely oreo 5s work best! when we look at you these guys discontinue and also look! avoid surely enjoy they relating to the oreo 5s and value!! they may so excellent produce into the shopping area, the evening meal or wherever you will amagine!

55. mildew remover - December 14, 2013

I really like it when people come together and share views.
Great site, stick with it!

56. kinesiology tape - February 26, 2014

I blog quite often and I seriously thank you for your content.
This great article has really peaked my interest. I will book mark your website and keep checking for new details about once a week.

I subscribed to your Feed too.

57. castles for - August 5, 2014

Hey just wanted to give you a quick heads up and let you know a
few of the images aren’t loading correctly. I’m not sure why
but I think its a linking issue. I’ve tried it in two different web
browsers and both show the same results.

58. offers on high quality swiss replica watches - September 2, 2014

Hi mates, how is all, and what you would like to say regarding this article, in my
view its truly awesome in support of me.

59. No pain, No Gain - Bodybuilding Documentary - September 22, 2014

If you locate that your scale is increasing in numbers, don’t forhet that you just mmay be losing
fat while gaining muscle.

60. www.salope-asiatique.info - September 24, 2014

Je νoudrai vus dire qque c’est vraiment une joie de vous lire

61. SEO techniques - October 4, 2014

This piece of writing presents clear idea designed for the new viewers of blogging, that truly how to do blogging.

62. pornhub gratuit - October 30, 2014

Magnifique ρoste une foiѕ de plus

63. Prevent closing of SSH Local Port Forwarding – segmentfault - November 15, 2015

[…] There is an example of configuring it here: https://embraceubuntu.com/2006/02/03/keeping-ssh-sessions-alive/ […]

64. 86Katrina - August 25, 2017

I see you don’t monetize your website, don’t waste your traffic, you can earn additional bucks every
month because you’ve got hi quality content. If you want to know how to make extra bucks, search for:
best adsense alternative Wrastain’s tools

65. emlakzone - December 23, 2018

Depending on your hardware and software configuration, you may experience SSH connectivity issues. For example, some types of firewalls may preemptively close connections after a certain amount of time has elapsed.http://www.emlakzone.com

66. Stetson - February 21, 2022

Most of what you say happens to be astonishingly precise and that makes me ponder the reason why I hadn’t looked at this with this light previously. This particular piece truly did switch the light on for me personally as far as this subject matter goes. Nevertheless at this time there is just one issue I am not too comfortable with and while I attempt to reconcile that with the core theme of your position, permit me observe what all the rest of the readers have to point out.Very well done.


Leave a Reply to castles for Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: