Finding the process id by port number

netstat is your friend when you're trying to troubleshoot a lot of network-related problems. To find a listening port, I would use netstat -tulpn | grep :<port number>

For example, to find what pids are listening on port 22, I would run:

netstat -tulpn | grep :22
tcp  0  0 0.0.0.0:22    0.0.0.0:*    LISTEN      3062/sshd

That tells me that sshd with pid 3062 is listening on port 22.


You might also like a solution using ss that is more systematic and more precise than grepping port numbers.

# ss -t -l 'sport = 22'
State      Recv-Q Send-Q Local Address:Port                 Peer Address:Port
LISTEN     0      128        *:ssh                      *:*
LISTEN     0      128       :::ssh                     :::*