So I'm working on a little SSH call back solution for one of my boxes, and I'm having a slight problem that I barely know what to even look up to figure out what's going on.

Basically, the client connects back over SSH and checks every 60 seconds if the connection is still active, using netstat. However, if the server reboots, and I check netstat, the connection still shows there. So I'm basically wanting to make sure that the client can keep an SSH connection alive (even if it's not being used) for a long time, but if the connection actually terminates from the server, I want the client to be able to drop the connection.

What options on the client, if not the server, should I be looking at to make sure stale/dead SSH connections are actually dropped while making sure that I can have a persistent SSH connection for future access?

I thought it may have been setting the ClientAliveInterval and ClientAliveCountMax, but I'm not sure if I need to set this on the client, or the server, and how the client would know it's disconnected if it's to be set on the server.

  • 1
    If you just set ClientAliveInterval to 0 on the server, don't you get the desired behaviour? Connections will be closed if the server drops/closes them, but will stay alive for as long as the client keeps them open. Read man sshd_config for descriptions on all the options for the ssh server.
    – Munir
    Nov 7, 2017 at 3:36

1 Answer 1


You can run the ssh command with options -o ServerAliveInterval=30 -o ServerAliveCountMax=3 and this will close the client if the server does not reply to 3 pings given every 30 seconds. You might also like to check out the autossh command which tries to ensure a connection is always kept open.

  • 1
    On a side note, ClientAliveInterval and ClientAliveCountMax on server side are also useful, for the same reasons. Nov 7, 2017 at 8:29
  • Thank you for this! Autossh is exactly something that I'd be looking for! Nov 8, 2017 at 21:55
  • The manual of autossh states: "For example, if you are using a recent version of OpenSSH, you may wish to explore using the ServerAliveInterval and ServerAliveCountMax options to have the SSH client exit if it finds itself no longer connected to the server. In many ways this may be a better solution than the monitoring port." Using these options, and a well-designed service file or POSIX shell while-loop, I do not see any reason to still use autossh.
    – Yeti
    Nov 6, 2021 at 13:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .