Skip to content

Instantly share code, notes, and snippets.

@defunkt
Created April 21, 2010 17:24
Show Gist options
  • Save defunkt/374130 to your computer and use it in GitHub Desktop.
Save defunkt/374130 to your computer and use it in GitHub Desktop.
List what ports are in use on OS X
# List what ports are in use on OS X
sudo lsof -iTCP -sTCP:LISTEN -P
@acdha
Copy link

acdha commented Apr 21, 2010

A better way to write this:

lsof -iTCP -sTCP:LISTEN

The list of protocols and states is system-dependent but pretty standard on Unixes

@briandoll
Copy link

I was about to say that @acdha's command omitted several ports, but it's because the original script uses sudo.

so yes, this seems best:
sudo lsof -iTCP -sTCP:LISTEN

@defunkt
Copy link
Author

defunkt commented Apr 21, 2010

Updated, thanks!

@defunkt
Copy link
Author

defunkt commented Apr 21, 2010

Un-updated - that command doesn't show me port #s in OS X.

@briandoll
Copy link

True. It seems to prefer port labels when they are available. Just for fun, here is a good reference: http://www.iana.org/assignments/port-numbers

@priteau
Copy link

priteau commented Apr 21, 2010

The difference is caused by the -P option which was in the original command. Try this one:

sudo lsof -iTCP -sTCP:LISTEN -P

@briandoll
Copy link

@priteau FTW!

@kennethreitz
Copy link

Wow, sudo lsof -iTCP -sTCP:LISTEN -P is alot faster and works great.

@acdha
Copy link

acdha commented Apr 22, 2010

@kennethreitz: that's why I posted it - doing the filtering in lsof rather than on its output can be enormously faster (particularly if you use the host/port filtering syntax - e.g. sudo lsof -iTCP:80 -sTCP:LISTEN -P for HTTP) and I've tended to encounter that in situations where it really mattered on busy servers.

@nevans
Copy link

nevans commented Apr 23, 2010

I've long been a fan of sudo netstat -tunlp, but I don't know if all of those options are cross-platform or Linux only.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment