How to check whether a port is occupied in LINUX

How to check whether a port is occupied in LINUX

I have never been able to figure out whether the port is occupied. After asking many people, I finally figured it out. Now let me summarize:

1.netstat -anp |grep port number

As follows, I take 3306 as an example, netstat -anp |grep 3306 (note here, I am operating as a normal user, so I added sudo. If you are operating as a root user, you can view it without adding sudo), as shown in Figure 1:

In Figure 1, the monitoring status is LISTEN, which means it is occupied. The last column shows that it is occupied by the service mysqld. Check the specific port number. As long as there is a line like the one in the figure, it means it is occupied.

2.netstat -nultp (no need to add port number here)

This command is used to view all currently used ports, as shown in Figure 2:

As you can see from the picture, my port 82 is not occupied.

3.netstat -anp |grep 82 to check the usage of port 82, as shown in Figure 3:

It can be seen that there is no LISTEN line, so it means that it is not occupied. Note that the LISTENING in the figure does not mean that the port is occupied. Do not confuse it with LISTEN. When checking the specific port, you must see the line of tcp, port number, and LISTEN to indicate that the port is occupied.

View the port number occupied by the process in Linux

More information about reserved ports can be found in the /etc/services file on Linux.

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.

Next we will find out the port number used by the sshd daemon.

Method 1: Using the ss command

[root@elk3 ~]# ss -tlnp | grep sshd

Method 2: Use the netstat command

[root@elk3 ~]# netstat -tnlp | grep ssh

Method 3: Using lsof command

You can also check using the port number.

[root@elk3 ~]# netstat -lntp | grep 860

Method 4: Using nmap command

[root@elk3 ~]# nmap -sV -p 22 localhost

In most cases, the above output will not show 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

Dec 01 21:43:36 elk3 systemd[1]: Starting OpenSSH server daemon...

Dec 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 ssh2

Dec 01 21:44:19 elk3 sshd[1106]: pam_unix(sshd:session): session opened for user root by (uid=0)

This is the end of this article about how to check whether a port is occupied in LINUX. For more information about LINUX port occupation, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to release port occupation in Linux
  • 6 ways to view the port numbers occupied by Linux processes
  • View the port number occupied by the process in Linux
  • Detailed example of Linux command to check port usage
  • How to find occupied ports and kill processes in Linux
  • How to check whether a port is occupied in Linux
  • Detailed explanation of Linux to check program port usage
  • How to view the process number and program name of the port occupied in Linux

<<:  Quickly learn MySQL basics

>>:  Vue.js performance optimization N tips (worth collecting)

Recommend

How to completely uninstall Docker Toolbox

Docker Toolbox is a solution for installing Docke...

Implementation of built-in modules and custom modules in Node.js

1. Commonjs Commonjs is a custom module in nodejs...

JavaScript programming through Matlab centroid algorithm positioning learning

Table of contents Matlab Centroid Algorithm As a ...

JavaScript method to detect the type of file

Table of contents 1. How to view the binary data ...

Implementation of whack-a-mole game in JavaScript

This article shares the specific code for JavaScr...

Dynamic SQL statement analysis in Mybatis

This article mainly introduces the dynamic SQL st...

Example of implementing TikTok text shaking effect with CSS

In daily development, front-end students often ar...

Kali Linux Vmware virtual machine installation (illustration and text)

Preparation: 1. Install VMware workstation softwa...

Detailed explanation of key uniqueness of v-for in Vue

Table of contents 1. DOM Diff 2. Add key attribut...

Use neat HTML markup to build your pages

The Internet is an organism that is constantly ev...

MySQL infobright installation steps

Table of contents 1. Use the "rpm -ivh insta...

Solve the problem of insufficient docker disk space

After the server where Docker is located has been...

How to install Elasticsearch7.6 cluster in docker and set password

Table of contents Some basic configuration About ...

Teach you the detailed process of installing DOClever with Docker Compose

Table of contents 1. What is Docker Compose and h...