可以用以下两种方式来查看端口进程的占用情况
lsof -i:端口号
netstat -tunlp |grep 端口号
一、lsof -i 命令
1.1、lsof -i
lsof -i 用以显示符合条件的进程情况
lsof (list open files) 是一个列出当前系统打开文件的工具。
以root 用户来执行lsof -i 命令如下
root@iZm5eetszs07500os8erolZ:~# lsof -i
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
systemd-n 292 systemd-network 19u IPv4 13544 0t0 UDP iZm5eetszs07500os8erolZ:bootpc
chronyd 492 _chrony 1u IPv4 15657 0t0 UDP localhost:323
chronyd 492 _chrony 2u IPv6 15658 0t0 UDP localhost:323
mysqld 538 mysql 24u IPv6 16645 0t0 TCP *:mysql (LISTEN)
mysqld 538 mysql 54u IPv6 1071387 0t0 TCP iZm5eetszs07500os8erolZ:mysql->47.105.146.74:52708 (ESTABLISHED)
mysqld 538 mysql 55u IPv6 1071389 0t0 TCP iZm5eetszs07500os8erolZ:mysql->47.105.146.74:52710 (ESTABLISHED)
mysqld 538 mysql 56u IPv6 1071346 0t0 TCP iZm5eetszs07500os8erolZ:mysql->47.105.146.74:52700 (ESTABLISHED)
mysqld 538 mysql 58u IPv6 1071341 0t0 TCP iZm5eetszs07500os8erolZ:mysql->47.105.146.74:52696 (ESTABLISHED)
mysqld 538 mysql 76u IPv6 1071352 0t0 TCP iZm5eetszs07500os8erolZ:mysql->47.105.146.74:52706 (ESTABLISHED)
mysqld 538 mysql 87u IPv6 1071343 0t0 TCP iZm5eetszs07500os8erolZ:mysql->47.105.146.74:52698 (ESTABLISHED)
mysqld 538 mysql 89u IPv6 1071350 0t0 TCP iZm5eetszs07500os8erolZ:mysql->47.105.146.74:52704 (ESTABLISHED)
mysqld 538 mysql 90u IPv6 1071397 0t0 TCP iZm5eetszs07500os8erolZ:mysql->47.105.146.74:52714 (ESTABLISHED)
mysqld 538 mysql 91u IPv6 1071348 0t0 TCP iZm5eetszs07500os8erolZ:mysql->47.105.146.74:52702 (ESTABLISHED)
mysqld 538 mysql 92u IPv6 1071394 0t0 TCP iZm5eetszs07500os8erolZ:mysql->47.105.146.74:52712 (ESTABLISHED)
AliYunDun 572 root 21u IPv4 16660 0t0 TCP iZm5eetszs07500os8erolZ:41882->100.100.30.26:http (ESTABLISHED)
sshd 659 root 3u IPv4 17222 0t0 TCP *:ssh (LISTEN)
nginx 3043 root 6u IPv4 352491 0t0 TCP *:http (LISTEN)
nginx 3043 root 7u IPv6 352492 0t0 TCP *:http (LISTEN)
nginx 3043 root 11u IPv4 397090 0t0 TCP *:9090 (LISTEN)
nginx 3679 www-data 6u IPv4 352491 0t0 TCP *:http (LISTEN)
nginx 3679 www-data 7u IPv6 352492 0t0 TCP *:http (LISTEN)
nginx 3679 www-data 11u IPv4 397090 0t0 TCP *:9090 (LISTEN)
systemd-r 4875 systemd-resolve 12u IPv4 1012887 0t0 UDP localhost:domain
systemd-r 4875 systemd-resolve 13u IPv4 1012888 0t0 TCP localhost:domain (LISTEN)
java 9691 root 24u IPv4 1063446 0t0 TCP *:tproxy (LISTEN)
java 9691 root 32u IPv4 1071345 0t0 TCP iZm5eetszs07500os8erolZ:52700->47.105.146.74:mysql (ESTABLISHED)
java 9691 root 33u IPv4 1071386 0t0 TCP iZm5eetszs07500os8erolZ:52708->47.105.146.74:mysql (ESTABLISHED)
java 9691 root 36u IPv4 1071342 0t0 TCP iZm5eetszs07500os8erolZ:52698->47.105.146.74:mysql (ESTABLISHED)
java 9691 root 38u IPv4 1071351 0t0 TCP iZm5eetszs07500os8erolZ:52706->47.105.146.74:mysql (ESTABLISHED)
java 9691 root 39u IPv4 1071340 0t0 TCP iZm5eetszs07500os8erolZ:52696->47.105.146.74:mysql (ESTABLISHED)
java 9691 root 40u IPv4 1071396 0t0 TCP iZm5eetszs07500os8erolZ:52714->47.105.146.74:mysql (ESTABLISHED)
java 9691 root 41u IPv4 1071347 0t0 TCP iZm5eetszs07500os8erolZ:52702->47.105.146.74:mysql (ESTABLISHED)
java 9691 root 42u IPv4 1071388 0t0 TCP iZm5eetszs07500os8erolZ:52710->47.105.146.74:mysql (ESTABLISHED)
java 9691 root 43u IPv4 1071349 0t0 TCP iZm5eetszs07500os8erolZ:52704->47.105.146.74:mysql (ESTABLISHED)
java 9691 root 44u IPv4 1071393 0t0 TCP iZm5eetszs07500os8erolZ:52712->47.105.146.74:mysql (ESTABLISHED)
sshd 10391 root 3u IPv4 1070279 0t0 TCP iZm5eetszs07500os8erolZ:ssh->223.73.146.81:53636 (ESTABLISHED)
redis-ser 20435 redis 6u IPv6 842890 0t0 TCP *:6379 (LISTEN)
redis-ser 20435 redis 7u IPv4 842891 0t0 TCP *:6379 (LISTEN)
root@iZm5eetszs07500os8erolZ:~#
各列信息意义如下:
COMMAND :进程的名称
PID :进程标识符
USER:进程所有者
FD:文件描述符,应用程序通过文件描述识别该文件。如cwd,txt等
TYPE:文件类型,如DIR,REG 等
DEVICE:指定磁盘名称
SIZE:文件的大小
NODE:索引节点(文件在磁盘上的标识)
NAME:打开文件的确切名称
1.2、lsof -i :端口号
lsof -i :端口号,查看指定某一端口的占用情况,例如查看8081号端口使用情况:
root@iZm5eetszs07500os8erolZ:~# lsof -i :8081
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
java 9691 root 24u IPv4 1063446 0t0 TCP *:tproxy (LISTEN)
root@iZm5eetszs07500os8erolZ:~#
二、netstat -tunlp 命令
2.1、netstat -tunlp
netstat -tunlp 用于显示tcp,ucp的端口和进程等相关情况,如下:
root@iZm5eetszs07500os8erolZ:~# netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 20435/redis-server
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3043/nginx: master
tcp 0 0 0.0.0.0:8081 0.0.0.0:* LISTEN 9691/java
tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN 4875/systemd-resolv
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 659/sshd
tcp 0 0 0.0.0.0:9090 0.0.0.0:* LISTEN 3043/nginx: master
tcp6 0 0 :::3306 :::* LISTEN 538/mysqld
tcp6 0 0 :::6379 :::* LISTEN 20435/redis-server
tcp6 0 0 :::80 :::* LISTEN 3043/nginx: master
udp 0 0 127.0.0.1:323 0.0.0.0:* 492/chronyd
udp 0 0 127.0.0.53:53 0.0.0.0:* 4875/systemd-resolv
udp 0 0 172.31.105.62:68 0.0.0.0:* 292/systemd-network
udp6 0 0 ::1:323 :::* 492/chronyd
root@iZm5eetszs07500os8erolZ:~#
命令字母含义解释
-t (tcp) 仅显示tcp相关选项
-u (udp) 仅显示udp相关选项
-n 拒绝显示列名,能显示数字的全部转化为数字
-l 仅显示出在listen(监听)的服务状态
-p 显示潜力相关链接的程序名
2.2 、netstat -tunlp |grep 端口号
netstat -tunlp |grep 端口号 ,用于查看指定端口号的进程,如查看8081号端口
root@iZm5eetszs07500os8erolZ:~# netstat -tunlp |grep 8081
tcp 0 0 0.0.0.0:8081 0.0.0.0:* LISTEN 9691/java
root@iZm5eetszs07500os8erolZ:~#
grep命令参考:Linux中grep命令详解