Muffinresearch Labs by Stuart Colville

Keeping ssh connections alive | Comments (9)

Posted in Linux/Unix on 1st June 2006, 2:44 pm by Stuart

Picture this; you come back from lunch at “My Old Dutch Pancake house” and every one of your ssh connections has been closed due to lack of activity. It’s reasonable that the server you are connected to (or the hardware you are connecting through) closes connections due to idling, but it’s such a pain to have remember each and every directory you were working on to be able to pick up where you left off. To remedy this there is a very simple thing you can do which will prevent your ssh connections from terminating prematurely.

On the mac (linux distributions should be similar) open terminal and type (you should be in your home directory if not type ‘cd ~’ first):

ls -al

You should see a list of files including the directory .ssh (the -a switch shows all files including hidden ones) Hidden files are not shown by default on the mac and if you would always like to see hidden files in bash you can add an alias for ls -a to your .bash_profile. I’ll cover this in a future post.

Next change directory into the .ssh directory (if it doesn’t create it first with ‘mkdir .ssh’):

cd .ssh

Now open the file called ‘config’ and add the following line (again you will need to create it if it doesn’t exist already):

ServerAliveInterval 240

This will now send a packet of data every 4 minutes so that your ssh session does not get disconnected. Job’s a good ‘un!

Post Tools

Comments: Add yours

1. On June 1st, 2006 at 3:15 pm Tim said:

Of course, if you’re using SSH for SFTP, you could also depend apon your client software.

WinSCP has a rather handy “Remember last used directory” checkbox in it’s session settings dialogs!

Not sure if that’s really relevant here, but it’s saved me some headaches in the past!

2. On June 1st, 2006 at 3:39 pm Xaprb said:

My favorite thing to do is start a GNU Screen session once I’m connected to the remote machine. If the connection goes down, no worries. The screen session is still there. I can re-connect and re-attach to the session. Screen has become indispensable for my daily work, for this and dozens of other reasons (leave a long-running job at work, then re-connect and check on it from home, etc etc).

3. On June 1st, 2006 at 3:53 pm Stuart Colville said:

@Tim: Thanks for keeping the flame burning for PC toting readers :-) .

@Xaprb: Good point, that’s one of the biggest benefits with screen and why everyone should check it out. For anyone new to screen there’s a great overview here: Gnu Screen.

4. On June 1st, 2006 at 5:37 pm Dave said:

What’s the “config file”

I don’t seem to have one:
————————
host:~/.ssh dave$ ls -a ~/.ssh
authorized_keys
id_rsa.pub
local.key
id_rsa
known_hosts

5. On June 1st, 2006 at 8:31 pm Gary said:

Also, if you happen to use the excellent Putty as your ssh client, it has a ‘Sending of null packets’ option in the Connection settings to help keep the connection alive.

A good tip for the server side though, one to chalk down for later !

6. On June 1st, 2006 at 9:26 pm Stuart Colville said:

@Dave: I’ve updated the line regarding the config file so that it’s less ambiguous. If you don’t have a file called ‘config’ in the .ssh directory you will need to create it first. Running:

vim config

Will create a new file ready for editing. The same is true for other command line text editors such as nano/pico too.

@Gary:

A good tip for the server side though, one to chalk down for later !

Actually this isn’t for the server. This configuration is to make the ssh command you use locally keep alive your connection. This way is better than a server-side configuration as not everyone will have access to the server’s ssh configuration.

The sending of null packets that putty has sounds like the same thing for putty users. Well spotted.

7. On June 2nd, 2006 at 8:49 pm Gary said:

Yeah, I was meaning in the sense that I normally hop through from one server to an internal server via ssh, so i’ll use the ServerAliveInterval to stop that connection dropping out.

Apologies for the confusion :)

I think I’ll be having a good read of man ssh

8. On June 2nd, 2006 at 9:49 pm Stuart Colville said:

@Gary: Hey, no need to apologise ;-)

9. On July 19th, 2007 at 6:11 pm Jonathan said:

Awesome! Thanks, this worked like a charm. It was really starting to bug me that it would time-out every hour and wouldn’t leave me connected.

Great fix!







XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>



Using Loggerhead with mod_wsgi|(0)

Here’s a post I wrote over on the Project Fondue Blog about our use of Loggerhead with mod_wsgi under Apache. Loggerhead is the rather nice branch viewer for bazaar branches as used on Launchpad.net.

If you’re not already subscribed to the Project Fondue blog feed then I can recommend it, as there should be some interesting posts coming out of there in the coming months (yes I’m unashamedly biased!).

Ubuntu: Turn off changing workspace with mouse wheel|(1)

I found the changing with the workspace with the mouse wheel really annoying. To disable it go to System => Preferences => CompizConfig (available if the compizconfig-settings-manager package is installed) and uncheck “Viewport Switcher” which is under the “Desktop” heading.

Photos on Flickr

© Copyright 2004-10 Stuart Colville, all rights reserved. May contain traces of Muffin. Powered by WordPress. Hosting by Slicehost.com This page was baked in 0.624s.