Linux Commands

Find Port Using PID in Linux

“Sometimes, we need to know which port number a particular process listens to as Linux users. An endpoint of a communication is a type of logical system known as a port. All ports in an operating system are associated with a service/process ID.

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

sudo apt-get install net-tools

For Fedora

sudo dnf install net-tools

For Gentoo

emerge sys-apps/net-tools

For OpenSUSE

sudo zypper install net-tools

For CentOS

sudo dnf install net-tools

For Arch Linux

pacman -S netstat-nat

Once successfully installed, in Linux, enter the following command in the terminal:

sudo netstat -ltnup

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.

ss -ltnup

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

sudo apt-get install psmisc

For Gentoo

sudo emerge -a sys-apps/psmisc

For OpenSUSE

sudo zypper install psmisc

For CentOS

sudo yum install psmis

For Arch Linux

sudo pacman -S psmisc

Running the following command after installing the above utility will allow you to find the listening service/process by specifying any port.

sudo fuser 80/tcp

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.”

fuser -v 22/tcp 68/udp

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.

About the author

Prateek Jangid

A passionate Linux user for personal and professional reasons, always exploring what is new in the world of Linux and sharing with my readers.