commandlinefu.com is the place to record those command-line gems that you return to again and again.
You can sign-in using OpenID credentials, or register a traditional username and password.
Subscribe to the feed for:
Shows you all listening tcp/udp ports, and what program has them open(depending on rights)
There are 2 alternatives - vote for the best!
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.
While `lsof` will work, why not use the tool designed explicitly for this job?
(If not run as root, you will only see the names of PID you own)
Alternative of OJM snippet :
This one show the IP too, where ports bind. It's very important, because if there's only 127.0.0.1 instead of 0.0.0.0, connections from internet are rejected.
Lists all opened sockets (not only listeners), no DNS resolution (so it's fast), the process id and the user holding the socket.
Previous samples were limiting to TCP too, this also lists UDP listeners.
If you can do better, submit your command here.
You must be signed in to comment.