How to prevent SSH from disconnecting if it's been idle for a while

56

23

I have a ssh connection to a machine which gets disconnected by that machine after 30 minutes of no user input. However, if I start something like top, the connection stays alive. Since this is a client's machine, I can not reconfigure that machine's SSH server. So I am looking for a way to automatically detect idleness and start something like top. Kind of a "screensaver" for Bash.

I know that I can do that with screen, but unfortunately screen is not installed, and I can not install software. So I need to use what Bash offers.

To make it clear: I am looking for a solution that I start once after logging in, and then I want to use that terminal, walk away, come back two hours later and continue working, without typing anything before walking away. Also, I am not looking to tunnel stuff (for that I recommend the great tool sshuttle)

Any ideas?

Isaac

Posted 2014-01-10T07:52:52.567

Reputation: 944

Possible duplicate of How to reliably keep an SSH tunnel open?

– Murmel – 2018-01-15T22:30:38.597

Just a quick note to make clear screen and Keepalive are not the same thing: for instance, if either ssh client or server gest disconnected, your ssh session will be terminated even if you have KeepAlive on, while a screen shell wouldn't be terminated. – MariusMatutiae – 2014-01-10T11:50:24.710

Answers

88

To make it clear: I am looking for a solution that I start once after logging in, and then I want to use that terminal, walk away, come back two hours later and continue working, without typing anything before walking away.

The problem is that there is something (usually a firewall or load-balancer), which is dropping idle sessions. If you configure session keepalives, the keepalives will prevent network devices from considering the session as idle.

Linux / Unix / Cygwin OpenSSH fix:
The simplest fix is to enable ssh client keepalives; this example will send an ssh keepalive every 60 seconds:

ssh -o "ServerAliveInterval 60" <SERVER_ADDRESS>

If you want to enable this on all your sessions, put this in your /etc/ssh/ssh_config or ~/.ssh/config:

ServerAliveInterval 60

For more information, see the ssh_config manpage

Putty Fix:

Save this to your PuTTY "Default Settings"...

  • Click on Connection
  • Type 60 into "Seconds between keepalives"

putty_screenshot

Mike Pennington

Posted 2014-01-10T07:52:52.567

Reputation: 2 273

26

In addition to Mike Pennigton's answer, I would like to make you aware of ServerAliveCountMax too.

  • The ServerAliveInterval will send a keepalive every x seconds (default is 0, which disables this feature if not set to something else).
  • This will be done ServerAliveCountMax times if no response is received. The default value of ServerAliveCountMax is 3 (see manpage ssh_config).

Example: If you set ServerAliveInterval to 60 and leave ServerAliveCountMax as it is, this means the keepalive will only wait for 3 * 60 = 180 seconds = 3 minutes before quiting.

To increase this to e.g. 2 hours of trying to keep the connection alive, you can do:

Per command:

Therefore you should consider to set

ssh -o "ServerAliveInterval 60" -o "ServerAliveCountMax 120" <SERVER_ADDRESS>

Persistent:

To make it persistent write it to /etc/ssh/ssh_config (will apply system-wide) or ~/.ssh/config (will apply user-only):

ServerAliveInterval 60
ServerAliveCountMax 120

Note

As dislick correctly pointed out, this might not what you want, depending on your situation:

  • If you would like to quickly terminate the session as soon as the server does not respond anymore, you should choose a low value for ServerAliveCountMax.
  • If you are more interested in keeping an already established connection (e.g. you go by train and have a high latency), you should choose a higher value for ServerAliveCountMax to allow ssh to keep trying to reestablish the connection.

See also:

Murmel

Posted 2014-01-10T07:52:52.567

Reputation: 555

2

I'm sorry, but this is wrong. ServerAliveCountMax specifies the amount of server alive messages which may be sent without receiving any messages back from the server. If you want ssh to exit after it freezes (so that you can restart it) you should actually set ServerAliveCountMax to a low number. See the manpage OP linked.

– dislick – 2019-03-22T09:09:00.853

1@dislick I had to think a little bit about this, but I think you are right depending on the context, hence I added a note to emphasize the context. – Murmel – 2019-09-23T11:57:46.500

4

I am using Mobaxterm and have also met with this problem. Mobaxterm also ships with an option to keep the client alive when the client is idle. Go to Settings -> Configuration -> SSH. There is section titled SSH settings, check the option SSH keepalive. Then it the problem should disappear.

enter image description here

jdhao

Posted 2014-01-10T07:52:52.567

Reputation: 230

does the "SSH keepalive" work in free edition... my session is getting disconnected even after checking up this option – samshers – 2019-08-04T11:02:55.180

Yes, it works. Your issue may have other causes. – jdhao – 2019-08-05T07:12:16.170