TCP keepalive is a mechanism used to determine if a network connection between two hosts is still active. It does this by periodically sending “keepalive” packets over the connection. If there’s no response after a certain number of attempts, the connection is considered dead and will be closed. This can help in detecting and cleaning up stale connections.
In this short guide, we will show you how to configure TCP keepalive settings on CentOS, RHEL, and Fedora systems. By following these steps, you can fine-tune the keepalive settings to suit your specific network requirements.
Step 1: Examine Current TCP Keepalive Settings
Before modifying the TCP keepalive settings, it’s helpful to examine the current settings. To do this, run the following commands in the terminal:
sysctl net.ipv4.tcp_keepalive_time sysctl net.ipv4.tcp_keepalive_intvl sysctl net.ipv4.tcp_keepalive_probes
These commands will display the current values for the following settings:
net.ipv4.tcp_keepalive_time: The time (in seconds) that a connection must be idle before the first keepalive packet is sent. net.ipv4.tcp_keepalive_intvl: The interval (in seconds) between keepalive packets if no response is received from the remote host. net.ipv4.tcp_keepalive_probes: The number of unacknowledged keepalive packets that must be sent before the connection is considered dead.
Step 2: Modify TCP Keepalive Settings
To modify the TCP keepalive settings, you can use the sysctl command followed by the -w flag. Here’s an example of how to set the tcp_keepalive_time to 600 seconds:
sudo sysctl -w net.ipv4.tcp_keepalive_time=600
Similarly, you can modify the other two settings as needed. For example, to set the tcp_keepalive_intvl to 60 seconds and the tcp_keepalive_probes to 5 attempts, run:
sudo sysctl -w net.ipv4.tcp_keepalive_intvl=60 sudo sysctl -w net.ipv4.tcp_keepalive_probes=5
Keep in mind that these changes will only persist until the next system reboot. To make these changes permanent, proceed to the next step.
Step 3: Make TCP Keepalive Settings Permanent
To make the TCP keepalive settings permanent, you need to add them to the /etc/sysctl.conf file. Open the file with a text editor, such as nano or vi:
sudo nano /etc/sysctl.conf
At the end of the file, add the following lines, adjusting the values to your desired settings:
net.ipv4.tcp_keepalive_time = 600 net.ipv4.tcp_keepalive_intvl = 60 net.ipv4.tcp_keepalive_probes = 5
Save the file and exit the text editor. The changes will take effect after the next system reboot.
In this guide, we have demonstrated how to configure TCP keepalive settings on CentOS, RHEL, and Fedora systems. By adjusting the keepalive time, interval, and probe settings, you can optimize your system’s ability to detect and clean up stale network connections.
Please feel free to leave comments and suggest improvements to this guide. Your feedback is valuable and helps us improve our content for our audience.