352

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?

428

On Linux, you must be root or the owner of the process to get the information you desire. As such, for processes running as another user, prepending sudo is most of what you need. In addition to that, on modern Linux systems, ss is tool to use to do this:

$ 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 remember to 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)
  • 32
    Note: under OSX, the -p option is for protocol rather than process. See this question – Bryan P Apr 10 '16 at 16:51
  • 3
    @BryanP the OP asked for Ubuntu so that's kinda irrelevant... – Adam B Sep 21 '16 at 14:26
  • 37
    @AdamB Unless a Mac user arrived here searching for Finding the PID of the process using a specific port – mraaroncruz Oct 10 '16 at 11:05
  • 2
    This answer would probably be improved by putting the need to sudo at the top. – Nacht Jan 6 '17 at 1:10
  • 2
    @MrOnyancha Use the terse (-t) options - lsof -ti tcp:80 – Mohnish Jul 19 '17 at 13:58
128

Also you can use lsof utility. Need to be root.

# lsof -i :25
COMMAND  PID        USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
exim4   2799 Debian-exim    3u  IPv4   6645      0t0  TCP localhost:smtp (LISTEN)
exim4   2799 Debian-exim    4u  IPv6   6646      0t0  TCP localhost:smtp (LISTEN)
  • 9
    This command will also give you processes with established connections, not just processes that are listening. – firelynx Aug 18 '15 at 10:10
  • 1
    Not necessarily to be root. And, for those who want to get PID only, you can lsof -i :25 -Fp, which produces output like p1234. – Robert Mar 9 '17 at 10:27
11

I am using "CentOS 7 minimal" which has nor netstat neither lsof. But a lot of linux distributions have the socket statistics command (i.e. ss).

Here is an example of execution:

# ss -tanp | grep 6379
LISTEN   0    128  127.0.0.1:6379   *:*   users:(("redis-server",pid=2531,fd=4))
9

Running the command with sudo would give you the PID. On my development machine I get:

$ netstat -nlp | grep 8080
tcp6       0      0 :::8080      :::*       LISTEN      -

$ sudo netstat -nlp | grep 8080
tcp6       0      0 :::8080      :::*       LISTEN      16449/java

And as mentioned in other answers you can also use the ss or the lsof commands.

  • running command as sudo display process ID – Eugen Konkov May 10 '18 at 8:10
  • But what if sudo netstat -nlp | grep 34157 still display - instead of PID? – Eugen Konkov May 10 '18 at 8:16
7

Also you can use fuser:

fuser -v -n tcp 22

The output :

                     USER        PID ACCESS COMMAND
22/tcp:              root        598 F.... sshd
  • It doesn't working fuser -v -n tcp 80, even I try with sudo – SuperKrish Dec 2 '16 at 5:42
  • 1
    Note: This requires sudo if the offending process was also started with sudo – laggingreflex Jan 21 '17 at 8:14
  • 1
    This is a good thing to remember generally. Commands in Linux generally won't give information on processes started by root/sudo unless the command is run with Sudo. This is true even when the command does not normally need sudo to run correctly. – njfife Jul 6 '17 at 21:15

protected by Community May 14 '18 at 6:09

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.