Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does linux query the port number

2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to query the port number of linux". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to query the port number of linux"!

Method: 1, use lsof to query the port number, lsof is a tool to list the current system open files, 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".

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

How does linux query the port number?

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

Lsof

Lsof (list open files) is a tool that lists files currently open by the system.

Lsof view the syntax format of port occupancy:

Lsof-I: Port number

Example

View the occupancy of server port 8000:

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

You can see that port 8000 has been occupied by light nodejs services.

Lsof-I requires the permission of the root user to execute, as shown below:

More lsof commands are as follows:

Lsof-iPlus 8080: view 8080 port occupancy

Lsof abc.txt: shows the process of opening the file abc.txt

Lsof-c abc: displays the files now opened by the abc process

Lsof-c-p 1234: lists the files opened by the process with process number 1234

Lsof-g gid: displays the process that belongs to the gid

Lsof + d / usr/local/: displays the files opened by the process in the directory

Lsof + D / usr/local/: is the same as above, but it will search the directory under the directory for a long time.

Lsof-d 4: displays processes that use fd 4

Lsof-I-U: displays all open ports and UNIX domain files

Netstat

Netstat-tunlp is used to display relevant information such as the port and process of tcp,udp.

Netstat view the syntax format of port occupancy:

Netstat-tunlp | grep port number

-t (tcp) displays only tcp related options

-u (udp) shows only udp related options

-n refuses to display aliases and can show that all numbers are converted to numbers

-l lists only the service status in Listen (snooping)

-p displays the name of the program that established the relevant link

For example, to view port 8000, use the following command:

# netstat-tunlp | grep 8000tcp 00 0.0.0.0 grep 8000tcp 8000 0.0.0.015 * LISTEN 26993/nodejs

More commands:

Netstat-ntlp / / View all current tcp ports netstat-ntulp | grep 80 / / View the usage of all 80 ports netstat-ntulp | grep 3306 / / View the usage of all 3306 ports

Kill

After finding the process occupied by the port, if you want to kill the corresponding process, you can use the kill command:

Kill-9 PID

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

Kill-9 26993 so far, I believe you have a deeper understanding of "how to query the port number of linux". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report