Find which process is using a specific port

If you need to know/find which process is using a specific port on your system, you can use some default tools for troubleshooting:

  • netstat
  • lsof

WARNING: For the use of these tools, you will need root rights (or sudo rights will be efficient)

First, with netstat you can use options tnlpu to display much informations on connections:

$ sudo netstat -tlnpu
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      5786/php-fpm: pool
tcp        0      0 127.0.0.1:27017         0.0.0.0:*               LISTEN      18591/mongod
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      15481/python
tcp        0      0 0.0.0.0:8081            0.0.0.0:*               LISTEN      19238/nginx
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1105/sshd
tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      15098/postgres
tcp6       0      0 :::22                   :::*                    LISTEN      1105/sshd

Then, with lsof you can easily find which connections are opened on a specified port with the option -i on your command-line:

$ sudo lsof -i tcp:22
COMMAND  PID       USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
sshd    1054       root    3u  IPv4   9672      0t0  TCP *:ssh (LISTEN)
sshd    1054       root    4u  IPv6   9674      0t0  TCP *:ssh (LISTEN)
sshd    1146       root    3u  IPv4   9794      0t0  TCP 192.168.56.101:ssh->192.168.56.1:49931 (ESTABLISHED)
sshd    1150       root    3u  IPv4   9876      0t0  TCP 192.168.56.101:ssh->192.168.56.1:49933 (ESTABLISHED)
sshd    1254       admin   3u  IPv4   9794      0t0  TCP 192.168.56.101:ssh->192.168.56.1:49931 (ESTABLISHED)
sshd    1265       admin   3u  IPv4   9876      0t0  TCP 192.168.56.101:ssh->192.168.56.1:49933 (ESTABLISHED)