189

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?

287
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

COMMAND     PID      USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
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
  • 11
    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. – dan Jan 18 '14 at 18:36
  • 1
    With GNU netstat, you can optionally list the pid of the process that opened each port. Is there any way to get that behavior with BSD netstat as well? – Chris May 3 '16 at 15:27
  • 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
54

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 127.0.0.1:56155 (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
7

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  10.0.2.23.58792        17.172.233.109.5223    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 17.172.233.109, which a further:

whois 17.172.233.109

taught me it is located at Apple.

3

First, I'm not a BSD expert, but like the OP I wanted the rough equivalent of running the following on a *nix box, or something close:

netstat -tulpn

I read other questions/answers offering lsof* and netstat* on MacOS, and I still wanted something with more compact output. So, this is what I quick put together:

netstat -Watnlv | grep LISTEN | awk '{"ps -o comm= -p " $9 | getline procname;colred="\033[01;31m";colclr="\033[0m"; print cred "proto: " colclr $1 colred " | addr.port: " colclr $4 colred " | pid: " colclr $9 colred " | name: " colclr procname;  }' | column -t -s "|"

It's a bit overkill, so I added color to the output for good measure. Since I'm not going to be able to remember, or want to type, this behemoth. I put it in a bash function and then just call that when needed. Here is said bash function:

macnst (){
    netstat -Watnlv | grep LISTEN | awk '{"ps -o comm= -p " $9 | getline procname;colred="\033[01;31m";colclr="\033[0m"; print colred "proto: " colclr $1 colred " | addr.port: " colclr $4 colred " | pid: " colclr $9 colred " | name: " colclr procname;  }' | column -t -s "|"
}

I have a small collection of these convenience functions in a file that I source from ~/.bash_profile, or ~/.zshrc. This is being added to the collection. It'd be interesting to see other opportunities to make this nicer/slimmer.

Sample Output:

> macns
proto: tcp4     addr.port: 127.0.0.1.9999     pid: 70078    name:  /Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home/bin/java
proto: tcp46    addr.port: *.35729            pid: 70078    name:  /Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home/bin/java
proto: tcp46    addr.port: *.62087            pid: 70078    name:  /Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home/bin/java
proto: tcp46    addr.port: *.62070            pid: 70078    name:  /Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home/bin/java
proto: tcp46    addr.port: *.62085            pid: 70078    name:  /Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home/bin/java
proto: tcp4     addr.port: *.61993            pid: 70043    name:  /Applications/IntelliJ IDEA.app/Contents/MacOS/idea
proto: tcp46    addr.port: *.61992            pid: 70065    name:  /Applications/IntelliJ IDEA.app/Contents/jdk/Contents/Home/jre/bin/java
proto: tcp4     addr.port: 127.0.0.1.42329    pid: 70065    name:  /Applications/IntelliJ IDEA.app/Contents/jdk/Contents/Home/jre/bin/java
proto: tcp4     addr.port: 127.0.0.1.61983    pid: 70043    name:  /Applications/IntelliJ IDEA.app/Contents/jdk/Contents/Home/jre/bin/java
proto: tcp4     addr.port: 127.0.0.1.63342    pid: 70043    name:  /Applications/IntelliJ IDEA.app/Contents/jdk/Contents/Home/jre/bin/java
proto: tcp4     addr.port: 127.0.0.1.6942     pid: 70043    name:  /Applications/IntelliJ IDEA.app/Contents/jdk/Contents/Home/jre/bin/java
proto: tcp4     addr.port: 127.0.0.1.3075     pid: 67931    name:  /Applications/electerm.app/Contents/Frameworks/electerm Helper.app/Contents/MacOS/electerm Helper
proto: tcp6     addr.port: *.58640            pid: 320      name:  /usr/libexec/rapportd
proto: tcp4     addr.port: *.58640            pid: 320      name:  /usr/libexec/rapportd
proto: tcp4     addr.port: 127.0.0.1.9770     pid: 71       name:  /Applications/Pritunl.app/Contents/Resources/pritunl-service
  • Please do not post gifs of Terminal bot paste the text here formatted as code. It is difficult ti copy and also gard to read if you need large fonts – Mark Dec 29 '18 at 20:46
  • The image isn't a gif, it's just a screen showing the output. If you click through you'll see it's a PNG. Hosted on imgur.. but that's what StackExchange defaults to I guess. Anyway, if you're looking for a different screenshot, I can re-grab. – kroolk Dec 30 '18 at 9:44
  • OK in my comment replace GIF by screenshot it does not matter which format. The point is it should be text – Mark Dec 30 '18 at 10:18
  • Since removing the long command from the sample output, it would actually now work better as a screenshot, since there's nothing that needs to be copied from it, and since it can also crop better than before. – mwfearnley Mar 19 at 11:55

You must log in to answer this question.

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