Determining which process is using a particular port


Sometimes, you need to determine which process is using a particular port. For example, you might need to determine the process if you receive the message port in use. You might also want to know the specific process when you are in the planning stages of a SAS® installation and you want to verify that particular ports are available. Regardless of your motivation, here is how to discover the process that owns a port.

UNIX Operating Environments: The lsof Command

The command lsof (LiSt Open Files) is a utility command that is found in most UNIX environments. Under UNIX, use this command to determine which process is using a port, as described in the next section.

How Do I Use the lsof Command?

To obtain the process ID (PID) of the process that is listening on a port, you submit an lsof command similar to the following:

lsof -i -n -P | grep port-number | grep LISTEN

The following example determines the PID of the process that is listening on port 8080:

[root@d21022 ~]# lsof -i -n -P | grep 8080 | grep LISTEN java 8059 sas 38u IPv4 7471428 TCP *:8080 (LISTEN)

In this example, 8059 is the PID of the process that is listening on port 8080. You can use the ps command to discover more information about that process.

What If the lsof Command Is not in My UNIX Environment?

Although lsof is available for nearly every variation of UNIX, it is generally not included in the base installation of the operating system. If lsof is not installed on your UNIX system, you can download the lsof source and build it.

As an alternative, you can find a prebuilt binary file and install that directly. The following list contains examples of some prebuilt binary files that you can use.

For other prebuilt lsof binary files, search for the term lsof binary download on the Web.

Windows Operating Environment: The netstat Command

Most editions of Microsoft Windows support the -o option in the netstat command. This option displays the owning PID, as shown in the following examples:

 

Example 1: Query the System

netstat -ano | findstr 8561 | findstr LISTEN

Example 2: Determining the Process ID

C:\Users\rocele>netstat -ano | findstr 8561 | findstr LISTEN TCP 0.0.0.0:8561 0.0.0.0:0 LISTENING 2164 TCP [::]:8561 [::]:0 LISTENING 2164

In this example, process ID 2164 is listening on port 8561. To find out more about that specific process, use the Windows Task Manager, as follows:

  1. Open the Windows Task Manager. In the task-manager dialog box, click the Processes tab.
  2. On the Processes tab, select the process ID ► you are interested in. Then select View Select Columns.
  3. Select PID (Process Identifier) by clicking the check box.

    A "Select Process Page Columns" popup appears with "PID (Process Identifier)" selected

    4. When this item is checked, it will be added to the list of columns that are displayed in the task manager, as shown below:

    In the image name column, the item is listed

    5. Sort on the PID column to make it easier to find the process ID in which you are interested. When you find that process ID, you can see additional information about that process.