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 '17 at 3:36

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.

| improve this answer | |
  • 1
    On a side note, ClientAliveInterval and ClientAliveCountMax on server side are also useful, for the same reasons. – Satō Katsura Nov 7 '17 at 8:29
  • Thank you for this! Autossh is exactly something that I'd be looking for! – user3447014 Nov 8 '17 at 21:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.