Mapping Ports and PIDs

By Sandra Henry-Stocker  3 comments

There are many reasons why you might need to determine what process is using a particular port or, conversely, what port(s) a particular process has open. There are also a number of tools such as lsof (list open files) and pfiles that can provide this information to you in various formats.

You can, for example, use the pfiles command to find out that process 27727 (an Apache process) has port 80 open:

# pfiles 27727 | grep -i port
        sockname: AF_INET 0.0.0.0  port: 80

If you don't use grep to pare down your display to just the open ports, you will see information on open files as well as ports and would then have to locate the port in the output.

You can view the same output for all snmpd processes like this:

# pfiles `pgrep snmpd` | grep port:
        sockname: AF_INET 0.0.0.0  port: 16161
        sockname: AF_INET 0.0.0.0  port: 32808
        sockname: AF_INET 0.0.0.0  port: 32809
        sockname: AF_INET 0.0.0.0  port: 161
        sockname: AF_INET 0.0.0.0  port: 32817
        sockname: AF_INET 0.0.0.0  port: 32873
	...

It gets a little trickier if you want a nice clean listing showing each PID along with its ports. Here's an example:

# for P in `pgrep snmpd`
> do
>     pfiles $P | egrep "$P|port:"
> done
10084:  /usr/lib/snmp/snmpdx -y -c /etc/snmp/conf
        sockname: AF_INET 0.0.0.0  port: 16161
        sockname: AF_INET 0.0.0.0  port: 32808
        sockname: AF_INET 0.0.0.0  port: 32809
10137:  /usr/sfw/sbin/snmpd
        sockname: AF_INET 0.0.0.0  port: 161
        sockname: AF_INET 0.0.0.0  port: 32817
        sockname: AF_INET 0.0.0.0  port: 32873
	...

Note that, in the commands above, I specified "port:" rather than simply "port" to avoid including in the display files (such as /export/home/jdoe/myfile) that happen to have the word "port" in their paths.

An even easier and more generalizable way to extract process/port mappings from your system is to make use of a Korn script named "pcp" (for "PID con Port").

The pcp script uses pfiles and other "proc" commands such a ptree to provide PID and port information in a tidy display. In fact, you can ask the script to supply ports associated with a process by using the -P option and specifying the PID or you can ask the script to supply PIDs responsible for certain open ports by using the -p option and specifying the port. Got that? Uppercase P for PID and lowercase p for port.

Let's look at some examples.

Here, we ask to see the port(s) that process 27727 has open:

# ./pcp -P 27727
PID     Process Name and Port
_________________________________________________________
27727   /usr/apache/bin/httpd
        sockname: AF_INET 0.0.0.0  port: 80
_________________________________________________________

Next, we ask to see all the processes that are using port 80:

# ./pcp -p 80
PID     Process Name and Port
_________________________________________________________
10132   /usr/apache/bin/httpd   80
        sockname: AF_INET 0.0.0.0  port: 80
_________________________________________________________
10156   /usr/apache/bin/httpd   80
        sockname: AF_INET 0.0.0.0  port: 80
_________________________________________________________
10157   /usr/apache/bin/httpd   80
        sockname: AF_INET 0.0.0.0  port: 80
...
...
_________________________________________________________
27727   /usr/apache/bin/httpd   80
        sockname: AF_INET 0.0.0.0  port: 80
_________________________________________________________

If you would like a table of all processes (all processes with open ports anyway) and their corresponding ports, you would run pcp with the -a (all) option.

You can download from pcp from:

http://www.unix.ms/pcp/

3 comments

    Anonymous 2 years ago
    Thanks Sandra, this tool is excellent.
    Anonymous 2 years ago
    The pfiles command, and the pcp script that uses it seemed a great idea. I tried it on a test machine and it worked a treat, then I tried it on a Sybase database server - ASE 12.5.3 running on Solaris 8. It hung the database server! I had to kill the dataservers and restart them.WARNING: pfiles is not like ps. It isn't harmless!I don't know what it did, but at one point there was a message saying that it couldn't gain control (presumably of the process) but why it would want to ???USE AT OWN RISK!
    Anonymous 2 years ago
    As always, Sandra, you've come through with some simple and great information on managing *nix. Thanks a bunch for this article! :)

      Add a comment

      Post a comment using one of these accounts
      Or join now
      At least 6 characters

      Note: Comment will appear soon after you have activated your account.
      Obscene/spam comments will be removed and accounts suspended.
      The information you submit is subject to our Privacy Policy and Terms of Service.

      ITworld LIVE

      IT Management/StrategyWhite Papers & Webcasts

      White Paper

      Evaluator Group: Storage Federation - IT Without Limits (Analysis of HP Peer Motion with Storage Federation)

      As the role of IT increases within organizations, the need to move data when and where it is needed is critical to support emerging business requirements. This has become increasingly difficult due to the huge growth of data volumes. This white paper sponsored by HP + Intel evaluates a solution that aims to enable the movement of data without physical limitations. Read now and see how this could enable agility and efficiency.

      White Paper

      ESG Lab Validation Report: HP Data Protector & Deduplication Solutions

      Many organizations have deployed disk-to-disk backup technologies to improve the speed and reliability of their backup and disaster recovery operations. A growing number of these now look to data deduplication to enhance retention periods and reduce costs. This ESG Lab Validation Report sponsored by HP + Intel examines a number of backup and recovery solutions and evaluates their ease of implementation as well as their ability to improve reliability and reduce costs.

      White Paper

      Business Value of Blade

      The nature of the blade platform makes system management, monitoring and provisioning easy and efficient. Access this resource to learn how blade migration will save your data center time and money while increasing performance.

      White Paper

      Accelerate time to application value

      For your IT organization to keep pace with the business, you need a new, faster approach to infrastructure deployment-an approach that increases agility and accelerates time to application value. That's HP Converged Systems. Built on Converged Infrastructure, these systems deliver the industry's first portfolio of pre-integrated, tested, and optimized infrastructure solutions for applications running in virtual, cloud, dedicated, or hybrid environments.

      White Paper

      Converged Infrastructure for Dummies

      As you know, everything is mobile, connected, interactive, and immediate. This is exactly why organizations need a highly agile IT infrastructure in order to keep pace with extreme fluctuations in business demand. This book will help you understand why infrastructure convergence has been widely accepted as the optimal approach for simplifying and accelerating your IT to deliver services at the speed of business while also shifting significantly more IT resources from operations to innovation.

      See more White Papers | Webcasts

      Ask a question

      Ask a Question