Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Does anyone know if Linux supports keep-alive socket options on an outgoing connection?

I made an outgoing connection with keep-alive options but netstat --timers shows off (I'm assuming timers are off):

tcp 0 0 localhost.localdomain:44307 ESTABLISHED off (0.00/0/0)

Incoming connections with the same socket options applied show:

tcp 0 0 localhost.localdomain:21527 ESTABLISHED keepalive (29.26/0/0)

I wish I could see socket options but neither ss or lsof will show me them.

share|improve this question

First you need to make sure that TCP keepalive is enabled on your system. You can check the default settings like this:

# sysctl net.ipv4.tcp_keepalive_time net.ipv4.tcp_keepalive_probes net.ipv4.tcp_keepalive_intvl
net.ipv4.tcp_keepalive_time = 7200
net.ipv4.tcp_keepalive_probes = 9
net.ipv4.tcp_keepalive_intvl = 75

Then make sure you're setting it properly in your code. It should look something like this:

int optval = 1;
if (setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, &optval, sizeof(optval)) < 0) {

On my system when I use the above code to set SO_KEEPALIVE on both sides I see:

tcp        0      0          ESTABLISHED keepalive (6958.37/0/0)
tcp        0      0         ESTABLISHED keepalive (6958.37/0/0)

And then I verified with wireshark that the keepalive NOP was being sent.

More details can be found in the TCP Keepalive HOWTO.

share|improve this answer
Thanks for the info, but I had already had those set and I'm setting it the exact same way as you, i'm just not setting keepalive in netstat. – James Hartig Dec 13 '11 at 21:58
In that case, post more info about your system and the source code (or a simplified test case) so I can try to reproduce the problem, otherwise there's not a lot to go on here to debug. – aculich Dec 14 '11 at 11:20
@aculich is right, if you have set keepalive in the system, it's your application that doesn't use it properly. I advice to read the TCP Keepalive HOWTO as suggested by aculich or to post the source code of the application or either try to execute an lsof to see what happens – tmow Dec 16 '11 at 16:29

Your Answer


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.