As Linux users, we all know about PIDs or process identifiers. A PID is a unique identifier for a specific active program running in an operating system. If you also want to know the methods to find the port using PID in Linux. In this guide, we’ll go through different approaches to see how you can find a service/process by listening to any port.”
Find Port Using PID in Linux
We have a collection of commands to find port using PID in Linux so let’s explain each one by one:
Netstat Command
Netstat Command, i.e., Network Statistics, displays information about network connections, such as interface statistics, routing tables, etc. This command is available on Linux and all Unix operating systems like OS and Windows. The netstat command was developed long ago and has not been updated since 2011 but don’t worry because it is still widely used.
There is a switch in the netstat command to display the PID (process identifier). This PID is attached to each connection, allowing you to identify port conflicts. Through this, you get to know which process listens in the port.
Although the netstat command is installed by default in the Linux operating system, if it is not so, you can install the net-tool utility in different operating systems through the following commands:
For Ubuntu
For Fedora
For Gentoo
For OpenSUSE
For CentOS
For Arch Linux
Once successfully installed, in Linux, enter the following command in the terminal:
That’s how you will get a list as output where you can find the TCP port and note down the corresponding PID number.
The flags used in the command used above are as follows:
l | To show this and only listening sockets |
t | To display TCP connections |
n | To instruct to show numeric addresses |
p | To show process name and process id |
SS Command
The netstat command is deprecated by some Linux distros and phased out in favor of more modern replacements such as the ss command.
Use the following command to listen to the process on any port with the help of the ss command.
We find that these two options are the same when comparing the ss command with the netstat command. We filter the output using the ss utility’s stat filter instead of the grep process.
Fuser Command
The fuser command displays the PIDs of processes using the file system, sockets, specified files, or named files in Linux. To install it on different operating systems of your Linux, follow the following commands.
For Ubuntu
For Gentoo
For OpenSUSE
For CentOS
For Arch Linux
Running the following command after installing the above utility will allow you to find the listening service/process by specifying any port.
As you can see, the output from the above command is straightforward. It does not give us detailed information about the process. You can thoroughly understand the process by passing the option “-v.”
Conclusion
Three different commands can evaluate finding a port using PID in Linux. We have explained everything about these commands so that you can use any of them as per your requirements. Many users prefer the ss command over netstat but remember that it is a classic and all-time best command.