Muffinresearch Labs by Stuart Colville

Keeping ssh connections alive | 9 Comments

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

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

  • http://nefariousdesigns.co.uk/ Tim

    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!

  • http://www.xaprb.com Xaprb

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

  • http://muffinresearch.co.uk Stuart Colville

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

  • Dave

    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

  • http://www.binaryslate.co.uk Gary

    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 !

  • http://muffinresearch.co.uk Stuart Colville

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

  • http://www.binaryslate.co.uk Gary

    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

  • http://muffinresearch.co.uk Stuart Colville

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

  • http://www.ginside.com/ Jonathan

    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!

GNU screen: open tab in current working directory|(1)

A nice trick for having screen open a new tab in the same directory as the one you’re currently in. To use it add it to your .screenrc

# Open new window in current dir.
bind c stuff "screen -X chdir \$PWD;screen^M"
bind ^c stuff "screen -X chdir \$PWD;screen^M"

Hat tip: mteckert on SuperUser.com

Ubuntu: add-apt-repository: command not found|(3)

When you’re using a minimal Ubuntu install if you find the ‘add-apt-repository’ command is missing (it’s useful for adding PPAs and other repositories), then simply run:

sudo apt-get install python-software-properties

Photos on Flickr

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