How to use the `lsof` command to find Open Ports in Linux In Linux
lsof is the command that 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.
Ways to Find Out List of All Open Ports in Linux
In this guide, we’ll explore how to identify the comprehensive list of open ports in Linux, crucial endpoints for communication within computer networks. Ports, serving as gateways for network communication, are represented by 16-bit numbers ranging from 0 to 65535. These ports play a pivotal role in facilitating communication through Internet transport protocols, including the Transmission Control Protocol (TCP) and the User Datagram Protocol (UDP).
The ports are categorized by the range of port numbers as follows:
- From 0 to 1023: These ports are known as 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.
Before we learn how to find the list of open ports in the Linux system, we must look at how we can get the list of all ports on the system by using the command mentioned below:
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.