How to Configure TCP keepalive Settings on Ubuntu (and other Debian-based systems)

TCP keepalive is a feature used to detect and close inactive or unresponsive connections. It works by periodically sending a small packet called a “keepalive probe” to maintain the connection. By default, TCP keepalive is enabled on most systems, but the settings can be tweaked to optimize network performance or address specific issues.

In this short guide, we will demonstrate how to configure TCP keepalive settings on Ubuntu (and other Debian-based systems). By following these steps, you will be able to customize the keepalive settings to suit your needs.

Step 1: View the Current TCP Keepalive Settings

To view the current TCP keepalive settings, open a terminal and run the following command:

sysctl net.ipv4.tcp_keepalive_time net.ipv4.tcp_keepalive_intvl net.ipv4.tcp_keepalive_probes

This command will display the current values for the following settings:

net.ipv4.tcp_keepalive_time: The interval, in seconds, between the last data packet sent and the first keepalive probe.
net.ipv4.tcp_keepalive_intvl: The interval, in seconds, between subsequent keepalive probes.
net.ipv4.tcp_keepalive_probes: The number of unacknowledged keepalive probes to send before considering the connection dead.

Step 2: Modify TCP Keepalive Settings

To modify the TCP keepalive settings, open the /etc/sysctl.conf file in your preferred text editor. For example, you can use nano:

sudo nano /etc/sysctl.conf

At the bottom of the file, add the following lines to set your desired keepalive settings:

net.ipv4.tcp_keepalive_time = 1200
net.ipv4.tcp_keepalive_intvl = 60
net.ipv4.tcp_keepalive_probes = 5

These example values set the keepalive time to 1200 seconds (20 minutes), the keepalive interval to 60 seconds, and the number of probes to 5. Adjust these values according to your specific requirements.

See also  How to Add a User to a Group on Ubuntu

Save the changes and exit the text editor.

Step 3: Apply the New TCP Keepalive Settings

To apply the new TCP keepalive settings, run the following command in the terminal:

sudo sysctl -p

This command will reload the /etc/sysctl.conf file and apply the new settings immediately.

Commands and Their Functions:

  • sysctl [options] – Displays or modifies kernel parameters at runtime.
  • sudo nano /etc/sysctl.conf – Opens the sysctl configuration file in the nano text editor with administrative privileges.
  • sudo sysctl -p – Reloads the sysctl configuration file and applies the new settings immediately.
See also  How to Disable SELinux on RHEL 7/CentOS 7/Oracle Linux 7

Conclusion

In this guide, we have shown you how to configure TCP keepalive settings on Ubuntu and other Debian-based systems. By modifying the keepalive time, interval, and probe settings, you can optimize network performance and address specific issues related to your environment.

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.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *