有时端口被占用导致服务起不来,发现本机端口被另一个程序使用了。这个时候我们可以使用tcpkill命令来删除这个长连接,让其自动重连换一个端口。然后再启动想启动的服务。

安装tcpkill:

yum -y install dsniff --enablerepo=epel

查询这个连接:

[root@sh5-saas-k8scs1-node-online-167 ~]# netstat -anp | grep 4080
tcp        0      0 10.10.182.167:4080      10.10.182.3:8443        ESTABLISHED 71932/kubelet       
unix  2      [ ACC ]     STREAM     LISTENING     2363408000 61767/containerd     /run/containerd/s/e3515d3603c24c9fdf4f6e16c493fe8953ab73538685ca8a52331356a3951649
unix  3      [ ]         STREAM     CONNECTED     2363408007 61767/containerd 

删除连接:

[root@sh5-saas-k8scs1-node-online-167 ~]# tcpkill -9 port 4080
tcpkill: listening on eth0 [port 4080]

10.10.182.167:4080 > 10.10.182.3:8443: R 1598753917:1598753917(0) win 0
10.10.182.167:4080 > 10.10.182.3:8443: R 1598754418:1598754418(0) win 0
10.10.182.167:4080 > 10.10.182.3:8443: R 1598755420:1598755420(0) win 0
10.10.182.167:4080 > 10.10.182.3:8443: R 1598756923:1598756923(0) win 0
10.10.182.167:4080 > 10.10.182.3:8443: R 1598758927:1598758927(0) win 0
10.10.182.167:4080 > 10.10.182.3:8443: R 1598761432:1598761432(0) win 0
10.10.182.167:4080 > 10.10.182.3:8443: R 1598764438:1598764438(0) win 0
10.10.182.167:4080 > 10.10.182.3:8443: R 1598767945:1598767945(0) win 0
10.10.182.167:4080 > 10.10.182.3:8443: R 1598771953:1598771953(0) win 0
10.10.182.3:8443 > 10.10.182.167:4080: R 1279911727:1279911727(0) win 0
10.10.182.3:8443 > 10.10.182.167:4080: R 1279913189:1279913189(0) win 0
10.10.182.3:8443 > 10.10.182.167:4080: R 1279916113:1279916113(0) win 0
10.10.182.3:8443 > 10.10.182.167:4080: R 1279920499:1279920499(0) win 0
10.10.182.3:8443 > 10.10.182.167:4080: R 1279926347:1279926347(0) win 0
10.10.182.3:8443 > 10.10.182.167:4080: R 1279933657:1279933657(0) win 0
10.10.182.3:8443 > 10.10.182.167:4080: R 1279942429:1279942429(0) win 0
10.10.182.3:8443 > 10.10.182.167:4080: R 1279952663:1279952663(0) win 0
10.10.182.3:8443 > 10.10.182.167:4080: R 1279964359:1279964359(0) win 0

使用帮助:

SYNOPSIS

tcpkill [-i interface] [-1...9] expression 

DESCRIPTION

tcpkill kills specified in-progress TCP connections (useful for libnids-based applications which require a full TCP 3-whs for TCB creation). 

OPTIONS

-i interface    Specify the interface to listen on.
    -1...9  Specify the degree of brute force to use in killing a connection. Fast connections may require a higher number in order to land a RST in the moving receive window. Default is 3.
    expression  Specify a tcpdump(8) filter expression to select the connections to kill. 

expression与tcpdump的语法一样。

更多参考:

http://www.irongeek.com/i.php?page=backtrack-3-man/tcpkill

https://en.wikipedia.org/wiki/Tcpkill