Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am trying to code a shell-script that uses a ssh-connection for doing "heartbeats". I want to terminate the client- and server-side of that connection after a certain timeout (after the connection drops).

What I found so far:

  • TCPKeepAlive yes/no for ssh and sshd
  • ClientAliveCountMax for sshd
  • ClientAliveInterval for sshd
  • ServerAliveCountMax for ssh
  • ServerAliveInterval for ssh

To change "ClientAliveCountMax" I would have to modify the sshd_config on each target machine (this option is disabled by default).

So my question is - can I use "TCPKeepAlive" for my purposes, too (without changing anything else on the source/target machines)?

Target operating system is SLES11 SP2 - but I do not think that is relevant here.

share|improve this question
    
Those parameters are all meant for situations where a firewall or intermediary device along the connection will terminate the connection. Those parameters are for sending periodic data to keep the connection alive, and also for shutting the connection down when there are X outstanding replies. Can you provide a little more detail of what youre doing? Are you using the ControlMaster option and using slave connections? –  Patrick Mar 14 '12 at 17:28
    
I just want to build a means to determine, if another node is "down" using several ssh-network connections with several physical lines. I do this by just openening an ssh-session (that more or less does an endless loop). I want that session to terminate if the connections breaks. I am wondering what the interval/count for TCPKeepalive is. –  Nils Mar 14 '12 at 21:57

1 Answer 1

up vote 22 down vote accepted

You probably want to use the ServerAlive settings for this. They do not require any configuration on the server, and can be set on the command line if you wish.

ssh -o ServerAliveInterval=5 -o ServerAliveCountMax=1 $HOST

This will send a ssh keepalive message every 5 seconds, and if it comes time to send another keepalive, but a response to the last one wasnt received, then the connection is terminated.

The critical difference between ServerAliveInterval and TCPKeepAlive is the layer they operate at.

  • TCPKeepAlive operates on the TCP layer. It sends an empty TCP ACK packet. Firewalls can be configured to ignore these packets, so if you go through a firewall that drops idle connections, these may not keep the connection alive.
  • ServerAliveInterval operates on the ssh layer. It will actually send data through ssh, so the TCP packet has encrypted data in and a firewall cant tell if its a keepalive, or a legitimate packet, so these work better.
share|improve this answer
1  
I think this is the right direction. Initial tests have shown that this will work - it will terminate sending and receiving ssh/sshd subprocesses latest 5 seconds after the connection drops. I guss that TCPKeepalive simply uses the TCP-stack-defaults - so it is harder to configure, too. –  Nils Mar 15 '12 at 20:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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