By Antony Reynolds on May 24, 2012
Who Owns What Port?
It is not uncommon to be unable to start a server process because some other process is holding onto a network port that is required by the server. The question is how do you find the offending process? I thought I would identify some of the commands I use to track down wayward port usage.
Identify the Conflict
The first thing to do is to identify the port that is being used. Hopefully your log file will indicate which port the server process was unable to obtain. Even if it did not identify the port and you know the ports that it requires then you can use the first of my helpful commands:
|netstat –anop tcp||netstat –lnt --program|
The Windows version lists all “-a” network sockets using TCP/IP v4 “-p tcp”. To make it easier to find the listening port I had them list in numeric format “-n” rather than using abbreviations. Other possible protocols are TCP/IP v6 “tcpv6”, UDP “udp” and UDP v6 “udpv6”. Finally I had the netstat command print out the process ID “-o” of the process using the port.
The Linux version is slightly different in that it lists only listening ports “-l” in numeric form “-n” for the TCP protocols “tcp”, both V4 and V6. For UDP protocol use “-u”. The process ID and program name is also displayed “—program”. Note that this is best run as root because you need root privileges to have netstat show you the pid of a process you don’t own.
Find the Culprit
Now that we know which process is holding which port in use the next thing to do is find out more about the process holding onto our port. The second of our helpful commands shows us the command line used to launch are mischievous process.
|tasklist /FI “PID eq <PID>”||ps –p <PID> –o args|
Unfortunately I haven’t found a good way to find out the actual command line from a Windows machine. Tasklist allows you to filter “/FI” the list of tasks to see the process name associated with a PID “PID eq <PID>, but if that process is a service then the process name will show as “svchost.exe”. You may be able to see more information by using Windows Process Explorer, but even that doesn’t always tell you what you need to know.
On Linux we can use the trusty ps command to find a given pid “-p <PID>” and output the command and associated command line arguments “-o args”. From this we know exactly who is using our PID.
Armed with this information we can reconfigure the errant process, shut it down or decide that we need to change the port number for our server process instead.