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

How to check which ports are open in Linux

青灯夜游
青灯夜游Original
2021-12-21 18:19:0748717browse

How to check open ports on Linux: 1. Use rpm to install the nmap tool, and execute the "nmap 127.0.0.1" command to view the open ports on this machine; 2. Execute the "netstat -anp" command to view the open ports. port.

How to check which ports are open in Linux

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

Linux Check which ports are open

1: The nmap tool detects open ports

nmap is a network scanning and Host detection tool. Installation of nmap is very simple as shown below for rpm installation.

[root@DB-Server Server]# rpm -ivh nmap-4.11-1.1.x86_64.rpm 
warning: nmap-4.11-1.1.x86_64.rpm: Header V3 DSA signature: NOKEY, key ID 37017186
Preparing...                ########################################### [100%]
   1:nmap                   ########################################### [100%]
[root@DB-Server Server]# rpm -ivh nmap-frontend-4.11-1.1.x86_64.rpm 
warning: nmap-frontend-4.11-1.1.x86_64.rpm: Header V3 DSA signature: NOKEY, key ID 37017186
Preparing...                ########################################### [100%]
   1:nmap-frontend          ########################################### [100%]
[root@DB-Server Server]#

Regarding the use of nmap, you can write a long and close-up article, and I will not expand it here. As shown below, nmap 127.0.0.1 checks the open ports of this machine and scans all ports. Of course, other server ports can also be scanned.

[root@DB-Server Server]# nmap 127.0.0.1
 
Starting Nmap 4.11 ( http://www.insecure.org/nmap/ ) at 2016-06-22 15:46 CST
Interesting ports on localhost.localdomain (127.0.0.1):
Not shown: 1674 closed ports
PORT     STATE SERVICE
22/tcp   open  ssh
25/tcp   open  smtp
111/tcp  open  rpcbind
631/tcp  open  ipp
1011/tcp open  unknown
3306/tcp open  mysql
 
Nmap finished: 1 IP address (1 host up) scanned in 0.089 seconds
You have new mail in /var/spool/mail/root
[root@DB-Server Server]#

How to check which ports are open in Linux

2: The netstat tool detects open ports

can be passed " netstat -anp" to see 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 two terminals can be opened , corresponding to the port number corresponding to the program one by one)

Extension: Check whether the port is open

lsof tool detects open ports

[root@DB-Server Server]# service mysql start
Starting MySQL......[  OK  ]
[root@DB-Server Server]# lsof -i:3306
COMMAND  PID  USER   FD   TYPE DEVICE SIZE NODE NAME
mysqld  7860 mysql   15u  IPv6  44714       TCP *:mysql (LISTEN)
[root@DB-Server Server]# service mysql stop
Shutting down MySQL..[  OK  ]
[root@DB-Server Server]# lsof -i:3306
[root@DB-Server Server]#

How to check which ports are open in Linux

[root@DB-Server Server]# lsof -i TCP| fgrep LISTEN
cupsd     3153    root    4u  IPv4   9115       TCP localhost.localdomain:ipp (LISTEN)
portmap   3761     rpc    4u  IPv4  10284       TCP *:sunrpc (LISTEN)
rpc.statd 3797 rpcuser    7u  IPv4  10489       TCP *:1011 (LISTEN)
sshd      4020    root    3u  IPv6  12791       TCP *:ssh (LISTEN)
sendmail  4042    root    4u  IPv4  12876       TCP localhost.localdomain:smtp (LISTEN)

Use telnet to check whether the port is open

Even if the server port is in the listening state, the firewall The port is blocked by iptables, and it is impossible to detect whether the port is open through this method.

netcat tool detects whether the port is open

[root@DB-Server ~]# nc -vv 192.168.42.128 1521
Connection to 192.168.42.128 1521 port [tcp/ncube-lm] succeeded!
[root@DB-Server ~]# nc -z 192.168.42.128 1521; echo $?
Connection to 192.168.42.128 1521 port [tcp/ncube-lm] succeeded!
0
[root@DB-Server ~]#  nc -vv 192.168.42.128 1433
nc: connect to 192.168.42.128 port 1433 (tcp) failed: No route to host

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