How to Run Netstat

Learning how to use netstat can help you understand the connections your computer is making and why. The netstat command is available on Macs by default. You don’t need to download or install it. To run netstat:

Netstat Flags and Options

Filtering netstat’s output is essential to understanding what’s happening on your Mac’s active ports. Netstat’s built-in flags allow you to set options, limiting the command’s scope. To see all netstat’s available options, type man netstat at the command prompt to reveal netstat’s man (short for “manual”) page. You can also view an online version of netstat’s man page.

Syntax

It’s important to note that netstat on macOS doesn’t work the same way as netstat on Windows and Linux. Using flags or syntax from those implementations of netstat may not result in the expected behavior. To add flags and options to netstat on macOS, use the following syntax: netstat [-AabdgiLlmnqrRsSvWx] [-c queue] [-f address_family] [-I interface] [-p protocol] [-w wait]

Useful Flags

Here are some of the most commonly used flags:

-a includes server ports in netstat’s output, which are not included in the default output. -g displays information associated with multicast connections. -I interface provides packet data for the specified interface. All available interfaces can be viewed with the -i flag, but en0 is typically the default outgoing network interface. (Note the lowercase letter.) -n suppresses the label of remote addresses with names. This speeds up netstat’s output while eliminating only limited information. -p protocol lists traffic associated with a specific networking protocol. The full list of protocols is available at /etc/protocols, but the most important ones are udp and tcp. -r displays the routing table, showing how packets are routed around the network. -s shows the network statistics for all protocols, whether or not the protocols are active. -v increases verbosity, specifically by adding a column showing the process ID (PID) associated with each open port.

Netstat Examples

Consider these examples: netstat -apv TCP This command returns only TCP connections on your Mac, including open ports and active ports. It also uses verbose output, listing the PIDs associated with each connection. netstat -a | grep -i “listen” This combination of netstat and grep reveals open ports, which are ports that are listening for a message. The pipe character | sends the output of one command to another command. Here, the output of netstat pipes to grep, letting you search it for the keyword “listen” and find the results.

Accessing Netstat Through Network Utility

You also can access some of netstat’s functionality through the Network Utility app, which is included in macOS versions up to Catalina (it’s not included in Big Sur). To get to Network Utility, type Network Utility into Spotlight Search to launch the app, then select the Netstat tab to access the graphical interface. Options within Network Utility are more limited than those available through the command line. Each of the four radio button selections runs a preset netstat command and displays the output. The netstat commands for each radio button are as follows:

Display routing table information runs netstat -r.Display comprehensive network statistics for each protocol runs netstat -s.Display multicast information runs netstat -g.Display the state of all current socket connections runs netstat.

Supplementing Netstat With Lsof

The macOS implementation of netstat doesn’t include much of the functionality users expect and need. Although it has its uses, netstat isn’t as useful on macOS as it is on Windows. A different command, lsof, replaces much of the missing functionality. Lsof displays files currently open in apps. You can also use it to inspect app-associated open ports. Run lsof -i to see the list of applications communicating over the internet. This is typically the goal when using netstat on Windows machines; however, the only meaningful way to accomplish that task on macOS is not with netstat, but with lsof.

Lsof Flags and Options

Displaying every open file or internet connection is typically verbose. That’s why lsof comes with flags for restricting results with specific criteria. The most important ones are below.

-i displays open network connections and the name of the process that is using the connection. Adding a 4, as in -i4, displays only IPv4 connections. Adding a 6 instead (-i6) displays only IPv6 connections. The -i flag also can be expanded to specify further details. -iTCP or -iUDP returns only TCP and UDP connections. -iTCP:25 returns only TCP connections on port 25. A range of ports can be specified with a dash, as it -iTCP:25-50. Using -i@1.2.3.4 returns only connections to the IPv4 address 1.2.3.4. IPv6 addresses can be specified in the same fashion. The @ precursor can also be used to specify hostnames in the same way, but both remote IP addresses and hostnames cannot be used simultaneously. -s typically forces lsof to display file size. But when paired with the -i flag, -s works differently. Instead, it allows the user to specify the protocol and status for the command to return. -p restricts lsof to a particular process ID (PID). Multiple PIDs can be set by using commons, such as -p 123,456,789. Process IDs can also be excluded with a ^, as in 123,^456, which would specifically exclude PID 456. -P disables the conversion of port numbers to port names, speeding up output. -n disables the conversion of network numbers to hostnames. When used with -P above, it can significantly speed up lsof’s output. -u user only returns commands owned by the named user.

lsof Examples

Here are a few ways to use lsof. lsof -nP -iTCP@lsof.itap:513 This complex-looking command lists the TCP connections with the hostname lsof.itap and the port 513. It also runs lsof without connecting names to IP addresses and ports, making the command run noticeably faster. lsof -iTCP -sTCP:LISTEN This command returns every TCP connection with the status LISTEN, revealing the open TCP ports on the Mac. It also lists the processes associated with those open ports. This is a significant upgrade over netstat, which lists PIDs at most. sudo lsof -i -u^$(whoami)

Other Networking Commands

Other Terminal networking commands that might be of interest in examining your network include arp, ping, and ipconfig.