Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I know how to do the opposite (find out what process has a given port open) using lsof or netstat, but extensive research on Google hasn't helped me solve the reverse problem.

I know I could use "netstat -np" combined with some grep and sed, but it seems hacky. Is there a reversed version of "lsof -i tcp:80" that will show me all the local ports opened by a given process?

share|improve this question
1  
I don't see why pipeline filtering is "hacky". It's how Unix was designed to be used. –  Skaperen Sep 6 '12 at 4:00

2 Answers 2

up vote 9 down vote accepted

Take a look at the man page, you'll find that you can use the -p option to specify a process id, and the -i option to limit the display to internet domain sockets (-i4 for just ipv4 and -i6 for just ipv6). So if you string them together...

lsof -p <pid> -i

...you get not quite what you want, because by default lsof will or together your requests. So add the -a (and) flag...

lsof -p <pid> -a -i

...and you'll get a list of the IPv4 sockets open by the specified process id.

share|improve this answer
    
Thank you! The -a flag is what I was missing! –  Alex G Sep 6 '12 at 16:34

I know I could use netstat -np combined with some grep and sed, but it seems hacky.

How about this:

# netstat --inet -nlp | grep <processname>

Is there a reversed version of lsof -i tcp:80 that will show me all the local ports opened by a given process?

# lsof -c <processname> | grep LISTEN
share|improve this answer
    
Downvoter, please leave a comment to let me know why. –  quanta Sep 6 '12 at 15:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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