Mac OS 查看网络端口的方式不同于 Linux,主要是 MacOS系统下的 netstat 命令的功能比较鸡肋

Mac OS 系统下,查看网络端口号的方式,主要有 netstat、lsof、网络实用工具(Mac OS 自带的)

 

1. netstat

$ netstat --help
Usage:  netstat [-AaLlnW] [-f address_family | -p protocol]
        netstat [-gilns] [-f address_family]
        netstat -i | -I interface [-w wait] [-abdgRtS]
        netstat -s [-s] [-f address_family | -p protocol] [-w wait]
        netstat -i | -I interface -s [-f address_family | -p protocol]
        netstat -m [-m]
        netstat -r [-Aaln] [-f address_family]
        netstat -rs [-s]

 

1)netstat 默认查看

$ netstat
Active Internet connections
Proto Recv-Q Send-Q  Local Address          Foreign Address        (state)    
tcp4       0      0  192.168.31.191.62976   ti-in-f113.1e100.https SYN_SENT   
tcp4       0    371  192.168.31.191.62975   a104-118-66-248..https ESTABLISHED
tcp4       0      0  localhost.62905        localhost.cslistener   ESTABLISHED
tcp4       0      0  localhost.8084         localhost.62904        ESTABLISHED
tcp4       0      0  localhost.62904        localhost.8084         ESTABLISHED
tcp4       0      0  192.168.31.191.50367   101.226.211.105.http-a ESTABLISHED
tcp4       0      0  192.168.31.191.59890   47.95.6.112.ssh        ESTABLISHED
tcp4       0      0  192.168.31.191.56988   tk-in-f188.1e100.5228  ESTABLISHED
tcp4       0      0  192.168.31.191.56286   17.252.156.21.5223     ESTABLISHED
tcp4       0      0  localhost.60280        localhost.60281        ESTABLISHED
tcp4      37      0  192.168.31.191.62061   mail.eclipse.org.https CLOSE_WAIT 
tcp4       0      0  localhost.30100        localhost.54910        CLOSE_WAIT 
tcp4       0      0  localhost.ssh          localhost.56570        ESTABLISHED

 

2)netstat 查看端口号

netstat -an | grep 9000

 

3)netstat 查看监听

netstat -an -p tcp | grep -iE "LISTEN"

 

 

2. lsof

1)查看端口号

sudo lsof -i :9000

$ sudo lsof -i :9000
COMMAND   PID   USER   FD   TYPE            DEVICE SIZE/OFF NODE NAME
php-fpm 34223   root   11u  IPv4 0x452eab886c917f5      0t0  TCP localhost:cslistener (LISTEN)
php-fpm 34224 nobody    0u  IPv4 0x452eab886c917f5      0t0  TCP localhost:cslistener (LISTEN)
php-fpm 34224 nobody    7u  IPv4 0x452eab89a96e275      0t0  TCP localhost:cslistener->localhost:63956 (CLOSE_WAIT)
php-fpm 34225 nobody    0u  IPv4 0x452eab886c917f5      0t0  TCP localhost:cslistener (LISTEN)
php-fpm 34226 nobody    0u  IPv4 0x452eab886c917f5      0t0  TCP localhost:cslistener (LISTEN)

 

2)lsof 更多命令

sudo lsof -i :9000
sudo lsof -P -i tcp:80
sudo lsof -i -P | grep -i "listen"

 

 

3. 网络实用工具Network Utility

Mac OS 10.9 以后,自带的网络工具就隐藏了,找到打开步骤如下

方法1)spotlight 搜索

查看Mac系统右上角,点击放大镜搜索 ——> 输入 Network Utility

 

方法2)查找出隐藏的路径

查看Mac系统左上角,依次点击 苹果图标 ——> 关于本机  ——> Overview  ——>  System Report  ——>  Window  ——>  Network Utility

之前网络实用工具在常见工具里,不知道苹果为什么要隐藏此工具,垄断的苹果可能不需要理由吧

 

指定端口号,点击扫描

 

 

 

参考推荐

Mac OS 80 端口占用问题的解决方法

Mac 远程连接失败:ssh: connect to host localhost port 22: Connection refused

Mac 电脑锁屏快捷方法

Mac 系统快捷键大全

Mac 高效程序猿开发必备软件