大网数据广告活动
服务器运维问答

Can TIME_WAIT port be reused for different remote server?

· 52CE 我爱测

On a machine, I found there are many TIME_WAIT connections(to different remote server) using same local port by netstat : tcp 0 0 10.32.198.120:10240 10.32.92.81:4000 TIME_WAIT tcp 0 0 10.32.198.120:10240 10.66.32.192:80 TIME_WAI…

问题原文

原帖链接 · WKPlus · CC BY-SA 3.0

On a machine, I found there are many TIME_WAIT connections(to different remote server) using same local port by netstat:

tcp        0      0 10.32.198.120:10240         10.32.92.81:4000            TIME_WAIT
tcp        0      0 10.32.198.120:10240         10.66.32.192:80             TIME_WAIT
tcp        0      0 10.32.198.120:10242         10.32.92.81:4000            TIME_WAIT
tcp        0      0 10.32.198.120:10242         10.66.32.192:80             TIME_WAIT
tcp        0      0 10.32.198.120:10243         10.32.92.81:4000            TIME_WAIT
tcp        0      0 10.32.198.120:10243         10.66.32.192:80             TIME_WAIT
tcp        0      0 10.32.198.120:10244         10.32.92.81:4000            TIME_WAIT
tcp        0      0 10.32.198.120:10244         10.66.32.192:80             TIME_WAIT
tcp        0      0 10.32.198.120:10245         10.66.32.192:80             TIME_WAIT

I knew TIME_WAIT is designed for preventing old packets from a previous connection from being accepted into a new connection. But since a new connection to a different remote server will not be mistaken as the old connection, so I think it's clearly safe to use a TIME_WAIT port for a different remote server.

So, is what I saw (same local ports in different connections) an evidence that TIME_WAIT port is reused?

I am on CentOS 6.5 (tcp_tw_reuse is set as 0) and these connections are created by python2.7.6/requests2.12.1.

社区高分解答

原帖链接 · Mark Riddell · CC BY-SA 3.0

The fact that your sockets are in TIME_WAIT doesn't really matter here, they could be in ESTABLISHED state and your question would still stand - why am I seeing the same source port for multiple connections?

TLDR: As long as the TCP 4-tuple (Source IP, Source Port, Destination IP, Destination Port) is not the same, it is permitted to use the same source port for multiple connections.

TIME-WAIT is used to prevent old packets from being accepted in a new connection using the exact same TCP 4-tuple as an old connection.


In most cases, Linux automatically assigns the source port from the Ephemeral port range which defaults to 32768 to 61000:

# cat /proc/sys/net/ipv4/ip_local_port_range
32768   60999

When deciding on which source port to use, the Kernel will first of all check if there is any source port which is unused from the ephemeral port range. With ~28k possible source ports, the Kernel is normally able to find an available port and stop there. If all 28k ports are in use however, it will next check if any of the ports pass the check_established source function which will allow an already allocated source port to be re-used as long as the new connection is for a different TCP 4-tuple.

Also, rather than letting the Kernel automatically assign a source port, it is also possible to programatically tell the Kernel exactly which source IP and port you would like to use for a specific connection.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-04-13)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。