Linux Administrator

How to Configure TCP Keepalive Setting in Linux

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.

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.

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

What is TCP Keepalive Setting?

TCP will send the keepalive probe contains null data to the network peer several times after a period of idle time. If the peer does not respond, the socket will be closed automatically.

The application will then receive a notification about the socket closure, which it should handle in the correct manner.

Most of the operating systems and hosts that support TCP also support TCP Keepalive.

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

TCP keepalive process waits for two hours (7200 secs) for socket activity before sending the first keepalive probe, and then resend it every 75 seconds. As long as there is TCP/IP socket communications going on and active, no keepalive packets are needed.

Configure Linux TCP Keepalive Settings

Please note that the following tuning is for linux operating system only. This steps has been tested in CentOS 5/6/7, RHEL 5/6/7 and Oracle Linux 6/7.

Method #1:

1. Edit /etc/sysctl.conf file.

# vi /etc/sysctl.conf

Add the following setting :

net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_keepalive_intvl = 60
net.ipv4.tcp_keepalive_probes = 20

2. Run the below command to apply the changes.

# sysctl -p

Method #2:

You can 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 connectivity 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
Thank you! for visiting LookLinux.

If you find this tutorial helpful please share with your friends to keep it alive. For more helpful topic browse my website www.looklinux.com. To become an author at Look Linux Submit Article. Stay connected to Facebook.

About the author

mm

Santosh Prasad

Hi! I'm Santosh and I'm here to post some cool article for you. If you have any query and suggestion please comment in comment section.

2 Comments

  • Hi! I’ve been using a VPS server to serve files from my hosting account, and I’m having trouble connecting to my node rest API because it often returns a connection timeout error when I tried to connect remotely, is there any way to avoid these errors permanently? I can see that majority of the websites running live doesn’t have this error, I’m wondering about the implementation they’ve used, can you help me? thanks!

    The occurrence rate of this error to come out each day is 50% or less, and as the developer, it gets a bit annoying having this kind of error, SSH too is getting this timeout, as well as FTP connection, not just my HTTP service (connecting to API from my client controller)

    Your help would save me from sinking, thanks!

  • Apache client is sneding request over firewall, and if there are no requests for 30mins then connection drops. Firewall has 30secs timeout.
    How to send keep alive from Apache TCP client sothat Apache will send keepalive every 10mins and connectio nwill never drop?

Leave a Comment