As we all know a port is only used by one process or service at one time. Port identifies a particular service or a process running on the system. Sometimes, while troubleshooting we need to know which port number a certain process is listening to. All processes running on a system are associated with a process identification number (PID) and a port number. In order to find which port number a particular process is listening on, there are few ways which we will talk about in this article.
Before proceeding towards the article, make sure you have root privileges. It will help you to obtain comprehensive information about the processes running on your system.
Note that We have used Debian 10 for running the commands and procedures mentioned in this article.
Method 1: Using the netstat command
Netstat is a most commonly used command-line utility that can be used to display information about network connections, interface statistics, and routing tables. It can also be used to find which port number is used by a certain process. You do not have to install it as it is already installed in the repositories of all Linux distributions. However, in case it is not already installed on your system, use the following command to install it:
$ sudo apt install net-tools
In order to find the port numbers which are being listened on by the processes, run the following command in Terminal:
$ sudo netstat -ltnp
The following output shows the port numbers which are used by certain processes along with their process IDs (PID).
If you do not have sudo privileges and run the above command without sudo, it will do not display the program name and PID as seen in the following output.
Now Let’s see what the ltnp in the above command means:
l – show listening sockets
t – show TCP connections
n – show IP addresses & port numbers in a numerical form
p – show PID/program name
If we look at the output of $ sudo netstat -ltnp, the fourth column is exactly what we are looking for: the port number on which a process is listening.
In order to obtain port information of a single process, you can simply pipe the output of netstat with the grep command.
For instance, to find the port number against the “sshd”, use the following command:
$ sudo netstat –ltnp | grep ‘sshd’
Similarly, if you want to find out the process name which is listening on a specific port, let’s say port 21, the following command will be used:
$ sudo netstat -ltnp | grep -w ':21'
Method 2: Using the lsof command
With the lsof command, you can view the list of all files open by the processes running on your system. Lsof can act as a single source for obtaining information which otherwise involves a large set of administration tools. Similar to netstat command, you will require sudo privileges in order to obtain detailed information.
If lsof is not already installed on your system, use the following command in Terminal to install it:
$ sudo apt install lsof
Once installed, you can use the lsof utility for finding processes running on specific ports. If you run the lsof utility without any parameters, it will return a lot of information that will be difficult for you to understand. Using parameters with lsof however, can help you to filter out and concentrate on the desired output.
Now in order to find the process listening on a specific port, let’s say port 22, use the following command:
$ sudo lsof -i :22
This command will return all processes running on port 22.
Method 3: Using the fuser command
The fuser is a Linux command that is used to find which process ID is using a file, directory or file systems. We can use this command to find the process running on a specific port.
You will require psmisc utility for using fuser command. If it is not already installed on your system, run the following command to install it:
$ sudo apt install psmisc
In order to view the processes running on any port let’s say TCP port 22, run the following command in Terminal:
$ sudo fuser 22/tcp
The above command has returned the process listening on port number 22. Now in order to view the process name against any process ID, use the following command syntax:
$ ps -p [processID] -o comm=
In our case, it would be
$ ps -p  -o comm=
From the above output, we can have found the process name sshd against the process ID 5859. It implies that the sshd with the process ID 5859 is listening on port 22.
In this article, we have learned some command-line utilities that you can use in order to view which ports a particular process is listening on.