You need to enable JavaScript to run this app.
Elastic Compute Service

Elastic Compute Service

Copy page
Download PDF
Kernel fine-tuning
Tuning net.ipv4.tcp_max_tw_buckets parameter in Linux operating system
Copy page
Download PDF
Tuning net.ipv4.tcp_max_tw_buckets parameter in Linux operating system
This topic describes how to modify the value of the net.ipv4.tcp_max_tw_buckets parameter in the Linux operating system.​
About the net.ipv4.tcp_max_tw_buckets parameter​
This parameter specifies the maximum number of socket connections in the TIME_WAIT state that the system can maintain simultaneously. By default, its value is calculated as total memory (in MB) multiplied by 4. When the number of connections in the TIME_WAIT state within an instance exceeds the upper limit, the kernel closes the excess TCP connections and displays a warning message TCP: time wait bucket table overflow in the /var/log/messages log.​
Applicable scenario​
The number of TCP connections varies across different business scenarios. To avoid resource waste and prevent business interruptions, you can modify the value of the net.ipv4.tcp_max_tw_buckets parameter based on your actual needs.​
  • If your business involves a large number of TCP connections with many in the TIME_WAIT state, you can try increasing the value of this parameter.​
  • If your business sees an unusually high number of TCP connections in the TIME_WAIT state, which leads to slow system performance, service lag, or even business disruptions, you can try lowering the value of this parameter.​
Procedure​
Step 1: Query the current parameter value​
Execute the following command on the target Linux instance to check the current system configuration for the maximum number of TCP connections that can simultaneously be in the TIME_WAIT state.​
​
cat /proc/sys/net/ipv4/tcp_max_tw_buckets​
​
If the following output is displayed, the current maximum number of TCP connections that can simultaneously be in the TIME_WAIT state is 5000.​
​​​
Step 2: Query the configurable range of the parameter​
Execute the following command on the target Linux instance to query the range of ports that Linux randomly assigns.​
tip​
The theoretical maximum number of randomly assigned ports available externally on Linux is 65535. Excluding reserved and occupied ports, the actual available number is approximately 60000. Therefore, the value of the tcp_max_tw_buckets parameter should not exceed this available range.​
​
cat /proc/sys/net/ipv4/ip_local_port_range​
​
If the following output is displayed, the available port range is [32768, 60999]. The theoretical maximum configurable value for the net.ipv4.tcp_max_tw_buckets parameter is 60999 - 32768 + 1 = 28238.​
​​​
Step 3: Modify the parameter configuration​
Method 1: Configuration by using the proc interface​
tip​
  • This method allows configuration changes to be made and applied while the system is running. However, the configuration will reset after an operating system restart and must be reconfigured.​
  • Replace 20000 in the command with your desired maximum number of TCP connections that can simultaneously be in the TIME_WAIT state.​
  1. Execute the following command on the target Linux instance to temporarily configure the net.ipv4.tcp_max_tw_buckets parameter by using the proc interface.​
  • ​
    echo 20000 > /proc/sys/net/ipv4/tcp_max_tw_buckets​
    ​
  1. Execute the following command to view the current parameter value.​
  • ​
    cat /proc/sys/net/ipv4/tcp_max_tw_buckets​
    ​
Method 2: Configuration by using the sysctl.conf file​
tip​
  • This method allows configuration changes to be made and applied while the system is running. The configuration changes remain effective after the operating system restarts and no reconfiguration is required.​
  • Replace 20000 in the command with your desired maximum number of TCP connections that can simultaneously be in the TIME_WAIT state.​
  1. Execute the following command on the target Linux instance to open the sysctl.conf file.​
  • ​
    vim /etc/sysctl.conf​
    ​
  1. Press the i key to enter edit mode.​
  1. Modify the value of the net.ipv4.tcp_max_tw_buckets parameter in the file.​
  • ​​​
  1. Press Esc, enter :wq, and then press Enter to save and close the file.​
  1. Execute the following command to make the configuration take effect.​
  • ​
    sysctl -p​
    ​
  • ​​​
Last updated: 2025.11.03 16:31:20