Ways to Find Out List of All Open Ports in Linux

17 Oct, 2021
In this article, we are going to see how to find out the list of all open ports in linux. Ports are the endpoint of communication in the computer networks, or we can also say that the port is working as the door for communication on the computer network. The ports are basically  16-bit numbers (from 0 to 65535). These ports are used to communicate by the  Internet transport protocols like the Transmission Control Protocol (TCP) and the User Datagram Protocol (UDP). 
The ports are categorized by the range of port number as follows:
From 0 to 1023: These ports are known as the Well-known ports. These ports can only be used by system (or root) processes or by programs executed by privileged users.From 1024 to 49151:These ports are known as the Registered ports. These ports can be used by ordinary user processes or programs executed by ordinary users.From 49152 to 65535:These ports are known as Dynamic Ports.
From 0 to 1023: These ports are known as the Well-known ports. These ports can only be used by system (or root) processes or by programs executed by privileged users.
From 1024 to 49151:These ports are known as the Registered ports. These ports can be used by ordinary user processes or programs executed by ordinary users.
From 49152 to 65535:These ports are known as Dynamic Ports.
We are going to see what are the by which we can find out the list of open ports in the Linux systems. But before that to get the list of all port on the system, you can use the following command:
$cat /etc/services 
Now let’s see how to find out the list of open ports in the Linux systems.
There are three ways by which we can find the list of open ports on the Linux system. Let’s see them one by one.
The netstat is a tool which give the information about the Linux networking subsystem. We use the netstat to list all open ports on the system. Use the following command to list all open ports on the system.
$ netstat -lntu
In the above command:
Option -l: list only listening sockets.
Option -n: show the port number.
Option -t: list the TCP ports.
Option -u: list the UDP ports
ss is another tool to investigate sockets. Which is the best alternative to the netstat command. So we can also use the ss tool to list the open ports on the system. Use the following command to list the all ports on the system.
$ ss -lntu
The meaning of all options used with the above command are the same as the previous netstat command.
lsof is the command which is used to list the files. We can use the lsof command to list the open ports on the system using the following command:
$ sudo lsof -i -P -n | grep LISTEN
In the above command:
Option -i: selects the listing of files, any of whose Internet address matches the address specified in i.
Option -P: inhibits the conversion of port numbers to port names for network files.
Option -n: inhibits the conversion of network numbers to host names for network files
So we have learned how to leas the all open ports in the Linux system. To know more above the above command, read the man page of the above commands.
Blogathon
Linux-Unix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.