Stop TIME_WAIT from hogging your server’s connections

Performance Tuning & Kernel Parameters (Sysctl)

Stop TIME_WAIT from hogging your server’s connections

🧩 The Challenge

You’re running a busy web server, maybe a database or a reverse proxy, and clients start seeing “connection refused” errors under load. Your logs show nothing obvious, CPU isn’t maxed, memory’s fine. You run `netstat -nat | grep TIME_WAIT` and it’s a sea of thousands of connections, just sitting there, eating up available ports. It’s a frustrating bottleneck that’ll ruin your day.

💡 The Fix

This is a classic kernel parameter adjustment. You can tell the kernel to be more aggressive about reusing these “waiting” sockets and speed up their ultimate cleanup. It’ll free up those precious port numbers and keep your services responsive.

sysctl -w net.ipv4.tcp_tw_reuse=1
sysctl -w net.ipv4.tcp_timestamps=1
sysctl -w net.ipv4.tcp_fin_timeout=30
# For persistence, add these three lines to a file like /etc/sysctl.d/99-custom-net.conf:
# net.ipv4.tcp_tw_reuse=1
# net.ipv4.tcp_timestamps=1
# net.ipv4.tcp_fin_timeout=30
# Then load them with: sysctl -p /etc/sysctl.d/99-custom-net.conf

⚙️ Why It Works

`tcp_timestamps` enables the mechanism for `tcp_tw_reuse` to work, letting the kernel recycle TIME_WAIT sockets for new outgoing connections. And `tcp_fin_timeout` shortens how long connections linger in the FIN-WAIT-2 state before being fully closed, speeding up their cleanup.

🚀 Pro-Tip: Never use `tcp_tw_recycle` with NAT – you’ll break client connections. Seriously, just don’t.

Linux Tips & Tricks | © ngelinux.com | 11/17/2026

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted