List all open ports and their owning executables

lsof -i -P | grep -i "listen"
Particularly useful on OS X where netstat doesn't have -p option.
Sample Output
launchd      1           root   18u  IPv6 0x08867e20      0t0    TCP localhost:631 (LISTEN)
launchd      1           root   19u  IPv4 0x0886cb4c      0t0    TCP localhost:631 (LISTEN)
launchd      1           root   73u  IPv6 0x08867bb0      0t0    TCP *:548 (LISTEN)
launchd      1           root   75u  IPv4 0x0886c740      0t0    TCP *:548 (LISTEN)
krb5kdc     29           root   15u  IPv6 0x0de526d0      0t0    TCP *:88 (LISTEN)
krb5kdc     29           root   16u  IPv4 0x132e2334      0t0    TCP *:88 (LISTEN)
Dropbox    204          patko   21u  IPv4 0x0886a2d4      0t0    TCP localhost:26165 (LISTEN)
Dropbox    204          patko   22u  IPv4 0x0886c334      0t0    TCP *:17500 (LISTEN)
iChatAgen  222          patko    8u  IPv4 0x0e05aaec      0t0    TCP *:50603 (LISTEN)

18
By: patko
2010-10-14 09:37:51

What Others Think

Just show: User Command Port: echo 'User: Command: Port'; echo "--------------------" ; lsof -i -P | grep -i 'listen' | awk '{print $3, $1, $9}' | sort | uniq | sed 's/ [a-z0-9\.\*]*:/ /' | sort -k 3 -n
wickedcpj · 373 weeks and 5 days ago
Nice touch wickedcpj. Thanks!
patko · 372 weeks and 1 day ago
don't forget to prepend with 'sudo' to see open ports by other users!
f1sherman · 328 weeks and 1 day ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: