Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

On my linux servers I have this alias set:

alias ns="sudo lsof -Pi|grep -v ESTABL|grep -v CLOSED|grep -v -e '->' | 
tr -s ' ' '\t' | awk '{print $3 "\t" $8 "\t" $5 "\t" $9 "\t" $1}'|
column -t|sort"

It gives me output like this:

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       User       Inode       PID/Program name
tcp        0      0   *               LISTEN      root       10459       2330/imap-login 
tcp        0      0 *               LISTEN      root       9646        4095/sshd       
tcp        0      0    *               LISTEN      root       10762       4724/master     
tcp        0      0   *               LISTEN      root       8576        362/apache2     
tcp        0      0    *               LISTEN      root       8574        362/apache2     
tcp        0      0   *               LISTEN      root       10460       2330/imap-login 
tcp        0      0*               LISTEN      root       10461       4639/dovecot    
tcp        0      0*               LISTEN      mysql      10967       3454/mysqld     
tcp        0      0 *               LISTEN      root       9748        4371/  

I want the same output for OSX, but lsof etc. are behaving differently. I think because it comes from BSD.

The problems i'm having:

  • When I just type the alias "ns" nothing is outputted.
  • When I type the whole command I get the following output


USER            NODE    TYPE  NAME                        COMMAND
_locationd      UDP     IPv4  *:*                         locationd
_mdnsresponder  UDP     IPv4  *:5353                      mDNSRespo
_mdnsresponder  UDP     IPv4  *:54398                     mDNSRespo
_mdnsresponder  UDP     IPv6  *:5353                      mDNSRespo
_mdnsresponder  UDP     IPv6  *:54398                     mDNSRespo
_netbios        UDP     IPv4  *:137                       netbiosd
_netbios        UDP     IPv4  *:138                       netbiosd

I need:

  • numeric ports and adresses
  • the whole command and PID
share|improve this question
I don't know howto do that on Mac OSX, but on Linux you could as well do netstat -ntpl for TCP or netstat -nupl for UDP –  mauro.stettler Dec 30 '13 at 19:22
If you gave us an example of the lsof output on OSX we might be able to help you parse it. Did you try netstat as suggested by @mauro? –  terdon Dec 30 '13 at 19:30
I added more info. I tried netstat, but i had troubles with it at well. I can't get the PID to be displayed, or the ports limited to address-family. –  Fabian Zeindl Dec 30 '13 at 20:09

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.