使用 lsof 代替 Mac OS X 中的 netstat 查看占用端口的程序

Posted in :

在 linux 裡可以使用

netstat -tulpn | grep LISTON

來看所有listen 的 port.

Mac OS 只有 -tuln 沒有 -p 的參數。


Depending on your version of Mac OS X, use one of these:

lsof -n -i4TCP:$PORT | grep LISTEN
lsof -n -iTCP:$PORT | grep LISTEN
lsof -n -i:$PORT | grep LISTEN

Substitute $PORT with the port number or a comma-separated list of port numbers.

Prepend sudo (followed by a space) if you need information on ports below #1024.

The -n flag is for displaying IP addresses instead of host names. This makes the command execute much faster, because DNS lookups to get the host names can be slow (several seconds or a minute for many hosts).

See the comments for more options.


查看当前所有监听的端口以及对应的Command和PID

lsof -nP -iTCP -sTCP:LISTEN

输出占用该端口的 PID

lsof -nP -iTCP:4000 |grep LISTEN|awk '{print $2;}'

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *