我们经常使用netstat -lnp可以查询对应端口跟进程,但是有些端口是在netstat -lnp里却没显示对应的进程pid,如果我们使用某个端口已经被占用,需要找到对应占用端口的进程关闭之类的,此时可以通过lsof来查询对应端口的对应进程pid号。
[wei@vm-cbu-qa-168-13 web-deploy]$ netstat -lnp
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp00 0.0.0.0:70080.0.0.0:* LISTEN27709/java
tcp00 0.0.0.0:62080.0.0.0:* LISTEN-
tcp00 0.0.0.0:800 0.0.0.0:* LISTEN-
tcp00 0.0.0.0:70090.0.0.0:* LISTEN27709/java
tcp00 127.0.0.1:15777 0.0.0.0:* LISTEN-
tcp00 0.0.0.0:20490.0.0.0:* LISTEN-
tcp00 127.0.0.1:15778 0.0.0.0:* LISTEN-
tcp00 0.0.0.0:35559 0.0.0.0:* LISTEN27709/java
tcp00 0.0.0.0:55336 0.0.0.0:* LISTEN27709/java
tcp00 0.0.0.0:42000.0.0.0:* LISTEN-
tcp00 0.0.0.0:58666 0.0.0.0:* LISTEN-
tcp00 0.0.0.0:845 0.0.0.0:* LISTEN-
tcp00 0.0.0.0:41100.0.0.0:* LISTEN-
tcp00 0.0.0.0:42070.0.0.0:* LISTEN-
tcp00 0.0.0.0:111 0.0.0.0:* LISTEN-
tcp00 0.0.0.0:20880 0.0.0.0:* LISTEN-
tcp00 0.0.0.0:42080.0.0.0:* LISTEN-
tcp00 0.0.0.0:18000 0.0.0.0:* LISTEN-
tcp00 0.0.0.0:18001 0.0.0.0:* LISTEN-
tcp00 0.0.0.0:18002 0.0.0.0:* LISTEN-
tcp00 0.0.0.0:42483 0.0.0.0:* LISTEN-
tcp00 0.0.0.0:48116 0.0.0.0:* LISTEN-
tcp00 10.20.168.13:8022 0.0.0.0:* LISTEN-
比如我们要查询20880端口被那个进程占用,netstat查询不出来
[wei@vm-cbu-qa-168-13 web-deploy]$ netstat -lnp | grep 20880(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
tcp00 0.0.0.0:20880 0.0.0.0:* LISTEN
我们用lsof来查询下
[wei@vm-cbu-qa-168-13 web-deploy]$ lsof -Pnl +M -i4 | grep 20880
java15239710260uIPv4 87019792 TCP 10.20.168.13:35822->10.20.172.31:20880 (ESTABLISHED)
java15239710285uIPv4 87038933 TCP 10.20.168.13:43058->10.20.172.93:20880 (ESTABLISHED)
java25755712544uIPv4 87265535 TCP *:20880 (LISTEN)
java25755712562uIPv4 87265755 TCP 10.20.168.13:40151->10.20.168.16:20880 (ESTABLISHED)
java25755712563uIPv4 87265695 TCP 10.20.168.13:59947->10.20.151.21:20880 (ESTABLISHED)
java25755712564uIPv4 87265696 TCP 10.20.168.13:47042->10.20.142.80:20880 (ESTABLISHED)
java25755712565uIPv4 87265703 TCP 10.20.168.13:40939->10.20.141.52:20880 (ESTABLISHED)
java25755712570uIPv4 87265751 TCP 10.20.168.13:43361->10.20.141.141:20880 (ESTABLISHED)
java25755712571uIPv4 87265753 TCP 10.20.168.13:40960->10.20.143.111:20880 (ESTABLISHED)
java25755712572uIPv4 87265752 TCP 10.20.168.13:45368->10.20.141.63:20880 (ESTABLISHED)
java25755712573uIPv4 87265754 TCP 10.20.168.13:34315->10.20.168.17:20880 (ESTABLISHED)
java27709713339uIPv4 87271862 TCP 10.20.168.13:50212->10.20.172.11:20880 (ESTABLISHED)
java27709713341uIPv4 87271859 TCP 10.20.168.13:41128->10.20.172.88:20880 (ESTABLISHED)
java27709713347uIPv4 87271865 TCP 10.20.168.13:46782->10.20.172.1:20880 (ESTABLISHED)
可以看到监听20880端口的是进程pid是25755,user是712。i4是查询ipv4端口
[wei@vm-cbu-qa-168-13 web-deploy]$ lsof -Pnl +M -i4
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
portmap1616 323uIPv4 3488 UDP *:111[portmapper]
portmap1616 324uIPv4 3489 TCP *:111[portmapper] (LISTEN)
DragoonAg415306uIPv4 28872832 TCP 10.20.168.13:54978->10.20.149.114:13888 (ESTABLISHED)
DragoonAg415308uIPv4 28872826 TCP 127.0.0.1:15777 (LISTEN)
DragoonAg41530 10uIPv4 28872828 TCP 127.0.0.1:15778 (LISTEN)
DragoonAg41530 12uIPv4 28872836 UDP 10.20.168.13:13777->10.20.149.114:60569
DragoonAg41530 13uIPv4 86898035 TCP 10.20.168.13:38027->10.20.159.108:15888 (ESTABLISHED)
DragoonAg41530 15uIPv4 86648332 TCP 127.0.0.1:15777->127.0.0.1:41653 (ESTABLISHED)
python1048804uIPv4 292127 TCP 10.20.168.13:8022 (LISTEN)
java152397105uIPv4 86647757 TCP *:4208 (LISTEN)
java15239710 15uIPv4 86647829 TCP *:35485 (LISTEN)
java15239710 18uIPv4 86647831 TCP *:4207 (LISTEN)
java15239710 20uIPv4 86647833 TCP *:42483 (LISTEN)
java15239710 22uIPv4 87286934 TCP 10.20.168.13:44190->110.75.194.17:80 (CLOSE_WAIT)
java15239710246uIPv4 86648330 TCP 127.0.0.1:41653->127.0.0.1:15777 (ESTABLISHED)
java15239710253uIPv4 86648976 TCP *:4110 (LISTEN)
java15239710255uIPv4 87010313 TCP 10.20.168.13:4110->10.20.172.31:38036
[wei@vm-cbu-qa-168-13 web-deploy]$ lsof -Pnl +M -i6
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
cfservd551304uIPv617813 TCP *:5308 (LISTEN)
java133340 64uIPv6 85851503 TCP 10.20.168.13:46591->10.20.145.12:57126 (ESTABLISHED)
java133340 65uIPv6 86276005 TCP 10.20.168.13:34506->10.20.172.85:80 (CLOSE_WAIT)
java133340 66uIPv6 86313523 TCP 10.20.168.13:36320->10.20.172.85:80 (CLOSE_WAIT)
java133340 67uIPv6 86382763 TCP 10.20.168.13:52680->10.20.172.85:80 (CLOSE_WAIT)
java133340 71uIPv6 85851508 TCP *:58126 (LISTEN)
lsof命令参数解释
1) -P :这个选项约束着网络文件的端口号到端口名称的转换。约束转换可以使lsof运行得更快一些。在端口名称的查找不能奏效时,这是很有用的。
2) -n : 这个选项约束着网络文件的端口号到主机名称的转换。约束转换可以使lsof的运行更快一些。在主机名称的查找不能奏效时,它非常有用。
3) -l :这个选项约束着用户ID号到登录名的转换。在登录名的查找不正确或很慢时,这个选项就很有用。
4) +M :此选项支持本地TCP和UDP端口映射程序的注册报告。
5) -i4 :仅列示IPv4协议下的端口。
6) -i6 : 仅列示IPv6协议下的端口。