brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: ServerAliveInterval lets SSH notice a dead connection

Network and access
~/tips/tip-ssh-serveraliveinterval

ServerAliveInterval 15

Makes the SSH client check the server and drop a dead connection after about 45 seconds.

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

More Network and access tips scp -3 alice@server-a:/var/backups/db.sql.gz bob@server-b:/srv/restore/ tailscale exit-node suggest tailscale lock sign nodekey:1abddef1 tlpub:abcdef12 wg genpsk > peer1.psk Table = off All Network and access tips (163) All topics

Link