Home  >  Article  >  Operation and Maintenance  >  How to check which ports are open in Linux

How to check which ports are open in Linux

藏色散人
藏色散人Original
2023-03-09 09:50:347276browse

How to check open ports in Linux: 1. Use the "netstat -anp" command to check which ports are open; 2. Use the "lsof -i:$PORT" command to check the program that uses the port, or check File "/etc/services", just find the service corresponding to the port from it.

How to check which ports are open in Linux

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

How to check which ports are open in Linux?

linux command to check which ports are open

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

##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

netstat -nupl | grep 3306
#这个表示查找处于监听状态的,端口号为3306的进程
Linux method to check port usage status and close port

https://blog.csdn.net/wudiyi815/article/details/7473097

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
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"

Related recommendations: "

Linux video tutorial

The above is the detailed content of How to check which ports are open 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