How to check if a port is occupied in Linux?

I have never been able to figure out whether the port is occupied before. After asking many people, I finally figured it out. Now I summarize:
1. netstat -anp | grep The port number is as follows. I took 3306 as an example, netstat -anp | grep 3306 (this As a note, I am operating as an ordinary user, so I added sudo. If I am operating as a root user, you can view it without adding sudo)
as shown in Figure 1:
Insert image description here
In Figure 1, the main monitoring status is LISTEN, which means it is occupied. Finally, One column shows that it is occupied by the service mysqld. Check the specific port number. As long as there is a line like this, it means it is occupied.
2.netstat -nultp (no need to add the port number here). This command is to check all currently used ports, as shown
Insert image description here
in Figure 2: It can be seen from the figure that my 82 port is not occupied.
3.netstat -anp | grep 82 to view The usage of port 82 is shown in Figure 3:
Insert image description here

It can be seen that there is no LISTEN line, so it means that it is not occupied. Note here that the LISTENING shown in the picture does not mean that the port is occupied. Do not confuse it with LISTEN. When viewing the specific port, you must see the tcp, port number, and LISTEN lines to indicate that the port is occupied. View it in Linux
. The port number occupied by the process can be viewed in the /etc/services file on Linux for more information about reserved ports. You can use the following six methods to view port information.
ss: Can be used to dump socket statistics.
netstat: can display a list of open sockets.
lsof: can list open files.
nmap: is a network detection tool and port scanner. systemctl: is the control manager and service manager of the systemd system. Below we will find out the port number used by the sshd daemon.
Method 1: Use the ss command
[root@elk3 ~]# ss -tlnp | grep sshd
Method 2: Use the netstat command
[root@elk3 ~]# netstat -tnlp | grep ssh
Method 3: Use the lsof command You can also use the port number to examine.
[root@elk3 ~]# netstat -lntp | grep 860
Method 4:
Use the nmap command
[root@elk3 ~]# nmap -sV -p 22 localhost
In most cases, the above output will not display the actual port number of the process. At this time, it is recommended to use the following journalctl command to check the detailed information in the log file.
[root@elk3 ~]# journalctl | grep -i ssh
December 01 21:43:36 elk3 systemd[1]: Starting OpenSSH server daemon…
December 01 21:43:36 elk3 sshd[860]: Server listening on 0.0 .0.0 port 22.
Dec 01 21:43:36 elk3 sshd[860]: Server listening on :: port 22. Dec
01 21:43:36 elk3 systemd[1]: Started OpenSSH server daemon.
Dec 01 21 :44:19 elk3 sshd[1106]: Accepted password for root from 192.168.122.1 port 49316 ssh2December 01 21:44:19 elk3 sshd[1106]: pam_unix(sshd:session): session opened for user root by (uid= 0)
This concludes this article on how to check whether a port is occupied in LINUX

Guess you like

Origin blog.csdn.net/weixin_47059371/article/details/124973682