436

I use ssh -p8520 username@remote_host to login remote server.

Issue:

It is always connected and works properly when I am in the work place. Unfortunately, terminal freezes in 10 - 15 minutes after I connected with the remote server from home.

There's no error/timeout report on the console but the cursor cannot move any more.

When enter w to check the login users, some zombies login users are there, and I have to kill them manually.

This is quite annoying. Can anyone help me?

7
  • 1
    I have had this too, I started to use screen. Maybe this is some kind of a timeout issue.
    – martin
    Aug 1, 2014 at 16:12
  • 1
    @martin screen helps to keep the program running. Unfortunately sometimes i have to work on the remote server:( Aug 1, 2014 at 16:14
  • If you start it without parameters, this gives you a shell.
    – martin
    Aug 1, 2014 at 16:15
  • Are your saying screen only? It can solve the lost connection issue? No screen -S screenName? I always use screen with -S and -r Aug 1, 2014 at 16:16
  • 1
    No, this does not solve the issue, you only avoid loosing your work in the terminal. If you don't do anything, it will still freeze, you just can pick up where you started. I meant that you can create a terminal which you can always resume by using screen. You seem to already know that ;)
    – martin
    Aug 1, 2014 at 16:20

5 Answers 5

756

The ssh daemon (sshd), which runs server-side, closes the connection from the server-side if the client goes silent (i.e., does not send information). To prevent connection loss, instruct the ssh client to send a sign-of-life signal to the server once in a while.

The configuration for this is in the file $HOME/.ssh/config, create the file if it does not exist (the config file must not be world-readable, so run chmod 600 ~/.ssh/config after creating the file). To send the signal every e.g. four minutes (240 seconds) to the remote host, put the following in that configuration file:

Host remotehost
    HostName remotehost.com
    ServerAliveInterval 240

To enable sending a keep-alive signal for all hosts, place the following contents in the configuration file:

Host *
    ServerAliveInterval 240
9
  • 2
    just in case it's not obvious this config file is on your PC/linux box
    – zzapper
    Oct 23, 2018 at 15:00
  • 38
    I always get confused about the config name: the client uses ServerAliveInterval while the server uses ClientAliveInterval . What a mess.
    – youkaichao
    Feb 11, 2019 at 13:42
  • 3
    @youkaichao There is often this confusion. As an API designer, there are reasons that I might put it in both ways. Ultimately, what's most critical, is good documentation of the chosen standard. Oct 17, 2019 at 1:33
  • 2
    c:/users/youruser/.ssh/config for Windows May 22, 2020 at 11:36
  • 5
    @youkaichao It's like if you went to Carl's house and you saw "Mike's address: <address>" on a slip of paper on the fridge. It's not confused that Mike's name is written in Carl's house; Carl is the only name that doesn't need to appear at Carl's house. Mar 5, 2021 at 19:49
303

I wanted a one-time solution:

ssh -o ServerAliveInterval=60 [email protected]

Stored it in an alias:

alias sshprod='ssh -v -o ServerAliveInterval=60 [email protected]'

Now can connect like this:

me@MyMachine:~$ sshprod
8
  • 17
    That's cool, but not exactly a "one-time solution". If you're going to connect to the server more than once, why not do Host * & ServerAliveInterval 240 (or specify the hostname, if you only want it for [email protected] as in rockymonkey555's answer? Setting an alias doesn't seem easier in any way.
    – Lambart
    Jul 16, 2015 at 1:00
  • 10
    Or, just create an alias in your ~/.bashrc: alias ssh='ssh -o ServerAliveInterval=60'
    – Jabba
    Mar 17, 2016 at 5:34
  • 29
    He means "one time" as in "it only works one time", not "I only have to write it one time". Nov 30, 2017 at 22:12
  • 3
    Yes, if you read "one time" as "single use", this does the job perfectly.
    – philraj
    Aug 21, 2019 at 19:45
  • 7
    If you want a "one-liner" that sets ServerAliveInterval to 60, great. However using a bash alias when you could be using .ssh/config is just silly. Oct 17, 2019 at 1:35
82

For those wondering, @edward-coast

If you want to set the keep alive for the server, add this to /etc/ssh/sshd_config:

ClientAliveInterval 60
ClientAliveCountMax 2

ClientAliveInterval: Sets a timeout interval in seconds after which if no data has been received from the client, sshd(8) will send a message through the encrypted channel to request a response from the client.

ClientAliveCountMax: Sets the number of client alive messages (see below) which may be sent without sshd(8) receiving any messages back from the client. If this threshold is reached while client alive messages are being sent, sshd will disconnect the client, terminating the session.

5
  • It seems that there is no such "parameter" as ClientAliveInterval, only ServerAliveInterval, you can check in the manual page "man ssh_config"
    – dtj
    Jul 28, 2016 at 19:16
  • 11
    That is the wrong one, look in "man sshd_config" for the server portion running the ssh daemon, not the client config. Jul 29, 2016 at 22:35
  • Should I use ClientAliveInterval to let the server check for client alive, or should i let the client "ping" the server with ServerAliveInterval repeatedly? Both seems not to make sense
    – droid192
    Jun 2, 2017 at 14:08
  • 3
    Only set the ClientAliveInterval on the server if you want the server to disconnect on dead connections that do not respond, and you can customize how often and when that happens. Jul 25, 2017 at 20:22
  • 1
    In my case, server side set ClientAliveInterval 15 and ClientAliveCountMax 2 , and client side set -o reconnect , works well when transferring large file through sshfs without stuck.
    – allenyllee
    Jun 23, 2021 at 6:29
37

putty settings

FYI Putty Users can set the options here

17

We can keep our ssh connection alive by having following Global configurations

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

ServerAliveInterval 60
2
  • Does that only work for the client or does it also work for sshd? Apr 12, 2016 at 12:15
  • 2
    @EdwardCoast It works on Client machine. I tested it on Mac
    – minhas23
    Nov 21, 2016 at 12:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.