Home > Operation and Maintenance > Linux Operation and Maintenance > How to check which port numbers are open in Linux

How to check which port numbers are open in Linux

藏色散人
Release: 2023-03-27 10:33:06
Original
24395 people have browsed it

How to check the open port numbers in Linux: 1. Log in to the Linux system and open the terminal; 2. Use the "netstat -anp" command to check which ports are open; 3. Use "lsof -i:$PORT" Just check the program that uses this port.

How to check which port numbers are open in Linux

#The operating environment of this tutorial: linux5.9.8 system, Dell G3 computer.

How to check which port numbers are open in Linux?

linux command to check which ports are open

netstat -nupl (UDP类型的端口)netstat -ntpl (TCP类型的端口)
Copy after login

##a means all

n means not to query dns

t means tcp protocol

u means udp protocol

p means query occupied program

l means query is listening Program

#这个表示查找处于监听状态的,端口号为3306的进程
Copy after login
Copy after login
Linux method of checking port usage status and closing port

Premise: First of all, you must know that the port does not exist independently, it is attached to the process. When a process is opened, its corresponding port is opened; when the process is closed, the port is closed. If a process is opened again next time, the corresponding port will also be opened again. Don't purely understand it as closing a certain port, but you can disable a certain port.

1. You can use "netstat -anp" to check which ports are open.

(Note: Adding parameter '-n' will convert the application to port display, that is, an address in digital format, such as: nfs->2049, ftp->21, so it can be turned on Two terminals, one by one corresponding to the port number corresponding to the program)

2. Then you can use "lsof -i:$PORT" to view the program that uses the port ($PORT refers to the corresponding port Number). Or you can also check the file /etc/services to find out the service corresponding to the port.

(Note: Some ports cannot be found through netstat. A more reliable method is "sudo nmap -sT -O localhost")

3. To close a certain port port, you can:

1) Disable the port through the iptables tool, such as:

iptables -A INPUT -p tcp --dport $PORT -j DROP
iptables -A OUTPUT -p tcp --dport $PORT -j DROP
Copy after login
Copy after login
2) Or close the corresponding application , the port will be closed naturally, such as:

"kill -9 PID" (PID: process number)

For example: through "netstat -anp | grep ssh"

Display: tcp 0 127.0.0.1:2121 0.0.0.0:* LISTEN 7546/ssh

then: "kill -9 7546"

##a means all

n means no dns query

t means tcp protocol

u means udp protocol

p means query occupied Program

l means querying the listening program

#这个表示查找处于监听状态的,端口号为3306的进程
Copy after login
Copy after login
Linux method of checking port usage status and closing port

Prerequisite: First you must know the port It does not exist independently, it is dependent on the process. When a process is opened, its corresponding port is opened; when the process is closed, the port is closed. If a process is opened again next time, the corresponding port will also be opened again. Don't purely understand it as closing a certain port, but you can disable a certain port.

1. You can use "netstat -anp" to check which ports are open.

(Note: Adding parameter '-n' will convert the application to port display, that is, an address in digital format, such as: nfs->2049, ftp->21, so it can be turned on Two terminals, one by one corresponding to the port number corresponding to the program)

2. Then you can use "lsof -i:$PORT" to view the program that uses the port ($PORT refers to the corresponding port Number). Or you can also check the file /etc/services to find out the service corresponding to the port.

(Note: Some ports cannot be found through netstat. A more reliable method is "sudo nmap -sT -O localhost")

3. To close a certain port port, you can:

1) Disable the port through the iptables tool, such as:

iptables -A INPUT -p tcp --dport $PORT -j DROP
iptables -A OUTPUT -p tcp --dport $PORT -j DROP
Copy after login
Copy after login
2) Or close the corresponding application , the port will be closed naturally, such as:

"kill -9 PID" (PID: process number)

For example: through "netstat -anp | grep ssh"

Display: tcp 0 127.0.0.1:2121 0.0.0.0:* LISTEN 7546/ssh

then: "kill -9 7546"

Recommended learning: "

linux video tutorial

The above is the detailed content of How to check which port numbers are open 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 admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template