Post

SSH: configuring keepalive so connections don't drop from inactivity

SSH connections drop due to inactivity when there’s no packet exchange for a period of time. To prevent this, configure keepalive on the server:

1
sudo vi /etc/ssh/sshd_config

Add or edit the lines:

1
2
3
TCPKeepAlive yes
ClientAliveInterval 30
ClientAliveCountMax 240
  • TCPKeepAlive yes → enables keepalive at the TCP level
  • ClientAliveInterval 30 → sends a keepalive packet every 30 seconds
  • ClientAliveCountMax 240 → waits up to 240 attempts before closing (240 × 30s = 2 hours)

Restart the SSH service to apply:

1
sudo systemctl restart sshd

Client-side alternative: if you don’t control the server, you can configure keepalive in your ~/.ssh/config:

1
2
3
Host *
    ServerAliveInterval 30
    ServerAliveCountMax 240

Works the same way, but configured from the client side.

This post is licensed under CC BY 4.0 by the author.