How to find the PID of the process running on a specific port ?

This sneppet will guide you on how to find the PID of the process running on a specific port. PID stands for Process ID. It is a unique number assigned to each process running on any machine. And this PID is used to identify and manage the processes running on a machine.

Find the PID of the process running on a specific port

lsof command

Using lsof command is the most straight-forward way and probably the simplest way to find the PID of the process using a specific port.

For example,

lsof -i :8080

The above command will directly give you the process ID (PID) and other details related to process that is listening on port specified.

netstat command

Alternatively, you can also use the following netstat command to find the PID.

For example,

netstat -tlnp | grep :8080

ss command

Using ss command is even more concise and another way to find the PID of the process using a specific port.

For example,

ss -tlnp 'sport = :8080'

The above command will give you the process ID (PID) and other process related details listening on to the specified port.

Please note, the ss command is often preferred over the netstat command as it is faster and more efficient than the netstat approach.

There are several reasons why you might want to find the PID of the process using a specific port, may be for troubleshooting purpose, to kill a process, to manage system resources such as memory and CPU usage, to investigate potential security issues such as unauthorized access, while debugging applications especially when working with networked applications etc.,

You will find the above methods useful for the cases mentioned. Please note, if the above commands does not work for you, then there might be some permission issue as sometimes Linux requires you to be either root or the owner of the process to get the required information.

The following command will show you the process details including owner information.

ls -l /proc/<PID>

Note: The /proc/<PID> directory has the information about the process with the specified process ID.

For example,

ls -l /proc/1234

dr-xr-xr-x 7 root root 0 Jan 12 14:30 .

In the above example, the owner of the process with PID 1234 is root. The first root is the owner’s username, and the second root is the group name.

Where,

dr-xr-xr-x are the permissions
7 is the number of hard links
root is the owner’s username
root is the group name
0 is the size of the directory
Jan 12 14:30 is the timestamp

References

 

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments