使用 lsof 代替 Mac OS X 中的 netstat 檢視佔用埠的程式
阿新 • • 發佈:2018-12-27
在 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;}'