List ports a process PID is listening on (preferably using iproute2 tools)?

You can use ss from the iproute2 package (which is similar to netstat):

ss -l -p -n | grep "pid=1234,"

or (for older iproute2 version):

ss -l -p -n | grep ",1234,"

Replace 1234 with the PID of the program.


I am not aware of a way using iproute2 tools. But as a workaround, you could try this one out.

lsof -Pan -p PID -i

should give you the information you are looking for.


Output

lsof -Pan -p 27808 -i
COMMAND   PID   USER   FD   TYPE    DEVICE SIZE/OFF NODE NAME
httpd   27808 apache    5u  IPv6 112811294      0t0  TCP *:80 (LISTEN)
httpd   27808 apache    7u  IPv6 112811298      0t0  TCP *:8443 (LISTEN)
httpd   27808 apache    9u  IPv6 112811303      0t0  TCP *:443 (LISTEN)

I got this command from here but not sure of the exact link since I have all of them noted down in the notebook. But you could check out from there as well.


You can use netstat for this to figure out pid of each listen process.

netstat - Print network connections, routing tables, interface statistics, masquerade connections, and multicast memberships

-a, --all Show both listening and non-listening (for TCP this means established connections) sockets. With the --interfaces option, show interfaces that are not marked

--numeric , -n Show numerical addresses instead of trying to determine symbolic host, port or user names.

-p, --program Show the PID and name of the program to which each socket belongs.

Here is an example:

# netstat -anp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      1507/rpcbind
tcp        0      0 0.0.0.0:51188               0.0.0.0:*                   LISTEN      1651/rpc.statd
tcp        0      0 0.0.0.0:1013                0.0.0.0:*                   LISTEN      1680/ypbind
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1975/sshd
tcp        0      0 127.0.0.1:631               0.0.0.0:*                   LISTEN      1763/cupsd
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      2081/master
tcp        0      0 127.0.0.1:27017             0.0.0.0:*                   LISTEN      2119/mongod
tcp        0     48 172.16.33.73:22             172.16.127.110:51850        ESTABLISHED 25473/sshd
tcp        0      0 172.16.33.73:22             172.16.127.110:51214        ESTABLISHED 24699/sshd
tcp        0      0 :::111                      :::*                        LISTEN      1507/rpcbind
tcp        0      0 :::9200                     :::*                        LISTEN      1994/java
tcp        0      0 :::9300                     :::*                        LISTEN      1994/java
tcp        0      0 :::22                       :::*                        LISTEN      1975/sshd
tcp        0      0 ::1:631                     :::*                        LISTEN      1763/cupsd
tcp        0      0 ::1:25                      :::*                        LISTEN      2081/master
tcp        0      0 :::59162                    :::*                        LISTEN      1651/rpc.statd