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
/proc/sys/net/ipv4/tcp_keepalive_time
/proc/sys/net/ipv4/tcp_keepalive_intvl
/proc/sys/net/ipv4/tcp_keepalive_probes
These already have some pre-defined default values which you check using the following
# cat /proc/sys/net/ipv4/tcp_keepalive_time
7200
# cat /proc/sys/net/ipv4/tcp_keepalive_intvl
75
# cat /proc/sys/net/ipv4/tcp_keepalive_probes
9
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.
Do you really need to refresh with sysctl -p and restart your computer for the changes to take effect? Thanks
The settings you are playing with are kernel parameters which will only take affect if the system boots with the new parameters.
But still sysctl -p command does the same work. So in case you can not restart the machine run sysctl -p and verify if it worked by using sysctl -a which will show you the currently loaded parameters in kernel.
I changed the parameters without restarting and without doing a sysctl -p. When i view the parameters..it looks like it took the new ones.
What command are you using to view those parameters?
and by verifying I meant does it affects your work due to which you were changing those values for example "disconnecting of putty connection automatically after provided value of minutes?"
Let me give you an example
For selinux even if you disable the service using console it won't make any change even though you can see the service is disabled until and unless you restart your system with that new value. So the kernel has to boot with the new values.
I'm running sysctl -a to view the parameters and it shows the 1800 instead of the 7200 for tcpkeepalive.
Check whether you have properly saved the file with new values
Then run # sysctl -p to refresh the parameters
When you run that command it will throw you the new values for each parameter.
Then restart your machine and run
# sysctl -a
But that is the issue…It looks like it already took the new parameters when i do sysctl -a …since it is showing the correct value….do i still need to restart the machine ?
I have already explained myself in the above comments for your question.