in

Check PID using Port Number in Solaris

Hi there,

Finding the process ID (PID) using a port number in Solaris can seem daunting at first. As a long-time Solaris admin and data analyst, let me walk you through this step-by-step in an easy-to-follow guide.

First, a quick overview on ports:

Ports allow different networking processes to share a single IP address. For example, a web server may listen on port 80, FTP on port 21, etc. But how do you match a port to its running process? That‘s where PID comes in.

Here‘s a more in-depth look at what‘s happening behind the scenes:

  • The kernel assigns each process a unique PID when it starts up.
  • To listen on ports, processes call the bind() system call to associate their socket with an IP and port.
  • This binds the process PID to that port.
  • The kernel tracks these bindings in an internal table.

So to find a PID from a port, we just need to query that table! The easiest way is using the pfiles command.

Let‘s walk through a simple shell script to lookup PIDs by port:

#!/bin/ksh

port=$1
pids=$(ps -ef -o pid=)

for pid in $pids; do
   pfiles $pid | grep $port 
   if [ $? -eq 0 ]; then 
      echo "Port $port is used by PID $pid"
   fi 
done

Here‘s what it‘s doing:

  1. Get all PIDs with ps -ef.
  2. Loop through each PID.
  3. Use pfiles to check if it‘s bound to the target port.
  4. If there‘s a match, print out the details.

Now you‘ve got a handy way to find any PID based on an open port!

As your resident Solaris guru, let me share some tips I‘ve picked up over the years:

  • Check /etc/services to match port numbers to standard services.
  • Use nmap or netstat for a quick port scan.
  • Watch ephemeral ports between 32768-65535 for short-lived processes.
  • Mind the TCP vs UDP protocols which use ports differently.

And if you‘re migrating from Linux, be aware of some key Solaris differences:

  • Solaris has a richer /proc filesystem with useful tools like pfiles.
  • The netstat -p flag doesn‘t work to show PIDs.
  • Ports below 1024 require root access to bind.

Getting a handle on ports and PIDs will make you a better Solaris admin. Let me know if you have any other questions! I‘m always happy to help a fellow geek out.

Cheers,
[Your Name]

AlexisKestler

Written by Alexis Kestler

A female web designer and programmer - Now is a 36-year IT professional with over 15 years of experience living in NorCal. I enjoy keeping my feet wet in the world of technology through reading, working, and researching topics that pique my interest.