If you are from a computer science background or even a little bit familiar with networking, then you may have heard of the TCP/IP stack. The TCP/IC stack comprises of five different layers, namely, the Physical Layer, Data Link Layer, Network Layer, Transport Layer, and Application Layer. Every layer of the TCP/IP stack has a different means of communication, and all communication within the Transport Layer is done via port numbers.
A port number is used to uniquely identify a device alongside the IP address. Inter-process communication is common when using computer systems. To facilitate this communication, operating systems keep certain ports open, depending upon the entity with which the user wishes to communicate. So, at any single instance, multiple ports can be open in your system.
When we say that a port is in use, we are essentially referring to a port that is open, or, in other words, a port that is in the listening state (ready to accept connections). There are multiple ways of determining the ports that are open in an operating system. This article shows you four possible methods to use to check whether a port is in use in Linux.
Note: All the methods demonstrated in this article have been executed in Linux Mint 20.
To determine whether a port is in use in Linux Mint 20, any of the following four methods can be used.
Method 1: Using the lsof Command
The lsof command can be used to list all the ports in use in your system in the following manner:
First, launch the Linux Mint 20 terminal by clicking on its shortcut icon. The terminal is shown in the image below:
Next, you will have to install the lsof command if you have never used it before. To do so, execute the following command in the terminal:
Upon the successful installation of the command, you will see the following output in the terminal:
Once this command has been installed, it can be used for querying any ports that are in use in Linux. To check your system for open ports, execute the following command in your terminal:
In the output of this command, the ports listed in the “LISTEN” state are the ones that are in use, as shown in the image below:
Method 2: Using the ss Command
The ss command can be used to determine any open TCP and UDP ports in your system in the following manner:
To query both the TCP and UDP ports that are in use, execute the following command in the terminal:
In the output of this command, the ports (both TCP and UDP) that are in use have the “LISTEN” state, whereas all the other ports show the “UNCONN” state.
Method 3: Using the netstat Command
The netstat command can also be used to determine any open TCP and UDP ports in your system in the following manner:
To query for the TCP and UDP ports that are in use, run the following command in the terminal:
If you try to run this command without the “sudo” keyword, you will not be able to access all the ports. If you are logged in with the root user account, then you may skip this keyword.
When you run this command, you will be able to see that all ports in use are in the “LISTEN” state, whereas the states of all other ports are unavailable, as shown in the image below:
Method 4: Using the nmap Command
The nmap command is yet another utility that can be used to determine the TCP and UDP ports that are in use in the following manner:
If the nmap utility is not yet installed on your Linux Mint 20 system, as it does not come installed by default, you may have to manually install it. To do so, execute the following command:
Once you have successfully installed the nmap utility on your Linux Mint 20 system, your terminal will return you the control back so that you can execute the next command, as shown in the image below:
After installing this utility, query for both the TCP and UDP ports that are in use in your system by running the following command in the terminal:
Once you have executed this command, the state of all ports that are in use will be “open,” as shown in the output in the image below:
Conclusion
This article showed you four different methods for checking whether a port is in use in your Linux system. All of these methods were tested with Linux Mint 20, however, you can also run the commands shown in these methods with any other distribution of Linux, with slight variations. Each of the commands used in these methods takes only a few seconds to execute. So, you have the time to try any of the four methods to see which one works best for you.
from Linux Hint https://ift.tt/2ISMb6y
0 Comments