Setting up custom TCP/IP Keep Alive Parameter in Linux

Why do we use Keep Alive parameter ?

I guess the name itself explains the 90% of the definition and the rest let me try to explain you.
Now many a times most of you have been in a situation when you are using putty and running some important script or application which takes days to process and complete like 2-3 days or may be more now in that case it happens the session gets disconnected automatically in between. It can happen with two reason either the ssh session got disconnected or the machine you have logged into has stopped receiving/sending  packets from the network. Now for the 1st case like if the ssh session has disconnected you from the network then that can be fixed with the below shown link
Putty session disconnects
But in case the machine stops transmitting packets or stops responding to the network packets then that is some thing which has to be taken care of to waste your time running the script every time and making sure your machine is sending packets by doing some network activities like pinging to some machine in network.
Now that is not a solution. You can increase the Keep Alive parameter in your machine so that the machine automatically send a network packet to the network at a regular interval so that the machine does not disconnects. It uses the same technology as used in the case of heartbeat.

How do we change Keep Alive parameter in Linux?

The files responsible for TCP Keep Alive are


These already have some pre-defined default values which you check using the following

# cat /proc/sys/net/ipv4/tcp_keepalive_time
# cat /proc/sys/net/ipv4/tcp_keepalive_intvl
# cat /proc/sys/net/ipv4/tcp_keepalive_probes

The numerical value in the first 2 files are defined in seconds and the third one is just a numerical value.

  • Now the above 3 files explains you that send the first network packet(keepalive probes) after 7200 seconds and send it nine times before you get response.
  • So if your machine gets a response before completing 9 times you will be stucked to the network after which you will be disconnected.
  • The second keepalive probe will be sent after 75 seconds and the same above procedure will be followed.
  • Similarly the third probe would start after 75 seconds.

We should change these default values as per our requirement. You would not be allowed to change the values using vi editor so this is another method you can follow

# echo 300 > /proc/sys/net/ipv4/tcp_keepalive_time
# echo 60 > /proc/sys/net/ipv4/tcp_keepalive_intvl
# echo 20 > /proc/sys/net/ipv4/tcp_keepalive_probes

Here we have change these values so now the first keepalive probe will be sent after 300 seconds i.e. 5 minutes and 20 probes would be sent before the network is disconnected. And the next packet will be sent again after 60 seconds i.e. 1 minute so this can obviously help resolve my network dis connectivy problem.
Now I need to refresh these settings on my machine
To refresh the sysctl kernel parameters

# sysctl -p

To view all the kernel parameters

# sysctl -a

Reboot your machine once to take the changes affect and load your kernel with these new parameters.
Let me know your success and failure.