Finding the PID of the process using a specific port?

  • 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*               LISTEN

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

    `netstat` command might work in many operations systems to allow you get that, you just have to find the arguments that will ensure it will show pids along each known opened port.

  • Chris Down

    Chris Down Correct answer

    7 years ago

    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        *:*                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*  LISTEN  125004/nginx

    You can also use lsof:

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

    Note: under OSX, the `-p` option is for protocol rather than process. See this question

    @BryanP the OP asked for Ubuntu so that's kinda irrelevant...

    @AdamB Unless a Mac user arrived here searching for `Finding the PID of the process using a specific port`

    This answer would probably be improved by putting the need to `sudo` at the top.

    What if i wanted to just return "125004" as PID?

    @MrOnyancha Use the terse (-t) options - `lsof -ti tcp:80`

    what does it mean when the process id field is missing on certain ports? (is it some stupid systemd thing?)

    Chris Down, I think it isn't ethical to copy paste other's answers in your answer. Originally your answer was about `netstat` only. You have included usage of `lsof` and `ss` commands from other answers and comments. At least mention the author of the answer when you extend your answer with options from other answers.

    @Alexandr Huh? This answer doesn't "copy paste others' answers". As with all of my answers, when they are upvoted or otherwise I get a notification about them, I occasionally take a look and realise that they could be improved, which is totally normal, since up to 2014 I used to answer extremely frequently here. In this case, for example, `ss` is not deprecated, whereas other tools are, so it improves the answer to add it. With respect, I don't really need to read the other answers here to know about the existence of these tools, considering I use these and other staple Linux tools every day.

    @Chris Down , I disagree here with you. You can look just one question below your question to see that another answer already have the option which you want to include into your answer. As a result, other answers are becoming redundant and can be removed because you just included all the information from other answers into your answer. You didn't improve your answer, you just included all the options from other answers into your answer. If you want to add something specific to these options you can edit others' answers.

    @Alexandr The point is that these updates don't derive from other answers, they're just updates that happen to relate to other answers. Where updates are a *result* of another answer, as opposed to being a coincidence, it's properly attributed, as with Heinzi here for example. You can't require attribution just because your answer coincidentally happens to contain a subset of information, when that information wasn't derived from your answer. If multiple discovery is likely in academia, it's infinitely more likely in simple things like this.

    @Chris Down , If you want to add some extra information (which you didn't) you can edit the related answer instead of including the whole answer into your answer. Can you tell me what is the point of including `lsof` example if Vitaliy's answer already has this option and his whole answer is based on this option? What is the point of including `ss` option if my answer already have this option and is based specifically for this option only? Also, below my answer there is the comment which is identical to your example (except the port).

    If your answer would had `ss` option I wouldn't create my answer which lists `ss` option. I bet that Vitaliy also wouldn't create the answer to list `lsof` option if your answer would have it. But you included other options into your answer and made other answers worse then your answer just because your answer contains all the information that other answers have.

    Also, notice, I don't tell that you don't know about existance of these tools. I tell that you including others' answers information into your answer. You should improve the answer which is related to specific option and don't just include that option into your answer because you want your answer to be the best one. It would be fair if your answer wouldn't include information options from other answers which were posted much before your edits. I.e. based on your edits and answers to this question I can tell that your answer must not include `ss` and `lsof` options.

    @Alexandr When I'm updating my answers, I don't consult every single other answer to see if there might be an overlap. I simply don't have the time to do that with the volume of content I've written on this site.

License under CC-BY-SA with attribution

Content dated before 6/26/2020 9:53 AM