3

I'm trying to increase the SSH timeout. The command:

> ssh <host> <command>

I tried to use:

> time ssh -o ConnectionTimeout=10 <host> "sleep 20"
0.044u 0.040s 0:23.99 0.3%      0+0k 0+152io 0pf+0w

But as you can see, it does not timeout after 10 secs.

I'm using the following SSH version:

> ssh --version
ssh.exe: Reflection for Secure IT 7.2.1.99 on x86_64-suse-linux-gnu (64-bit).

How can I set a timeout in the command-line (without updating different files like /etc/ssh/sshd_config, since I don't have sudo), which increases the default timeout?

The example above was to test if the timeout works (and it didn't). I want to run a job which runs about 20min~. I believe (I might be wrong) that the default timeout kills the job and ends with:

Connection to <host> closed by remote host.

So I belive I need to increase the ssh timeout. I want to increase the ssh timeout using the command-line (since I don't have sudo, and prefer not to update any files) so it won't close the session while running.

vesii
  • 203
  • If you downvoted (it's ok!), please explain why so I could improve the question – vesii Nov 07 '21 at 09:55
  • Are you sure Connection to <host> closed by remote host. is because of a timeout? You can open a session and do nothing (or sleep for, say, 3600 seconds) to make sure that's the case. – Eduardo Trápani Nov 07 '21 at 15:53
  • The Connection to <host> closed by remote host. message indicates that the remote host closed the connection, not your ssh client. This means that there is nothing you can do with your client that would increase that timeout (if it was a timeout related to the actual SSH connection). Contact the admin of the remote host to figure out why the connection was closed. Note that this could also be due to the shell timing out, or your session reaching a resource limit, or triggering some other reason for terminating the remote shell or connection. – Kusalananda Nov 08 '21 at 09:26

1 Answers1

3

ConnectTimeout doesn't affect in any way a connection which was already established, it only configures how much ssh will wait for a remote host to respond to its trying to establish a connection in the first place.


If you want an already established ssh connection to to time out after say 2 or 10 seconds, no matter what, a stupid trick you can use is setting ServerAliveCountMax to 0, which means that the ssh client won't even care if the server is alive and responding to the client's keep-alive packets, but will just cut everything short after the first ServerAliveInterval has expired:

$ time ssh -o ServerAliveInterval=2 -o ServerAliveCountMax=0 localhost sleep 2000
Timeout, server localhost not responding.

real 0m2.248s user 0m0.049s sys 0m0.018s

This will not kill the command running on the remote server, but just the connection.


If on the contrary, you do NOT want to have your connection broken after an arbitrary interval, but the server's admin has used the same trick on the server side (i.e. by setting ClientAliveCountMax to 0 and ClientAliveInterval to e.g. 20 * 60 = 1200 seconds in sshd_config), there's not much you can do. Neither making your connection or your remote process busy nor configuring keep-alives would help with that.

If they let you run background commands on the remote machine, possible workarounds are to run your own ssh server on a port >= 1024 or run a terminal multiplexer like screen or tmux and immediately reconnect and reattach to it when the connection is broken.

  • Note that it won't kill the remote command, just the ssh connection (the command would get a SIGPIPE later on if it ever writes something to stdout or stdin though). To terminate the command after 2 seconds, it would be more like ssh host timeout 2 sleep 10 – Stéphane Chazelas Nov 07 '21 at 12:55