Find the listening process for a port
22 Dec 2020Introduction
In networking, a port is assigned as a logical entity that a socket is established on. These sockets are owned by processes in your operation system. From time to time, it can be unclear which process owns which socket (or who is hogging which port).
In today’s article, I’ll take you through a few techniques on finding out who is hanging onto particular ports.
netstat
netstat is a general purpose network utility that will tell you about activity within your network interfaces.
netstat - Print network connections, routing tables, interface statistics, masquerade connections, and multicast memberships
If you can not find netstat
installed on your system, you can normally get it from the net-tools
package.
The following command will give you a breakdown of processes listening on port 8080
, as an example:
An important message appears here. “Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.”. There will be processes invisible to you unless you run this command as root
.
Breaking down the netstat invocation:
l
will only show listening socketst
will only show tcp connectionsn
will show numerical addressesp
will show you the PID
You can see above, that no process is shown. Re-running this command as root
:
lsof
lsof will give you a list of open files on the system. Remember, sockets are just files. By using -i
we can filter the list down to those that match on an internet address.
fuser
fuser is a program that has overlapping responsibilities with the likes of lsof
.
fuser — list process IDs of all processes that have one or more files open
You can filter the list down directly with the command:
This gives us a PID to work with. Again, note this is run as root
. Now all we need to do is to tranform this PID into a process name. We can use ps
to finish the job.