Linux 查看端口占用情況可以使用 lsof 和 netstat 命令。
lsof
安裝lsof
yum install -y lsof
使用命令lsof -i:80查看服務(wù)器 80 端口的占用情況:
[root@localhost ~]# lsof -i:80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 14059 root 6u IPv4 88876 0t0 TCP *:http (LISTEN)
nginx 14060 nginx 6u IPv4 88876 0t0 TCP *:http (LISTEN)
nginx 14061 nginx 6u IPv4 88876 0t0 TCP *:http (LISTEN)
nginx 14062 nginx 6u IPv4 88876 0t0 TCP *:http (LISTEN)
nginx 14063 nginx 6u IPv4 88876 0t0 TCP *:http (LISTEN)
netstat
安裝netstat
yum -y install net-tools
netstat -tunlp 用于顯示 tcp,udp 的端口和進(jìn)程等相關(guān)情況。
使用netstat -tunlp | grep 80查看服務(wù)器 80 端口的占用情況:
[root@localhost ~]# netstat -tunlp | grep 80
tcp 0 0 192.168.255.134:2380 0.0.0.0:* LISTEN 2166/etcd
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 14059/nginx: master
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 11321/docker-proxy
tcp6 0 0 :::8080 :::* LISTEN 11327/docker-proxy
-t (tcp) 僅顯示tcp相關(guān)選項(xiàng)
-u (udp)僅顯示udp相關(guān)選項(xiàng)
-n 拒絕顯示別名,能顯示數(shù)字的全部轉(zhuǎn)化為數(shù)字
-l 僅列出在Listen(監(jiān)聽)的服務(wù)狀態(tài)
-p 顯示建立相關(guān)鏈接的程序名