Wednesday, November 9, 2011

Solaris: Checking which process is bound to which TCP/UDP port number


On solaris, you'll need lsof, but it seems that lsof is not part of the Solaris default installables. It should be available on Sunfreeware

# /usr/local/bin/lsof -i:123
COMMAND PID USER   FD   TYPE        DEVICE SIZE/OFF NODE NAME
xntpd   335 root   19u  IPv4 0x600282a2c40      0t0  UDP *:ntp
xntpd   335 root   20u  IPv4 0x600282a2840      0t0  UDP localhost:ntp
xntpd   335 root   21u  IPv4 0x6002cb81ac0      0t0  UDP scglob20:ntp
xntpd   448 root   19u  IPv4 0x30016a2c800      0t0  UDP *:ntp
xntpd   448 root   20u  IPv4 0x6002f088500      0t0  UDP localhost:ntp
xntpd   448 root   21u  IPv4 0x30016a2ca00      0t0  UDP 161.19.6.146:ntp

Else, you can do the following, the pid the following command could return multiple results

# port=25
# for i in `ls /proc`; do
>    pfiles $i | grep AF_INET | grep "$port" 2>&1 >/dev/null; portfound=$?
>    if [ $portfound -eq 0 ]; then
>       echo "pid $i found to be bounded to port $port"
>    fi
>done
pid 17175 found
pid 2874 found
pid 29348 found
pid 4482 found
pid 463 found
pid 6696 found

This can be followed with, use the pid output from the earlier output
# ps -eo user,pid,comm | grep  
e.g.
# ps -eo user,pid,comm | grep 17175
    root 17175 /usr/lib/sendmail

No comments: