列出所有侦听端口以及关联进程的PID

・3 分钟阅读

列出所有侦听端口以及关联进程的PID

lsof -Pan -i tcp -i udp
这个命令比cousin netstat更便携,它在所有的BSD,gnu/Linux,AIX和Mac OS X上运行良好,默认情况下,在Solaris或HPUX上找不到lsof,但是如果需要,可以安装软件包,命令的工作方式如图所示,这是我能找到的最便携的命令,可以列出监听端口及它相关的pid ,
示例输出
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nmbd 1381 root 10u IPv4 37683465 0t0 UDP *:137
nmbd 1381 root 13u IPv4 37683466 0t0 UDP *:138
nmbd 1381 root 14u IPv4 37683468 0t0 UDP 192.168.1.66:137
nmbd 1381 root 15u IPv4 37683469 0t0 UDP 192.168.1.66:138
nmbd 1381 root 16u IPv4 37683470 0t0 UDP 192.168.1.6:137
nmbd 1381 root 17u IPv4 37683471 0t0 UDP 192.168.1.6:138
smbd 1384 root 25u IPv6 37683528 0t0 TCP *:445 (LISTEN)
smbd 1384 root 26u IPv6 37683530 0t0 TCP *:139 (LISTEN)
smbd 1384 root 27u IPv4 37683532 0t0 TCP *:445 (LISTEN)
smbd 1384 root 28u IPv4 37683534 0t0 TCP *:139 (LISTEN)
portmap 1642 daemon 4u IPv4 4146 0t0 UDP *:111
portmap 1642 daemon 5u IPv4 4161 0t0 TCP *:111 (LISTEN)
rpc.statd 1658 statd 4u IPv4 4186 0t0 UDP *:986
rpc.statd 1658 statd 6u IPv4 4195 0t0 UDP *:50215
rpc.statd 1658 statd 7u IPv4 4198 0t0 TCP *:37962 (LISTEN)
cupsd 1689 root 5u IPv4 51957733 0t0 TCP *:631 (LISTEN)
cupsd 1689 root 16u IPv6 51957734 0t0 TCP *:631 (LISTEN)
cupsd 1689 root 18u IPv4 51957737 0t0 UDP *:631
dhclient 2031 root 5u IPv4 5307 0t0 UDP *:68
apache2 2093 root 4u IPv6 5480 0t0 TCP *:80 (LISTEN)
avahi-dae 2281 avahi 13u IPv4 5670 0t0 UDP *:5353
avahi-dae 2281 avahi 14u IPv6 5671 0t0 UDP *:5353
avahi-dae 2281 avahi 15u IPv4 5672 0t0 UDP *:60574
avahi-dae 2281 avahi 16u IPv6 5673 0t0 UDP *:37378
hddtemp 2705 root 0u IPv4 6851 0t0 TCP 127.0.0.1:7634 (LISTEN)
dhclient 3115 root 5u IPv4 10765 0t0 UDP *:68
exim4 13674 Debian-exim 4u IPv4 45844908 0t0 TCP 127.0.0.1:25 (LISTEN)
sshd 26220 root 3u IPv4 16151998 0t0 TCP *:22 (LISTEN)
sshd 26220 root 4u IPv6 16152000 0t0 TCP *:22 (LISTEN)
apache2 30313 www-data 4u IPv6 5480 0t0 TCP *:80 (LISTEN)
apache2 30316 www-data 4u IPv6 5480 0t0 TCP *:80 (LISTEN)
Hujiuxiang profile image