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.
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.
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.
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.
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:
netstat -ano | findstr 8561 | findstr LISTEN
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:
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: