how to Find the port number according to PID

The following command can be used to find out which process is listening on a particular port:

netstat -ltnp | grep -w ':8011' 

How can we do the opposite, find the port number according to PID number?

Asked By: yael


lsof is more appropriate for this:

sudo lsof -i -a -p PID

replacing PID as appropriate.

Answered By: Stephen Kitt

According to netstat manual :

This program is mostly obsolete. Replacement for netstat is ss.

So let’s use ss with its -p option then pipe to grep the desired pid.

Answered By: MC68020
Categories: Answers Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.