An SSH session that hangs forever after the network went away is one of my least favourite terminal moments. By default the ssh client doesn’t check whether the server is still there while nothing is happening. Two lines in your config change that.
Host *
ServerAliveInterval 15
ServerAliveCountMax 3
Put this in ~/.ssh/config. When ssh hasn’t received anything from the server for 15 seconds, it sends a small message through the encrypted channel and asks for an answer. After 3 unanswered messages it gives up and disconnects, so a dead server is noticed after about 45 seconds and you get your prompt back. The default for ServerAliveInterval is 0, which means these messages are never sent.
The regular traffic has a side effect I like: a home router or firewall that throws away idle connections after a while sees activity, so an idle session to your NAS usually stays open too.
For a one-off, pass it on the command line:
ssh -o ServerAliveInterval=15 [email protected]
Don’t mix it up with TCPKeepAlive. That one works at the TCP level, and the man page points out that those keepalives can be spoofed. Server alive messages travel inside the encrypted connection. Also keep the numbers sensible: a very short interval with a low count means a short hiccup on a train or a flaky Wi-Fi network already kills your session. If you’d rather set it on the server, ClientAliveInterval and ClientAliveCountMax in sshd_config do the same thing from the other side.
Further reading: ssh_config(5) manual, sshd_config(5) manual