Home  >  Article  >  Operation and Maintenance  >  How to check which port number is occupied by a program in Linux

How to check which port number is occupied by a program in Linux

尊渡假赌尊渡假赌尊渡假赌
尊渡假赌尊渡假赌尊渡假赌Original
2023-06-30 16:24:1316699browse

The way to check the port number occupied by a program in Linux is: 1. netstat command to check the network connection and monitoring status of the system; 2. lsof command to list the files and process information currently opened by the system; 3. ss command, an alternative tool to netstat, provides faster and more efficient network connection information query; 4. /proc file system, you can obtain relevant information by accessing files in these directories.

How to check which port number is occupied by a program in Linux

The operating system of this tutorial: Linux5.18.14 system, Dell G3 computer.

In the Linux system, you can use the following methods to check the port number occupied by the program:

1. netstat command:

Use netstat The command can check the network connection and monitoring status of the system. The following is an example of using the netstat command to view the port numbers occupied by a program:

  netstat -tuln | grep <进程名或PID>

This command will list all TCP and UDP connections that are listening and established, and filter out the port number information related to the specified process .

2. lsof command:

lsof (List Open Files) command can list the files and process information currently opened by the system. The following is an example of using the lsof command to view the port number occupied by a program:

  lsof -i :<端口号>
    lsof -i TCP:<端口号>
    lsof -i UDP:<端口号>

This command will list information about all processes occupying the specified port number.

3. ss command:

ss (socket statistics) is an alternative tool to netstat, providing faster and more efficient network connection information query. The following is an example of using the ss command to view the port number occupied by a program:

  ss -tuln | grep <进程名或PID>

This command will list all TCP and UDP connections that are listening and established, and filter out the port number information related to the specified process.

4. /proc file system:

In the Linux system, each running process has a corresponding directory in the /proc directory, which can be accessed through Get information about the files in the directory. The following is an example of using the /proc file system to view the port number occupied by a program:

  ls -l /proc/<PID>/fd | grep socket

where is the PID of the process. This command will list the open socket file links of the specified process, from which you can Get the port number.

The above method can help you check the port number occupied by the program, which is very useful when identifying and troubleshooting port conflicts or network connection problems.

The above is the detailed content of How to check which port number is occupied by a program in Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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 admin@php.cn