SEARCH

How-To Geek

Keep Your Linux SSH Session From Disconnecting

I’m the type of geek that has an SSH client open at all times, connected to my most frequently used servers so that I have instant access for monitoring and anything else. As such, it irritates me greatly when I get disconnected, so I’m sharing a few methods for keeping your session alive.

You can configure the ssh client to automatically send a protocol no-op code code every number of seconds so that the server won’t disconnect you. This is setting is sometimes referred to as Keep-Alive or Stop-Disconnecting-So-Much in other clients.

Global Configuration

Add the following line to the /etc/ssh/ssh_config file:

ServerAliveInterval 60

The number is the amount of seconds before the server with send the no-op code.

Current User Configuration

Add the following lines to the ~/.ssh/config file (create if it doesn’t exist)

Host *
  ServerAliveInterval 60

Make sure you indent the second line with a space.

Per-Host Configuration

If you only want to enable keep alive for a single server, you can add that into the ~/.ssh/config file with the following syntax:

Host *hostname.com
   ServerAliveInterval 60

Works quite well, hope it helps somebody else out there.

Lowell Heddings, better known online as the How-To Geek, spends all his free time bringing you fresh geekery on a daily basis. You can follow him on if you'd like.

  • Published 08/16/07

Comments (15)

  1. Micha

    Thanks a lot!

    That’ll save my life!
    OK, I wouldn’t die if i didn’t know this, but it shuold make life easier. I hate being disconnected…

  2. Chuck

    That’s a great tip. Another idea (one you can use in tandem with the above) is to use the “screen” program. Then, if you are somehow disconnected you can just re-atach the screen and all your stuff is there just as you left it.

  3. Dan

    Already knew this, but it popped up on my iGoogle page as I have your feed on there, great tip and excellent explination, K.I.S.S :D (Keep it simple Stupid)

    I’ll be pointing some of my customers to this blog entry :P

  4. oOJINxOo

    ty soooo much ^_^

  5. Nathan

    Helped much, thanks mate!

  6. Ammar

    thnx :)

  7. Chris

    So I have an account to a linux machine @ school that I do most of my programming projects through. I ssh into my account and work from there and many a times my session times out while reading a how-to article and I have to reconnect( annoying ). The thing is security on my schools system is really tight. /what I want to know is will I be able to run this command as joe user or do I need root permission to do it? I can edit some things like my bash profile but I cannot add programs with apt or change any system files….. It seems like the session time is something regulated by the systems admins but idk……… thanks for your help. I love this blog!

  8. linux lover

    Thanks for this tips.
    It help me much today, working on SSH connection.

  9. rakeen

    Thanks a lot. I have found this solution for long time.

  10. Ferdinand

    wow !!! thank you so much :D
    just as i need
    now i can do more multitasking

  11. Alexis Rondeau

    Thank you very much for sharing this, works well on my Mac!

  12. Tom Fix

    I was struggling with this on Cygwin. I don’t know why they wouldn’t add these parameters in the installation.

  13. blackmacmac

    great tip, thanks a lot. I didn’t know I should add the line under ‘Host *’. Somebody says that you need to add ‘ServerAliveCountMax N’ (such as ‘ServerAliveCountMax 100′, the default is 3), otherwise only a maximum of 3 messages will be sent to the server (or 3 times)…. , the connection is down. Makes sense, but haven’t tested yet.

  14. Sivael

    That is one of the most useful little thingies ever.
    I mean – working on two servers plus local with update scripts wih them constantly disconnecting – tedious:P
    Now, I can simply walk into Mordor :D

  15. sawrub

    Is there a way to overcome this situation.

    Situation : I’m on a LAN connected to remote server over SSH, and had to go over for a meeting on another floor along with my notebook. I switch on WLAN and connect to it with a descent 90+ signal strength, disconnect my LAN. Network manager shows both connections there, but when i try to fire a command on the server [ using gnome terminal ] to which i was connected to, the server is not responding and i have to make a new connection to it.

    All of the above settings have been done, but that seems to work only in case e.g. i connect to a server over LAN [ have both LAN and WLAN active ]. I break the LAN connection, try connecting to the server over WLAN, but server does not reply back. I connect back to the LAN, and it works.

    OS : Fedora 14
    OpenSSH_5.5p1, OpenSSL 1.0.0e-fips 6 Sep 2011
    NetworkManager Applet 0.8.4

Get Free Articles in Your Inbox!

Join 134,000 newsletter readers

Email:

Go check your email!