Lists all listening ports together with the PID of the associated process

lsof -Pan -i tcp -i udp
This command is more portable than it's cousin netstat. It works well on all the BSDs, GNU/Linux, AIX and Mac OS X. You won't find lsof by default on Solaris or HPUX by default, but packages exist around the web for installation, if needed, and the command works as shown. This is the most portable command I can find that lists listening ports and their associated pid.
Sample Output
COMMAND     PID        USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME
nmbd       1381        root   10u  IPv4 37683465      0t0  UDP *:137
nmbd       1381        root   13u  IPv4 37683466      0t0  UDP *:138
nmbd       1381        root   14u  IPv4 37683468      0t0  UDP 192.168.1.66:137
nmbd       1381        root   15u  IPv4 37683469      0t0  UDP 192.168.1.66:138
nmbd       1381        root   16u  IPv4 37683470      0t0  UDP 192.168.1.6:137
nmbd       1381        root   17u  IPv4 37683471      0t0  UDP 192.168.1.6:138
smbd       1384        root   25u  IPv6 37683528      0t0  TCP *:445 (LISTEN)
smbd       1384        root   26u  IPv6 37683530      0t0  TCP *:139 (LISTEN)
smbd       1384        root   27u  IPv4 37683532      0t0  TCP *:445 (LISTEN)
smbd       1384        root   28u  IPv4 37683534      0t0  TCP *:139 (LISTEN)
portmap    1642      daemon    4u  IPv4     4146      0t0  UDP *:111
portmap    1642      daemon    5u  IPv4     4161      0t0  TCP *:111 (LISTEN)
rpc.statd  1658       statd    4u  IPv4     4186      0t0  UDP *:986
rpc.statd  1658       statd    6u  IPv4     4195      0t0  UDP *:50215
rpc.statd  1658       statd    7u  IPv4     4198      0t0  TCP *:37962 (LISTEN)
cupsd      1689        root    5u  IPv4 51957733      0t0  TCP *:631 (LISTEN)
cupsd      1689        root   16u  IPv6 51957734      0t0  TCP *:631 (LISTEN)
cupsd      1689        root   18u  IPv4 51957737      0t0  UDP *:631
dhclient   2031        root    5u  IPv4     5307      0t0  UDP *:68
apache2    2093        root    4u  IPv6     5480      0t0  TCP *:80 (LISTEN)
avahi-dae  2281       avahi   13u  IPv4     5670      0t0  UDP *:5353
avahi-dae  2281       avahi   14u  IPv6     5671      0t0  UDP *:5353
avahi-dae  2281       avahi   15u  IPv4     5672      0t0  UDP *:60574
avahi-dae  2281       avahi   16u  IPv6     5673      0t0  UDP *:37378
hddtemp    2705        root    0u  IPv4     6851      0t0  TCP 127.0.0.1:7634 (LISTEN)
dhclient   3115        root    5u  IPv4    10765      0t0  UDP *:68
exim4     13674 Debian-exim    4u  IPv4 45844908      0t0  TCP 127.0.0.1:25 (LISTEN)
sshd      26220        root    3u  IPv4 16151998      0t0  TCP *:22 (LISTEN)
sshd      26220        root    4u  IPv6 16152000      0t0  TCP *:22 (LISTEN)
apache2   30313    www-data    4u  IPv6     5480      0t0  TCP *:80 (LISTEN)
apache2   30316    www-data    4u  IPv6     5480      0t0  TCP *:80 (LISTEN)

29
By: atoponce
2010-06-07 15:22:44

4 Alternatives + Submit Alt

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.

Considering A New Car?

You should consider a Tesla. Get 6 Months Free Supercharging.

Free Supercharging

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: