Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set SSH connection timeout

Tags:

ssh

timeout

I'm trying to cut down the time ssh is trying to open a connection to a host. If I put for example ssh www.google.com it takes very long until the prompt comes back.

I read about using ssh -o ConnectTimeout=10 www.google.com instead, but even this takes very long. Is there maybe a number of attemps I can modify to decrease the blocking time?

like image 745
multiholle Avatar asked Apr 19 '12 22:04

multiholle


People also ask

How do I set SSH connection timeout?

Use the ssh_timeout command to set the number of minutes that the SSH session can be idle before it is closed. The default timeout interval is 0 minutes. Use this value, if you do not want the SSH session to expire. The minimum timeout interval is 2 minutes.

How long is SSH timeout?

Some systems use a default as low as five seconds, and some go as high as two hours; the average is typically around three to five minutes. Your SSH connection, if it has not been modified to change the timeout on either the server or client end, will use this timeout.


1 Answers

The problem may be that ssh is trying to connect to all the different IPs that www.google.com resolves to. For example on my machine:

# ssh -v -o ConnectTimeout=1 -o ConnectionAttempts=1 www.google.com OpenSSH_5.9p1, OpenSSL 0.9.8t 18 Jan 2012 debug1: Connecting to www.google.com [173.194.43.20] port 22. debug1: connect to address 173.194.43.20 port 22: Connection timed out debug1: Connecting to www.google.com [173.194.43.19] port 22. debug1: connect to address 173.194.43.19 port 22: Connection timed out debug1: Connecting to www.google.com [173.194.43.18] port 22. debug1: connect to address 173.194.43.18 port 22: Connection timed out debug1: Connecting to www.google.com [173.194.43.17] port 22. debug1: connect to address 173.194.43.17 port 22: Connection timed out debug1: Connecting to www.google.com [173.194.43.16] port 22. debug1: connect to address 173.194.43.16 port 22: Connection timed out ssh: connect to host www.google.com port 22: Connection timed out 

If I run it with a specific IP, it returns much faster.

EDIT: I've timed it (with time) and the results are:

  • www.google.com - 5.086 seconds
  • 173.94.43.16 - 1.054 seconds
like image 181
sinelaw Avatar answered Sep 21 '22 04:09

sinelaw