15

I am writing a Bash script that performs several commands on remote machines via SSH.

The problem is when one of those machines is unreachable, I would like the script to skip and move on instead, it stays stuck until and after a long time it gives a connection timed out.

Is there a way for setting a shorter timeout in an ssh command?

Vlastimil Burián
  • 27,586
  • 56
  • 179
  • 309
kuma
  • 275
  • 1
  • 2
  • 5

1 Answers1

25

To specify the timeout (in seconds), use the ConnectTimeout option as specified in the ssh_config manual page:

ssh -o ConnectTimeout=10 user@remotehost

To specify the timeout for all hosts, add this configuration to a wildcard stanza in your ssh config file, typically ~/.ssh/config for personal configuration, or /etc/ssh/ssh_config to apply systemwide:

Host *
    ConnectTimeout 10
user4556274
  • 8,725
  • 2
  • 31
  • 37