I would like to list my open network ports in Terminal with built in commands. As what I know, netstat is the command to use. But I'm struggling to get any useful information out of it.

How can I list my open ports with netstat? Any specific flags that helps me in this case?

up vote 235 down vote accepted
netstat -ap tcp | grep -i "listen"

Achive Internet connections (including servers)
Proto Recv-Q Send-Q  Local Address          Foreign Address        (state)   
tcp4       0      0  localhost.25035        *.*                    LISTEN
sudo lsof -PiTCP -sTCP:LISTEN

GitHub      850 grgarside   6u   IPv4 0x23c345381d089301      0t0  TCP localhost:25035 (LISTEN)
  • 1
    Thanks! However, the output is quite hard to read, but lsof was a bit better, thanks. – Jonas Jan 17 '14 at 23:13
  • 10
    You need to use sudo lsof .... Without the sudo, lsof can only see processes you own, and hence won't show any ports opened by system processes. Also, you can skip the grep command by telling lsof you only want to see ports in the LISTEN state with sudo lsof -PiTCP -sTCP:LISTEN. – Gordon Davisson Jan 18 '14 at 0:20
  • 1
    → grgarside: beware of the effects of grep -i "listen" within your 2 examples. – daniel Azuelos Jan 18 '14 at 18:36
  • 2
    I just did, turns out the -v flag turns that on. The pid will be in the second-to-last column. – Chris May 3 '16 at 15:31
  • 1
    Why "listen" and not listen? – user1071847 Apr 25 at 12:37

maybe you can use lsof:

lsof -Pn -i4

-i4 means only show ipv4 address and ports -P and -n fast output

output like this

➜  lsof -Pn -i4 | grep LISTEN
QQPlatfor 22767 xxxx   15u  IPv4 0x36c2bfa04e49385d      0t0  TCP *:49969 (LISTEN)
GoAgentX  33377 xxxx    4u  IPv4 0x36c2bfa06e68b12d      0t0  TCP *:56154 (LISTEN)
GoAgentX  33377 xxxx   20u  IPv4 0x36c2bfa04e492f8d      0t0  TCP (LISTEN)
  • 2
    What does this command do? – nohillside Sep 22 '15 at 8:54
  • 3
    lsof lists open files. Network sockets count as files, so each open network socket (either listening or actively in use) will be listed in lsof. – Craig Trader Dec 11 '15 at 9:53
  • It shows also the process id (netstat doesn't) – lib Mar 15 '16 at 15:21
  • lsof -Pn -i6 for IPV6 – Jared Burrows Jul 29 '16 at 0:28
  • 1
    One my machine at least, this answer takes 0.1s, while @grgarside's is 28+ s. The difference is the -i4 switch, to only look at IPv4 addresses. – Davor Cubranic Feb 21 '17 at 17:49

The simplest method is to use netstat:

$ netstat -ap tcp
Active Internet connections (including servers)
Proto Recv-Q Send-Q  Local Address          Foreign Address        (state)    
tcp4       0      0    ESTABLISHED
tcp4      87      0  my_iMac__at_home.55481 stackoverflow.co.http  ESTABLISHED
tcp4     116      0  my_iMac__at_home.55478 stackoverflow.co.http  ESTABLISHED
tcp4      58      0  my_iMac__at_home.63452 stackoverflow.co.http  ESTABLISHED
tcp4      87      0  my_iMac__at_home.63429 stackoverflow.co.http  ESTABLISHED
tcp4       0      0  localhost.63173        localhost.773          ESTABLISHED
tcp4       0      0  localhost.773          localhost.63173        ESTABLISHED
tcp4       0      0  localhost.63173        *.*                    LISTEN     
tcp4       0      0  localhost.63172        *.*                    LISTEN     
tcp4       0      0  localhost.ipp          *.*                    LISTEN     
tcp6       0      0  localhost.ipp          *.*                    LISTEN     

…without any added filtering, so as to get the correct headers, and to see both servers listening, and connections already established in both directions. In this example, the 1st line exhibits a connexion from my Mac toward, which a further:


taught me it is located at Apple.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.