Find a Linux or Unix process (PID) based on port


Today I ran into a problem where I wanted to find out what Linux process (PID) was listening on which port. You might

A fellow sysadmin coworker showed me this command:

lsof -i :port

Lsof will list the PID and the owner of the process that is listening or interacting on that port!

lsof -i :22
sshd    1997 root    3u  IPv6   5602       TCP *:ssh (LISTEN)
sshd    2458 root    3u  IPv6   7721       TCP centos-at01.canada003.com:ssh->10.20.0.12:55858 (ESTABLISHED)

Sweet!

 

Alternatively you can also do this:

netstat -nap |grep port|grep -i LISTEN

Which will list the ports that the server is listening on as well as the associated PID. Below is a sample output of the httpd process that is listening on port 80 with PID 591

tcp        0      0 :::80                       :::*                        LISTEN      591/httpd

 

For a complete list of PIDs that a port is listening on you may want to use the fuser command. Below is an example for the port 80 httpd process.

fuser -v 80/tcp
                     USER        PID ACCESS COMMAND
80/tcp:              apache      591 F.... httpd
                     apache     8979 F.... httpd
                     apache    15452 F.... httpd
                     apache    15453 F.... httpd
                     apache    15454 F.... httpd
                     apache    15455 F.... httpd
                     apache    15456 F.... httpd
                     apache    15457 F.... httpd
                     apache    15458 F.... httpd
                     apache    15459 F.... httpd
                     apache    15514 F.... httpd
                     apache    15559 F.... httpd
                     apache    15654 F.... httpd
                     apache    15752 F.... httpd
                     apache    15783 F.... httpd
                     apache    16506 F.... httpd
                     apache    17572 F.... httpd
                     apache    21656 F.... httpd
                     root      23934 F.... httpd
                     apache    23963 F.... httpd
                     apache    32508 F.... httpd
Related Posts with Thumbnails

About the Author

Alfred Tong
Author and owner of this blog. A Networking enthusiast, full time networking and systems Engineer. Generally curious about all things IT.Certifications: GIAC GSEC, CCNP-S, CCNP, CCSP, CCDP, CCNA, RHCE, JNCIA - FWV