Thursday, January 12, 2012

How to find a PIDs using a port?

Simple solution: Use lsof :)

The following solution works perfectly on a Linux system. In case of Solaris, lsof has to be installed first because it is not a part of default OS.


[root@linux01 ~]# lsof -w -n -i tcp:22
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
sshd 2410 root 3u IPv4 6134 TCP *:ssh (LISTEN)
sshd 1639 root 3u IPv4 1637600475 TCP 10.20.30.99:ssh->10.20.50.231:35263 (ESTABLISHED)
sshd 9592 root 3u IPv4 3266851044 TCP 10.20.30.99:ssh->10.20.50.231:44621 (ESTABLISHED)
sshd 10119 root 3u IPv4 3006017510 TCP 10.20.30.99:ssh->10.20.50.231:62445 (ESTABLISHED)
sshd 13909 root 3u IPv4 739010581 TCP 10.20.30.99:ssh->10.20.40.88:35215 (ESTABLISHED)
sshd 24696 root 3u IPv4 273846126 TCP 10.20.30.99:ssh->10.20.40.89:39529 (ESTABLISHED)
sshd 24900 root 3u IPv4 352035868 TCP 10.20.30.99:ssh->10.20.40.88:59163 (ESTABLISHED)
sshd 25662 root 3u IPv4 982162556 TCP 10.20.30.99:ssh->10.20.40.89:65096 (ESTABLISHED)
sshd 27358 root 3u IPv4 982169152 TCP 10.20.30.99:ssh->10.20.40.89:65097 (ESTABLISHED)



No comments: