How to list down all listening ports in Linux?

To troubleshoot network connectivity or application issues, you need to check which ports the system is using and which applications are listening on that particular port.

In this article, we will see different linux commands to check the open ports in Linux to troubleshoot the issue.

1) Use netstat command to check if the port is open in Linux

The netstat command is part of the net-tools package, which is not included in the Linux distribution by default. It displays network-related data such as network connections, routing tables, interface statistics, masquerade connections, multicast memberships, and so on. On Ubuntu, you can use the following command to install the net-tools package.

# apt update -y && apt install net-tools -y

To list all TCP or UDP ports used by the system, including the service using the port and the socket status, use the following command.

# netstat -tunlp

-t -> It Shows the TCP ports.
-u -> It displays the UDP ports.
-n -> It shows the numerical addresses instead of resolving hosts.
-l -> It displays only the listening ports.
-p -> It shows the PID and the listener's process name.

Below is the description of the important columns which you should know.

Proto - The protocol that the socket uses.
Local Address - It shows the IP Address and port number the process listens to.

PID/Program name - It shows the PID and process name.



2) Use ss command to check if the port is open in Linux

The ss command is another command line utility for checking open ports. It shows socket statistics that are used to check if a port is open. The ss command shows more information about open ports than other tools.

Enter the following command to get a list of all listening ports with ss:

# ss -tunlp

3) Use lsof command to check if the port is open in Linux

The lsof command is another helpful tool for checking open ports. The lsof stands for list open files and displays information about open files on your system. This information includes file descriptors, process IDs, user IDs, and so on.

To get a list of all TCP ports that are listening, use the lsof command in the terminal.

# sudo lsof -nP -iTCP -sTCP:LISTEN

The options used are as follows:
-n -> It lists the entry which does not convert the port number to the port name.
-p -> It shows the entry which does not resolve the hostname and shows numerical addresses.
-iTCP -sTCP:LISTEN -> It shows only network files with TCP state LISTEN.

That's all.



Was this answer helpful?

« Back