141

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
  • 1,824
  • 3
  • 17
  • 23
  • 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 Jan 10 '14 at 11:50
  • Possible duplicate of [How to reliably keep an SSH tunnel open?](https://superuser.com/questions/37738/how-to-reliably-keep-an-ssh-tunnel-open) – Murmel Jan 15 '18 at 22:30

6 Answers6

163

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
  • 3,173
  • 3
  • 26
  • 35
61

In addition to Mike Pennington'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:

Mike Pennington
  • 3,173
  • 3
  • 26
  • 35
Murmel
  • 1,135
  • 10
  • 12
  • 6
    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](https://linux.die.net/man/5/ssh_config) OP linked. – dislick Mar 22 '19 at 09:09
  • 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 Sep 23 '19 at 11:57
  • @dislick it seems to me that what is posted here is quite correct. Quoting the man page: "If, for example, `ServerAliveInterval` (see below) is set to 15 and `ServerAliveCountMax` is left at the default, if the server becomes unresponsive, `ssh` will disconnect after approximately 45 seconds." – Jonathan E. Landrum Jun 09 '22 at 01:15
  • Regarding the advice to set ServerAliveCountMax to 120, this completely borks the behavior most people want… the job of keepalives is first to detect network problems within a reasonable time… if the network fails, we want to abort the ssh session… However nobody wants to wait two hours to abort a dead network. Set ServerAliveCountMax to 3 and ServerAliveInterval to 60… or something similar to detect failure as quickly as you want… I would point out that some keepalive loss is possible if the network is running at capacity… if this is you, adjust ServerAliveCountMax accordingly. – Mike Pennington Aug 27 '22 at 22:48
13

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
  • 349
  • 5
  • 28
2

I usually do:

while true; do echo -n a; sleep 10; done
0

It is never too late...

But if you only using non GUI than something better than top might be crontab.

Just create a bash script that mkdir and rm that folder each 5 minutes

or

send /dev/null to /dev/null. lol.

Hastur
  • 18,764
  • 9
  • 52
  • 95
  • How can this prevent `ssh` from disconnecting? – Kamil Maciorowski Feb 16 '21 at 09:30
  • @Noajm Have you tried if it works? Why `crontab`? You do not easily know from a script/program executed in `crontab` if a ssh session is active, and where (which tty) to send the output? There are also downsides to writing and deleting something on the disk (if you don't have a partition to write to?). BTW If it works with `top` maybe it works just running `date` every 5 minutes, but from a script ... please [edit] your post if you want to modify something. – Hastur Feb 16 '21 at 09:30
  • Try it. I use it for my collage server and works good for me. It does kick me out after 2-3 days – Noajm IsMy Name Feb 16 '21 at 09:32
  • @Noajm Hi. Some servers, for example university clusters with user and group quotas and many logins, disconnect you from an ssh session after a set amount of time, if you have not performed disk I/O operations, regardless of the connection parameters requested by your client. Others delete your data from common areas if they haven't had _recent_ access. In those cases you can think of I/O operations like `touch` to keep the session active. BTW If you have to keep an active session for 2 days I think you will find interesting [`screen`](https://www.gnu.org/software/screen/). – Hastur Feb 16 '21 at 09:50
  • A question: how did it work a script in crontab?? how did you find the session to keep alive? Can you say something more about it. Have a nice day. – Hastur Feb 16 '21 at 09:56
  • @Hastur I mean I really do not know I know basic and still learning but I know I use this server just as cloud for me so I usually just through things to into it and then I scp things when I ever want. So, long time ago they kept kicking me out every 30 minutes when I do nothing. Then I just add this "run.sh" -> "cat /dev/null > /var/mail/UserID" to run every 5 minutes using Crontab ... and then things so far good – Noajm IsMy Name Feb 16 '21 at 10:16
  • Thx for the answer. You are probably triggering some reaction with the internal email notification system. There are downsides. The first is that you go and rewrite the redirect file (`/var/mail/UserID`) every time. Specifically, you delete it (recreate it empty). And then there is the system burden for the triggered action. Finally, the additional work of the cron daemon. Try the canonical manner of the accepted answer removing your script from cron... `:-)`, it should work in other cases too... – Hastur Feb 16 '21 at 10:39
  • @Hastur, you are right about the notification.. I keep getting this prompt saying "" You have new mail!" – Noajm IsMy Name Feb 16 '21 at 11:18
  • ...each 5 minutes I suppose ... BTW read about [`screen`](https://www.gnu.org/software/screen/). You will find it really useful. Read about the possibility to **detach a session** and reconnect it (reattach) later. Give it a look on internet, maybe starting from something similar to **[>>this page<<](https://linuxize.com/post/how-to-use-linux-screen/)** and this [other one](https://askubuntu.com/questions/124897/how-do-i-detach-a-screen-session-from-a-terminal). – Hastur Feb 16 '21 at 11:27
  • @Hastur I really liked that so i can start a screen and run top on it and go back to my regular screen.... but i am having difficultly with their "commands for managing" so without GUI I will be screwed :) – Noajm IsMy Name Feb 17 '21 at 16:48
  • ""I am having Mac not sure if that why I can't apply the commands""" – Noajm IsMy Name Feb 17 '21 at 16:53
  • I tried it on the server ... haha I don't know I think I am causing a lot of disturbance when I run the "w" command...... so many pts/# taken by me – Noajm IsMy Name Feb 17 '21 at 17:04
0

How about a watch 'mkdir temp; ls -l temp; rmdir temp?