Find Linux/Solaris process using port number

Often we encounter errors that a particular required port is already being used by another process.

The steps to find out which process is using a particular port number is relatively easy in Linux but it can be a bit tricky on Solaris.

Let us see how to find process PID from port number

1. For Linux Operating System

There are 3 ways to find this out depending on which package is installed in your OS.

i) fuser

The syntax of using fuser is
fuser <port>/<protocol>

For example, to find which process is using TCP port 23792,

root@dbhost1 etc]# fuser 23792/tcp
23792/tcp: 17006

ii) netstat

The syntax of using netstat to find process ID from port is
netstat -tulpn | grep <port>

For example, to find which process is using TCP port 23792,

[root@dbhost1 etc]# netstat -tulpn | grep 23792
tcp 0 0 :::23792 :::* LISTEN 17006/java

iii) lsof

The syntax of using lsof to find process ID from port is
lsof -i <protocol>:<port>

For example, to find which process is using TCP port 23792,

[root@dbhost1 etc]# lsof -i tcp:23792
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
java 17006 grid 170u IPv6 594633 0t0 TCP *:23792 (LISTEN)

 

2. For Solaris Operating System

Since these utilities are generally not installed or available by default on Solaris operating system, we use following easy script to find PID from port number.

Note: Save this script as check_port.sh and grant execute permissions using chmod +x to this script.

#!/bin/bash

# Get the process which listens on port

# $1 is the port we are looking for

if [ $# -lt 1 ]
then
echo “Please provide a port number parameter for this script”
echo “e.g. $0 22”
exit
fi

echo “Greping for your port, please be patient (CTRL+C breaks) … ”

for i in `ls /proc`
do
pfiles $i | grep AF_INET | grep $1
if [ $? -eq 0 ]
then
echo Is owned by pid $i
fi
done

Now the syntax of using this script is
./check_port.sh <port number>

For example, to find which process is using TCP port 1521,

root@hostname # ./check_port.sh 1521
Greping for your port, please be patient (CTRL+C breaks) …
sockname: AF_INET 10.21.26.24 port: 1521
Is owned by pid 10279
^C

That’s it. Please feel free to add/suggest any new utilities to other users in comment section below.

Happy learning !

Sep 17th, 2014 | Posted by Tushar Thakker | In Uncategorized
  1. JF Houle
    Jul 30th, 2020 at 13:19 | #1

    i pimped the results, if that helps someone :

    if [ $? -eq 0 ]
    then
    echo “################################################################################################”
    echo “Is owned by pid $i”
    echo;echo “pwdx $i :”
    pwdx $i
    echo;echo “pargs -l $i :”
    pargs -l $i
    echo “################################################################################################”
    fi

  2. champa065
    Feb 18th, 2016 at 10:07 | #2

    Hi ORA Tanning,
    i have run java application but i’m not sure port running.
    i try find port of my application.
    how to check my java application run which one port?
    please help me for solve it.
    Thanks,

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>