Linux Process – Finding the PID of the Process Using a Specific Port

iplinuxnetstatprocess

I am installing hadoop on my Ubuntu system. When I start it, it reports that port 9000 is busy.

I used:

netstat -nlp|grep 9000

to see if such a port exists and I got this:

   tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN

But how can I get the PID of the process which is holding it?

Best Answer

Your existing command doesn't work because Linux requires you to either be root or the owner of the process to get the information you desire.

On modern systems, ss is the appropriate tool to use to get this information:

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

You can also use the same invocation you're currently using, but you must first elevate with sudo:

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

You can also use lsof:

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