How does Linux check the port number occupied by the program?

  In the Linux system, if you want to check the port number occupied by the program, you can use commands, such as: netstat command, lsof command, ss command, etc. How to use it, the following is a detailed introduction.

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

  1. netstat command

  Use the netstat command to view the system's network connections and monitoring. The following is an example of using the netstat command to view the port number occupied by a program:

  netstat -tuln | grep <process name or PID>

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

  2. lsof command

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

  lsof -i :<port number>

  lsof -i TCP:<port number>

  lsof -i UDP:<port number>

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

  3. ss command

  ss 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 <process name or PID>

  This command will list all the TCP and UDP connections being monitored 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 under the /proc directory, and relevant information can be obtained by accessing files in these directories. The following is an example of using the /proc file system to view the port number occupied by a program:

  ls -l /proc//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 the port number can be obtained.

Guess you like

Origin blog.csdn.net/oldboyedu1/article/details/131852974