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!

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!
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).
@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.
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
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 !
@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:
Will create a new file ready for editing. The same is true for other command line text editors such as nano/pico too.
@Gary:
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.
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
@Gary: Hey, no need to apologise
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!