How to query the port number in linux

WBOY
Release: 2022-01-27 16:08:53
Original
29339 people have browsed it

Method: 1. Use lsof to query the port number. lsof is a tool that lists open files in the current system. The syntax is "lsof -i:port number"; 2. Use the netstat command to query the port number. The syntax is "netstat -tunlp | grep port number".

How to query the port number in linux

#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.

How to query the port number in Linux

Linux can use the lsof and netstat commands to check the port occupancy.

lsof

lsof (list open files) is a tool that lists open files on the current system.

lsof Check the port occupancy syntax format:

lsof -i:端口号
Copy after login

Example

Check the server 8000 port occupancy:

# lsof -i:8000
COMMAND   PID USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME
nodejs  26993 root   10u  IPv4 37999514      0t0  TCP *:8000 (LISTEN)
Copy after login

You can see that the 8000 port has been lightly used nodejs service occupied.

lsof -i requires root user permissions to execute, as shown below:

How to query the port number in linux

More lsof commands are as follows:

lsof - i:8080: Check the 8080 port occupation

lsof abc.txt: Display the process that opens the file abc.txt

lsof -c abc: Display the file currently opened by the abc process

lsof -c -p 1234: List the files opened by the process with process number 1234

lsof -g gid: Display the status of the process belonging to gid

lsof d /usr/local/ : Display the files opened by the process in the directory

lsof D /usr/local/: Same as above, but the directory under the directory will be searched, which takes a longer time

lsof -d 4: Display using fd Process for 4

lsof -i -U: Display all open ports and UNIX domain files

netstat

netstat -tunlp is used to display Tcp, udp ports and processes and other related information.

netstat View port occupancy Syntax format:

netstat -tunlp | grep 端口号
Copy after login
  • -t (tcp) Only display tcp related options

  • -u (udp) Only display udp related options

  • -n Refuse to display aliases, convert all numbers that can be displayed into numbers

  • -l List only Out of the service status of Listen (monitoring)

  • -p Display the name of the program that establishes the relevant link

For example, to view the situation of port 8000, use The following commands:

# netstat -tunlp | grep 8000
tcp        0      0 0.0.0.0:8000            0.0.0.0:*               LISTEN      26993/nodejs
Copy after login

More commands:

netstat -ntlp   //查看当前所有tcp端口
netstat -ntulp | grep 80   //查看所有80端口使用情况
netstat -ntulp | grep 3306   //查看所有3306端口使用情况
Copy after login

kill

After checking the process occupying the port, if you want to kill the corresponding The process can use the kill command:

kill -9 PID
Copy after login

As shown in the above example, we see that the PID corresponding to the 8000 port is 26993. Use the following command to kill the process:

kill -9 26993
Copy after login

Related recommendations: "Linux Video Tutorial

The above is the detailed content of How to query the port number in linux. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!