Brian Dunagan

September 7 2011
Server Tip: Better SSH Timeouts

I recently had an issue with my SSH connections timing out quickly. At Starbucks, I had no issues, but at home, the connection timed out faster than usual.

First, I wanted to see how fast, so I wrote a short bash script that sleeps for increasing periods of time. I found that the connection timed out after five minutes.

let time=0
while true; do
  let time=time+10
  echo timeout is $time seconds
  sleep $time

I tried creating a ~/.ssh/config file and telling the connection to stay alive: TCPKeepAlive yes. No luck, because the default time period is two hours.

Instead, I ignored the TCP connection’s state and told the SSH client to send a null packet every twenty seconds to keep the connection alive. The SSH client times out if it doesn’t receive a server response after ten packets.

# ~/.ssh/config
TCPKeepAlive no
ServerAliveInterval 20
ServerAliveCountMax 10

Thanks to Andy Skelton for making this tidbit googleable.

Remind Me Later 1.4.1 Compiling PowerPC with LLVM GCC 4.2 in Xcode 4.1 on 10.7
LinkedIn GitHub Email