简体   繁体   中英

How to find out which Port number a process is using

I want to be able to find out which port number a process is and filtering the results using a keyword.

For example, I may want to quickly find out which port numbers are being used by "node" js apps.

This did not work:

netstat tulnap | grep "node"

This did not return the port numbers:

ps aux | grep node

This is how I found a solution:

     » lsof -i -P  | grep node
    node      14489 me   12u  IPv4 0x...      0t0    TCP *:4000 (LISTEN)

Also if i knew the port and I was looking for the process name I would:

     » lsof -i :4000
    COMMAND   PID       USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
    node    14489 me   12u  IPv4 0x...      0t0  TCP *:terabase (LISTEN)

Active Internet connections (w/o servers)

netstat -pnt 

Active Internet connections (only servers)

netstat -pntl

What you're doing may also never work since GREP is never guaranteed to deliver either no, one, or more than 1 results.

But, in context of your specific case, any process IS actually unique, and has a unique number. So, you must do your search first to find a process number.

Doing a simple grep will still give you either none, one, or more than one process number. But you'll need to find an additional filter, so that you end up with 1 process number.

If you have 1 process number, you can check port(s) being used by that process.

What makes you think "node" is reported in PS ? It may also not be.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM