ssh keeps disconnecting

“ssh keeps disconnecting my session”. Here’s the fix!

Server Side Fix

As root modify /etc/ssh/sshd_config and add these lines

##Server will send "keep alive" messages every 4minute 58 seconds
ClientAliveInterval 298

##If there is no response after 4.1 hours close the connection
ClientAliveCountMax 50

If you want to be able to ssh to other machines from your server without getting disconnected all the time you need to:

  1. su to your main user
  2. edit ~/.ssh/config
  3. add these lines:
Host *
    ServerAliveInterval 240

Finally you must:
Modify permissions of ~/.ssh/config
Restart sshd

# chmod 600 ~/.ssh/config
# service sshd restart

You must fix your client (ex: Putty) if you are behind a NAT firewall and are still experiencing issues!

source: http://tartarus.org/~simon/putty-snapshots/htmldoc/Chapter4.html#config-keepalive

The Connection panel allows you to configure options that apply to more than one type of connection.

4.13.1 Using keepalives to prevent disconnection

If you find your sessions are closing unexpectedly (most often with ‘Connection reset by peer’) after they have been idle for a while, you might want to try using this option.

Some network routers and firewalls need to keep track of all connections through them. Usually, these firewalls will assume a connection is dead if no data is transferred in either direction after a certain time interval. This can cause PuTTY sessions to be unexpectedly closed by the firewall if no traffic is seen in the session for some time.

The keepalive option (‘Seconds between keepalives’) allows you to configure PuTTY to send data through the session at regular intervals, in a way that does not disrupt the actual terminal session. If you find your firewall is cutting idle connections off, you can try entering a non-zero value in this field. The value is measured in seconds; so, for example, if your firewall cuts connections off after ten minutes then you might want to enter 300 seconds (5 minutes) in the box.

Note that keepalives are not always helpful. They help if you have a firewall which drops your connection after an idle period; but if the network between you and the server suffers from breaks in connectivity then keepalives can actually make things worse. If a session is idle, and connectivity is temporarily lost between the endpoints, but the connectivity is restored before either side tries to send anything, then there will be no problem – neither endpoint will notice that anything was wrong. However, if one side does send something during the break, it will repeatedly try to re-send, and eventually give up and abandon the connection. Then when connectivity is restored, the other side will find that the first side doesn’t believe there is an open connection any more. Keepalives can make this sort of problem worse, because they increase the probability that PuTTY will attempt to send data during a break in connectivity. (Other types of periodic network activity can cause this behaviour; in particular, SSH-2 re-keys can have this effect. See section 4.19.2.)

Therefore, you might find that keepalives help connection loss, or you might find they make it worse, depending on what kind of network problems you have between you and the server.

Keepalives are only supported in Telnet and SSH; the Rlogin and Raw protocols offer no way of implementing them. (For an alternative, see section 4.13.3.)

Note that if you are using SSH-1 and the server has a bug that makes it unable to deal with SSH-1 ignore messages (see section 4.25.1), enabling keepalives will have no effect.

4.13.2 ‘Disable Nagle’s algorithm’

Nagle’s algorithm is a detail of TCP/IP implementations that tries to minimise the number of small data packets sent down a network connection. With Nagle’s algorithm enabled, PuTTY’s bandwidth usage will be slightly more efficient; with it disabled, you may find you get a faster response to your keystrokes when connecting to some types of server.

The Nagle algorithm is disabled by default for interactive connections.

4.13.3 ‘Enable TCP keepalives’

NOTE: TCP keepalives should not be confused with the application-level keepalives described in section 4.13.1. If in doubt, you probably want application-level keepalives; TCP keepalives are provided for completeness.

The idea of TCP keepalives is similar to application-level keepalives, and the same caveats apply. The main differences are:

  • TCP keepalives are available on all connection types, including Raw and Rlogin.
  • The interval between TCP keepalives is usually much longer, typically two hours; this is set by the operating system, and cannot be configured within PuTTY.
  • If the operating system does not receive a response to a keepalive, it may send out more in quick succession and terminate the connection if no response is received.

TCP keepalives may be more useful for ensuring that half-open connections are terminated than for keeping a connection alive.

TCP keepalives are disabled by default.

Solution

  1. Open PuTTY.
  2. Select Left Menu ‘Connection‘.
  3. Add seconds between keep alive, based on on you sever. If not sure put 120.
  4. Also u can enable TCP commands SO_KEEP_ALIVE option.

One comment

  1. The “Server Side Fix” is possible only if you have root access to the server – not a case for any security jumpers.
    The “keepalives” settings are not always accepted fro the server/router/switch end, as well…

    From Windows you can use AutoHotkey (IronAHK for Linux) to automate a keepalive activity.
    That’s one script example:


    SetTitleMatchMode 2 ; 2 - partially, 3 - exact match
    MyWinTitle=- PuTTY ; Describe the window for keep alive signals
    Timeout=180000 ; Call KeepAlive every 180 seconds (180000 ms)

    ttl := Timeout / 1000
    ToolTip, Keepalives every '%ttl%' seconds for the last touched '%MyWinTitle%' window`nTo stop the keepalive press F12
    SetTimer, RemoveToolTip, 3000 ; Shows the tooltip for 5 seconds
    return

    RemoveToolTip:
    SetTimer, RemoveToolTip, Off ; only once
    ToolTip
    Gosub, MyKeepAlive
    Return

    MyKeepAlive:
    WinGetActiveTitle, Title

    IfWinExist, %MyWinTitle%
    {
    WinActivate
    WinGet, mywin_id
    WinGetActiveTitle, mywin_title
    Send {!}{BS}
    MsgBox,,Info message, The window for keep-alives is '%mywin_title%',3
    WinActivate, %Title%
    }
    else
    {
    MsgBox,,Exit message, Open the window first!,10
    ExitApp
    }

    SetTimer, KeepAlive, %Timeout%
    Return

    KeepAlive:
    WinGetActiveTitle, Title
    WinGetTitle, current_title, ahk_id %mywin_id%

    If current_title = %mywin_title%
    {
    WinActivate, ahk_id %mywin_id%
    Send {!}{BS}
    WinActivate, %Title%
    }
    else
    {
    MsgBox,,Exit message, The window was closed!,10
    ExitApp
    }
    Return
    Return

    F12::ExitApp ; Exit button

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.