41

What is the equivalent of Linux's netstat -tln on OS X?

Options:

-l, --listening (Show only listening sockets. (These are omitted by default.)
--numeric , -n (Show numerical addresses instead of trying to determine symbolic host, port or user names.)
-t, --tcp
4
  • 2
    What does netstat -tln actually do?
    – nohillside
    Commented Nov 24, 2014 at 9:04
  • You can just open a Terminar an execute on it. It will work.
    – jherran
    Commented Nov 24, 2014 at 9:07
  • 3
    @jherran No, OS X is using a BSD-based netstat which accepts different sets of options.
    – lolski
    Commented Nov 24, 2014 at 9:13
  • @patrix in Linux, it lists all open TCP ports on the machine, and whether it listens locally (127.0.0.1) or to remote connections (0.0.0.0).
    – lolski
    Commented Nov 24, 2014 at 9:16

3 Answers 3

42

Should be this command:

sudo lsof -iTCP:$PORT -sTCP:LISTEN 

Who is listening on a given TCP port on Mac OS X?

3
  • 1
    OP question didn't have a port argument, this answer requires one. This gives a useful command apple.stackexchange.com/a/327967/209603
    – goetz
    Commented Dec 6, 2018 at 18:31
  • 8
    @goetzc Just use -iTCP instead of -iTCP:$PORT.
    – Navin
    Commented Dec 24, 2018 at 9:00
  • 5
    I would add -P since OP specified -n in their question. Also +1 to @Navin. sudo lsof -iTCP -sTCP:LISTEN -P is closer to netstat -tln
    – seeafish
    Commented Nov 7, 2019 at 15:28
28

The closest equivalent you can get on macOS is:

netstat -p tcp -van | grep '^Proto\|LISTEN'
  • tu options are not available, but they can be replaced by either -p tcp or -p udp, although you can't have both at the same time
  • -p option is replaced with -v which effectively gets you PIDs listed
  • -l option is not available, but you can work around it by using -a option (which includes servers in the listing) and grep LISTEN (to filter only for listening)
0
6

For real-time monitoring, try this:

nettop -p 60683

You can also restrict the interface type, like wifi or wired...

nettop -t wifi -n -p 60683

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .