Linux 查看端口占用情况可以使用 lsof 和 netstat 命令。
lsof
安装lsof
yum install -y lsof
使用命令lsof -i:80
查看服务器 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 的端口和进程等相关情况。
使用netstat -tunlp | grep 80
查看服务器 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相关选项
-u (udp)仅显示udp相关选项
-n 拒绝显示别名,能显示数字的全部转化为数字
-l 仅列出在Listen(监听)的服务状态
-p 显示建立相关链接的程序名