Ports are analytical entities showing an endpoint of communication belonging to the given process or service. An application/service can only hold a single port at a time. Sometimes, the user needs to know which process ID is using the specific port.
This post will signify to the user to find the process ID using a specific port in Linux.
- Method 1: Through netstat Utility
- Method 2: Through lsof Utility
- Method 3: Thorough ss Utility
- Method 4: Thorough fuser Utility
Method 1: Through netstat Utility
The netstat (Network Statistics) is the networking tool for configuration and troubleshooting over the network connection. To use the netstat command in Linux, you might need to install the “net-tools”, consider the given command install it:
$ sudo apt install net-tools #Debian/Ubuntu
$ sudo yum install net-tools #CentOS/RHEL/Fedora
$ sudo pacman -S netstat-nat #Arch-Linux
$ sudo zypper install net-tools #OpenSUSE
This utility can be used to find the PID of the process running a particular port, run the netstat command with sudo permission:
$ sudo netstat -ltnup
The options in the above command is described in the following table:
Options | Purpose |
---|---|
“p” flag | Displays Process ID or name. |
“u” flag | For Displaying UDP connections. |
“t” flag | For Displaying TCP connection. |
“l” flag | Displays the listening sockets. |
“n” flag | Displays addresses in numerical format. |
To find the PID of the process using a specific port, use the netstat command with grep and specify the port number to check:
$ sudo netstat -nlp|grep '33060'
The ID of the process listening on port 33060 is “1056”.
Method 2: Thorough ss Utility
Another method to find the process ID for a specific port is through the “ss” (Socket Statistics) built-in utility. This tool is similar to the netstat command with more detailed information.
Use the “sport” and specify the port number as shown:
$ sudo ss -ltnup 'sport = :33060'
The process ID of the port 33060 is “1056”.
Note: the “ss” utility is the replacement of the “netstat,” or you can say that advanced version of the “netstat.”
Method 3: Through lsof Utility
The “lsof” is the utility utilized to list all open files it can be used for listing the information port specific. To get the support of the lsof utility, get it on Linux using one of the commands as per the Linux distribution:
$ sudo apt install lsof #Debian/Ubuntu
$ sudo yum install lsof #RHEL/CentOS/Fedora
$ sudo zypper install lsof #OpenSUSE
$ sudo pacman -S lsof #Arch-Linux
Run “lsof” with the “sudo” for root permissions and the “i” flag to select and specify the port number:
$ sudo lsof -i :33060
The process ID of the port “33060” is “1056” as shown in the “PID” column.
Method 4: Thorough fuser Utility
Another method to find the process ID for the particular port is the “fuser” utility. It displays the information for which process utilizes the named file, file system, or sockets. To install the fuser utility on Linux, the following command can be considered:
$ sudo apt install psmisc #Debian/Ubuntu
$ sudo yum install psmisc #RHEL/CentOS/Fedora
$ sudo pacman -S psmisc #Arch-Linux
$ sudo zypper install psmisc #OpenSUSE
The user can use the below command with the particular port number to search for the process ID.
$ sudo fuser -v 33060/tcp
The process ID of the particular port “33060” is “1056”.
The user can also specify the multiple port numbers to get the IDs of the process running on these ports:
$ sudo fuser -v 33060/tcp 68/udp
The process ID for both ports has been displayed.
Conclusion
In Linux, To find the process ID of the process using the specific port, use the “netstat”, “ss”, “losf”, or the “fuser” utility. The “netstat”. The “netstat” utility can be accessed by installing the “net-tools”, while the “ss” is the built-in tool and the replacement of the “netstat”. The “fuser” and “lsof” can be installed manually if not found in the system.