As we all know, a port is only used by one process or service at a time. A port identifies a specific service or process running on the system. Sometimes, when troubleshooting, we need to know which port number a particular process is listening on. All processes running on the system are associated with a process identification number (PID) and port number. To find out which port number a particular process is listening on, there are several ways that we will talk about in this article.
Make sure you have root privileges before starting the article. This will help you get comprehensive information about the processes running on your system.
Please note that we used Debian 10 to execute the commands and procedures mentioned in this article.
Method 1: Using the netstat command
Netstat is the 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 out which port number is being used by a particular process. There is no need to install it, as it is already installed in the repositories of all Linux distributions. However, if it is not already installed on your system, use the following command to install it:
$ sudo apt install net-tools
To find the port numbers that processes are listening on, run the following command in Terminal:
$ sudo netstat -ltnp
The following output shows the port numbers that are used by specific processes as well as their process identifiers (PIDs).
If you do not have sudo rights and you run the above command without sudo, it will not display the program name and PID as shown in the following output.
Now let’s see what ltnp in the above command means:
l – show sockets for listening
t – show TCP connections.
n – show IP addresses and port numbers in numeric 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 the process is listening.
To get the port information of a single process, you can simply pipe the netstat output using the grep command.
For example, to find the port number via sshd, use the following command:
$ sudo netstat –ltnp | grep ‘sshd’
Likewise, if you want to find out the name of the process that is listening on a specific port, 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 a list of all files opened by the processes running on your system. Lsof can act as a single source for information that would otherwise require a large set of administration tools. As with the netstat command, you need sudo privileges for details.
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 to find processes running on specific ports. If you run lsof without any parameters, it will return a lot of information that will be difficult for you to understand. However, using options with lsof can help you filter and focus on the output you want.
Now, to find the process listening on a specific port, 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
Fuser is a Linux command that is used to determine the ID of a process using a file, directory, or filesystem. We can use this command to find a process running on a specific port.
To use the fuser command, you need the psmisc utility. If it is not already installed on your system, run the following command to install it:
$ sudo apt install psmisc
To view the processes running on any port, say TCP port 22, run the following command in Terminal:
$ sudo fuser 22/tcp
The above command returned a process listening on port number 22. Now, to view the process name by 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 find the sshd process name by process id 5859. This means sshd with process id 5859 is listening on port 22.
In this article, we learned about some command line utilities that can be used to view which ports a particular process is listening on.
Debian: find out what port number a process is listening on