Count TCP socket state and decrease TIME_WAIT state

To count socket states use following command:

# netstat -an | awk '/^tcp/ {A[$(NF)]++} END {for (I in A) {printf "%5d %s\n", A[I], I}}'

Output should be something like this:

   11 LISTEN
  28 TIME_WAIT
  4 ESTABLISHED

If you see a lot of connections in TIME_WAIT status you can force kernel to reuse TIME_WAIT buckets with:

echo 1 > /proc/sys/net/ipv4/tcp_tw_recycle

or add to /etc/sysctl.conf

net.ipv4.tcp_tw_recycle = 1

afterwards do sysctl -p to activate changes.

Leave a Reply

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