Linux check port occupancy command

1. lsof -i:port number

Used to check the usage of a certain port, such as checking the usage of port 5000:

sudo lsof -i:5000

Insert image description here

Note: It is best to use sudo to enable administrator rights here. If administrator rights are not enabled, related processes may not be detected.
(Not all processes can be detected, and all process information other than this user will not be displayed. If you want to see all information, you must switch to the root user)

Commonly used commands:

lsof -i:5000:查看5000端口占用
lsof abc.txt:显示开启文件abc.txt的进程
lsof -c abc:显示abc进程现在打开的文件
lsof -c -p 1234:列出进程号为1234的进程所打开的文件
lsof -g gid:显示归属gid的进程情况
lsof +d /usr/local/:显示目录下被进程开启的文件
lsof +D /usr/local/:同上,但是会搜索目录下的目录,时间较长
lsof -d 4:显示使用fd为4的进程
lsof -i -U:显示所有打开的端口和UNIX domain文件

2. netstat command

The netstat command is a very useful tool for monitoring TCP/IP networks. It can display routing tables, actual network connections, and status information for each network interface device. Each parameter of the netstat command is explained as follows:

-a : 显示所有连线中的Socket
-t : 指明显示TCP端口
-u : 指明显示UDP端口
-l : 仅显示监听套接字(所谓套接字就是使应用程序能够读写与收发通讯协议(protocol)与资料的程序)
-p : 显示进程标识符和程序名称,每一个套接字/端口都属于一个程序。
-n : 不进行DNS轮询,显示IP(可以加速操作)

Commonly used commands:

netstat -ntlp   //查看当前所有tcp端口占用情况
netstat -ntulp | grep 80   //查看所有80端口使用情况
netstat -ntulp | grep 5000   //查看5000端口使用情况
netstat -anp //查看端口的连接客户端IP
netstat -anp |grep 5000 //查看5000端口的连接客户端IP

2.1 netstat -tunlp command

(1) Check the occupancy of all ports

sudo netstat -tunlp

Insert image description here

(2) Check the occupancy of the designated port

netstat -tunlp |grep 端口号

Insert image description here

2.2 netstat -anp command

(1) Check the occupancy of all ports

sudo netstat -anp

Insert image description here

(2) Check the occupancy of the designated port

netstat -anp |grep 端口号

Insert image description here

Guess you like

Origin blog.csdn.net/u012856866/article/details/131450421