How to reduce timeout for ssh when network is down

sshterminal

I'm using macbook and annoyed by ssh timeout everyday.

When my internet connection is down(by moving to conference room) or most of the times that macbook goes to sleep mode, ssh connections show timeout message Timeout, server ~~~~ not responding. after 10~15 seconds, and any key input including Ctrl+zxcd all does nothing before timeout.

How can I change this, so instead of waiting 15 seconds to re-open ssh or relaunching terminal to open another ssh connection that I can get timeout message & get disconnected after 3 seconds?

Here's my ~/.ssh/config file… yeah I've tried ServerAliveInterval but it didn't work.

Host git-codecommit.*.amazonaws.com
  User ABCDEFGHICJKLMNOPQRST
  IdentityFile ~/.ssh/id_rsa
Host *
  ServerAliveInterval 3
  ConnectTimeout 3
  ConnectionAttempts 1

Also my /etc/ssh/ssh_config is modified that it contains only comments.
(Because of bash: warning: setlocale: LC_CTYPE: cannot change locale (UTF-8): No such file or directory.)

To reproduce this, ssh any remote host and then turn Wifi off.

Best Answer

You need to set ServerAliveCountMax as well. The system works so that it after a timeout of "ServerAliveInterval" seconds, it sends a packet and tries for "ServerAliveCountMax" times before timing out completely.

So add:

ServerAliveCountMax 1

to the settings!

I would also recommend taking a look at mosh instead of regular SSH. This will enable you to keep the same connection to the server even between wakes, moves to the conference room, etc.