Check the Linux port occupancy status

Linux view the port occupancy can use lsof and netsta t command or use iis7 server monitoring tool .
lsof

lsof (list open files) list the current system is a tool to open the file.

lsof view the port occupied syntax:

lsof -i: port number

Examples

View occupancy server port 8000:

lsof -i:8000

COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nodejs 26993 root 10u IPv4 37999514 0t0 TCP *:8000 (LISTEN)

You can see port 8000 is already in use light nodejs service.

lsof -i root privileges required to execute the user, as shown below:

More lsof command as follows:

lsof -i: 8080: 8080 View port occupied
lsof abc.txt: displays the process open files abc.txt of
lsof -c abc: abc display process is now open files
lsof -c -p 1234: 1234 lists the process ID for the process of the open files
lsof -g gid: gid ownership of the process display case
lsof + d / usr / local / : the process open files in the directory is displayed
above, but will search the directory under the directory: lsof + d / usr / local / , longer
lsof -d 4: 4 is displayed using fd for the process of
lsof -i -U: displays all open ports and UNIX domain files

netstat

netstat -tunlp used to display the relevant circumstances tcp, udp port, and processes.

netstat view the port occupied syntax:

netstat -tunlp | grep port number

-t (tcp) 仅显示tcp相关选项
-u (udp)仅显示udp相关选项
-n 拒绝显示别名,能显示数字的全部转化为数字
-l 仅列出在Listen(监听)的服务状态
-p 显示建立相关链接的程序名

Such as viewing port 8000 case, use the following command:

netstat -tunlp | grep 8000

tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 26993/nodejs

More command:

netstat -ntlp // View all current tcp port
netstat -ntulp | grep 80 // View all 80 ports use
netstat -ntulp | grep 3306 // View all 3306 port usage

kill

After the occupation of the port process found, if you want to kill the corresponding process can use the kill command:

kill -9 PID

As an example, we see that port 8000 corresponding PID is 26993, use the following command to kill the process:

kill -9 26993

Guess you like

Origin blog.51cto.com/14479189/2425938