Home >Operation and Maintenance >Linux Operation and Maintenance >How to check programs and listening ports in Linux

How to check programs and listening ports in Linux

青灯夜游
青灯夜游Original
2023-02-09 12:53:073802browse

Checking method: 1. Use the lsof command to check, the syntax is "sudo lsof -i -P -n | grep LISTEN" or "doas lsof -i -P -n | grep LISTEN"; 2. Use the netstat command To check the listening port and program, the syntax is "netstat -tulpn | grep LISTEN"; 3. Use the nmap command to check, the syntax is "sudo nmap -sT -O localhost".

How to check programs and listening ports in Linux

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

How to check the programs and listening ports in Linux

1. Open a terminal, such as a shell command window.

2. Run any of the following commands:

sudo lsof -i -P -n | grep LISTEN
sudo netstat -tulpn | grep LISTEN
sudo nmap -sTU -O IP地址

Method 1: lsof command

The syntax is as follows:

$ sudo lsof -i -P -n
$ sudo lsof -i -P -n | grep LISTEN
$ doas lsof -i -P -n | grep LISTEN  ### OpenBSD

The output is as follows :

How to check programs and listening ports in Linux

Look carefully at the last line of the above output:

sshd    85379     root    3u  IPv4 0xffff80000039e000      0t0  TCP 10.86.128.138:22 (LISTEN)
  • sshd is a program The name

  • 10.86.128.138 is the IP address of the sshd program binding (LISTEN)

  • 22 is the TCP port being used (LISTEN)

  • ##85379 is the process ID (PID) of the sshd task

Method 2: netstat command

You can use netstat as shown below to check the listening ports and programs.

Netstat syntax in Linux
$ netstat -tulpn | grep LISTEN
netstat syntax in FreeBSD/MacOS X
$ netstat -anp tcp | grep LISTEN
$ netstat -anp udp | grep LISTEN
netstat syntax in OpenBSD
$ netstat -na -f inet | grep LISTEN
$ netstat -nat | grep LISTEN
This command can also be used in windows :
netstat -bano | more
netstat -bano | grep LISTENING
netstat -bano | findstr /R /C:"[LISTING]"

Method 3: nmap command

The syntax is as follows:

$ sudo nmap -sT -O localhost
$ sudo nmap -sU -O 192.168.2.13 ### 列出打开的 UDP 端口
$ sudo nmap -sT -O 192.168.2.13 ### 列出打开的 TCP 端口
The sample output is as follows:

How to check programs and listening ports in Linux

You can merge TCP/UDP scanning with one command:

$ sudo nmap -sTU -O 192.168.2.13
Related recommendations: "

Linux Video Tutorial"

The above is the detailed content of How to check programs and listening ports 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