How to find out which process listens on certain port on Solaris?

This week i (again after a while) needed to know which (damn) process held
certain port and so was quite "successfully" blocking my code.
In Tux world i believe problem like this could be solved simply by using a stupid
parameter of netstat command. It is unfortunately not that easy on Solaris :-(

In an excited mood i totally forgot i had written a handy shell script for this
purpose some time ago. So i killed all suspicious processes to solve the problem.
Just to help myself keep in mind there is a better
way i published the script at http://blogs.sun.com/japod/resource/portHolder.sh

The script exploits pfile command, which provides information
on files being used by certain process. And since everything is a file in the unix world,
pfiles provides information on port numbers as well.
The bad news is that you need to invoke pfile command
for every single running process in order to get complete information.

Core of the script looks like the following:

PIDS=`ps -ef $LIMIT_UID| awk '{ print $2 }' | sed 1d`
for p in $PIDS ;do
pfiles $p 2> /dev/null | grep "port: $PORTNUM" > /dev/null 2>&1
if test 0 -eq $? ;then
if test -z "$PRINT_ARGS" ;then
ps -p $p | sed 1d
pargs -l $p

Lets say, you want to find out who holds port number 1175. Then you run:

%portHolder.sh -p 11750                                                                                  
3673 pts/4 0:34 java

Well, you might have launched many java processes, so in order to find out which one is
"yours" you add -a parameter:
%portHolder.sh -p 11750 -a                                                                               /usr/jdk/jdk1.6.0_01/bin/java '-Dcom.sun.aas.instanceRoot=/export/home/japod/.personalDomain9/personalDomain'
... lines removed for clarity...

Join the discussion

Comments ( 7 )
  • Mike Friday, November 30, 2007

    Or you could just use lsof.

  • Jakub Friday, November 30, 2007

    Which I can not find on my box (Nevada, x86 -- build 61: %which lsof gives me: lsof not found :-(

  • mmao Sunday, December 2, 2007

    On windows, I used couple GUIs, like activePorts, procexp, and the netstat -pant for linux, not sure, if sure if there's a gui on \*inx platform, that will quite useful for monitoring the port usage on the fly.

  • MartinG Monday, December 3, 2007

    I just got it from Blastwave ... then 'lsof -i' and you get the ports held by processes. Works great, thanks Mike & Jakub! ;O)

  • MartinG Monday, December 3, 2007

    Hmm, some more observation. I tried to find who's holding ports 5800 and 5900 (it's the default vncserver + http vnc access to desktop in Solaris 11 builds). Jakub's portholder prints this easily:

    bash-3.2$ ./portHolder.sh -p 5800

    910 ? 1:13 vino-ser

    Then I tried

    lsof -i :5800

    No result. Hmm, I tried the same with 5900 and got no result as well. Then, knowing the pid from Jakub's script, I tried

    lsof -i | grep 910. Output is below:

    vino-serv 910 snajper 12u IPv4 0xd67a4300 0t0 TCP \*:\*->\* (ESTABLISHED)

    vino-serv 910 snajper 19u IPv6 0xd67a3000 0t0 TCP \*:\*->[::170c]:\* (ESTABLISHED)

    vino-serv 910 snajper 20u IPv6 0xd678db00 0t0 TCP \*:\*->[::16a8]:\* (ESTABLISHED)

    So, lsof is able to find the port holder, but only this command leads me to some results:

    lsof -i | grep 170c

    where first I need to convert the port numbers to hexa. Is there a way to tell lsof to work in human readable format?

  • guest Sunday, November 17, 2013

    Worked well. Thanks for sharing. I tried using netstat but I couldn't find process id. I dont have lsof installed either, and I could not install that under the circumstance.

    Thanks a lot.

  • Simon M Thursday, March 27, 2014

    Where can I download this script from now as the above link to the script doesn't appear to be working anymore...

Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha