Linux: Force Close A Socket / Port On Server In a TIME_WAIT State

by on April 9, 2013 · 3 comments· LAST UPDATED March 10, 2014

in , ,

I run a BT client and few other server program on Linux. Sometime these programs get overloaded with too many connections and crashes. If I restart my apps, I see lots of old IPs in a TIME_WAIT state. How do I force and and close everything in a TIME_WAIT state under Linux operating systems?

TIME-WAIT state can exists on either server or client program. It represents waiting for enough time to pass to be sure the remote TCP received the acknowledgment of its connection termination request.
Tutorial details
DifficultyIntermediate (rss)
Root privilegesNo
RequirementsNone
Estimated completion time5m
The /proc/sys/net/ipv4/tcp_fin_timeout setting determines the time that must elapse before TCP/IP can release a closed connection and reuse its resources. This is known as TIME_WAIT state. TIME_WAIT is a normal part of the TCP connection. However, if you must close a socket in TIME_WAIT state, try:

[a] Restart the networking service

[b] cutter command

Finding out current TIME_WAIT settings

Type the following command
$ cat /proc/sys/net/ipv4/tcp_fin_timeout
Sample outputs:

60

You can lower the value by typing the following command as root user:
# echo 20 > /proc/sys/net/ipv4/tcp_fin_timeout
To set /proc/sys/net/ipv4/tcp_fin_timeout to 20 permanently, edit the file /etc/sysctl.conf and set it as follows:

net.ipv4.tcp_fin_timeout=20

Restating the network service

To restart the network service under RHEL / CentOS based systems, enter:
# service network restart
OR
# /etc/init.d/network restart
Ubuntu / Debian Linux, user try the following command:
$ sudo service networking restart
OR
$ sudo /etc/init.d/networking restart

cutter command

Cutter is an open source program that allows Linux firewall administrators to abort TCP/IP connections routed over the firewall or router on which it is run.

Examples

To cut all connections from 192.168.1.10 to server, enter:
# cutter 192.168.1.10
To cut all ssh connection from 192.168.1.1 to server, type:
# cutter 192.168.1.1 22
To cut all http connection from 192.168.1.5 to ssh server 202.54.1.20, run:
# cutter 202.54.1.20 192.168.1.5 80
See how to install and use the cutter command for more information here.

See also

See the following man pages:
man 7 socket
man 7 tcp
man 7 ip
man 5 proc

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 3 comments… read them below or add one }

1 Lyle April 10, 2013 at 5:18 am

As far as I know, there is no way (aside from modifying the source, constant TCP_TIMEWAIT_LEN) to modify the MSL nor the TCP TIME_WAIT

You may have luck with the time_wait bucket (reuse|recycle), but it becomes tricky if your upstream is stateful and doesn’t expect the same source-port so quickly (<2*MSL)

Reply

2 Carlos Fernández San Millán March 10, 2014 at 8:33 pm

Hello!

I believe the examples have typos as the IPs do not correspond.

Best.

Reply

3 Nix Craft March 10, 2014 at 11:42 pm

Thanks for the heads up!

Reply

Leave a Comment

Tagged as: , , , , ,

Previous Faq:

Next Faq: