get pid in linux

Find PID of Process Running on Port

Sometimes you may need to find out which process is running on a specific port. In this article, we will look at how to find PID of process running on port. This is useful if you are trying to run a process on a port and get an error that the port is already in use.


Find PID of Process Running on Port

Here are the different ways to find PID of process running on port.


1. Using ss command

In the latest versions of Linux, you can use ss command to find out which process is running on a given port. Here is an example to find out which process is running on port 80. Replace 80 below with port number of your choice.

$ sudo ss -lptn 'sport = :80'
State   Local Address:Port  Peer Address:Port              
LISTEN  127.0.0.1:80        *:*                users:(("nginx",pid=15004,fd=12))
LISTEN  ::1:80              :::*               users:(("nginx",pid=15004,fd=11))


2. Using netstat

In older Linux systems, you can use netstat command to find process running on a specific port. Here is the above example using netstat. Replace 80 below with port number of your choice.

$ sudo netstat -nlp | grep :80
tcp  0  0  0.0.0.0:80  0.0.0.0:*  LISTEN  15004/nginx

In the above output, the last value displays PID (in bold) followed by application command. In the above output 15004 is the PID of process running on port 80. Please note, you need to be logged in as a user with sudo privileges to be able to run netstat command as shown above.


3. Using lsof

You can also use lsof command to find the file using port of your choice. However, in this case, you need to logged in as root user or user with sudo privileges. Replace 80 in the command below with your port of choice.

$ sudo lsof -n -i :80 | grep LISTEN
nginx   15004 nginx    3u  IPv4   6645      0t0  TCP 0.0.0.0:80 (LISTEN)

In the above output, the second value 15004 is the PID of process running in port 80.

In this article, we have looked at 3 easy ways to view PID of process using a port in our system.

Also read:

How to Check if Port is Open or Closed in Linux
How to Determine Kernel Architecture in Linux
How to List All Cron Jobs for All Users
How to Check Crontab Logs in Linux
How to Add Repository in Ubuntu/Debian Linux

Leave a Reply

Your email address will not be published. Required fields are marked *