使用命令
netstat -tuplen-t tcp连接 -u udp连接 -p, –programs display PID/Program name for sockets -l, –listening display listening server sockets -e, –extend display other/more information -n, –numeric don’t resolve names
netstat status: - time_wait tcp发起断连方,socket会进入该状态,不能被回收使用。该状态是用于保证新分配的socket不受之前延迟重发报文的影响。可通过调整/etc/sysctl.conf中的net.ipv4.tcp_fin_timeout来调整。 - time_close
列出文件
lsof -i:xxxx列出关联相关端口的文件
time_wait 状态的影响:
- TCP 连接中,「主动发起关闭连接」的一端,会进入 time_wait 状态
- time_wait 状态,默认会持续 2 MSL(报文的最大生存时间),一般是 2x2 mins
- time_wait 状态下,TCP 连接占用的端口,无法被再次使用
- TCP 端口数量,上限是 6.5w(65535,16 bit)
- 大量 time_wait 状态存在,会导致新建 TCP 连接会出错,address already in use : connect 异常
机器上有很多time_wait状态的连接,怎么办?
- 允许 time_wait 状态的 socket 被重用
- 缩减 time_wait 时间,设置为 1 MSL (MSL is the maximum time a TCP segment is expected to live, or stay in the network. The original TCP specification, RFC 793, defines this as 2 minutes)