I'm looking to list all ports a PID is currently listening on.
How would you recommend I get this kind of data about a process?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
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.
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
@jofel's answer shows you the appropriate tool to use,
ss, here's the replacements for the other networking tools in iproute2.
The deprecated commands and their iproute2 equivalents are as follows:
deprecated replacement(s) ========== ============== - arp ip n (ip neighbor) - ifconfig ip a (ip addr), ip link, ip -s (ip -stats) - iptunnel ip tunnel - iwconfig iw - nameif ip link, ifrename - netstat ss, ip route (for netstat-r), ip -s link (for netstat -i), ip maddr (for netstat-g) - route ip r (ip route)
The basic list is also here on Wikipedia: http://en.wikipedia.org/wiki/Iproute2.