谁在 Mac OS X 上监听给定的 TCP 端口? [英] Who is listening on a given TCP port on Mac OS X?

查看:31
本文介绍了谁在 Mac OS X 上监听给定的 TCP 端口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Linux 上,我可以使用 netstat -pntl |grep $PORTfuser -n tcp $PORT 找出哪个进程 (PID) 正在侦听指定的 TCP 端口.如何在 Mac OS X 上获得相同的信息?

On Linux, I can use netstat -pntl | grep $PORT or fuser -n tcp $PORT to find out which process (PID) is listening on the specified TCP port. How do I get the same information on Mac OS X?

推荐答案

在 macOS Big Sur 及更高版本上,使用以下命令:

On macOS Big Sur and later, use this command:

sudo lsof -i -P | grep LISTEN

或者只查看 IPv4:

or to just see just IPv4:

sudo lsof -nP -i4TCP:$PORT | grep LISTEN

在旧版本上,使用以下形式之一:

On older versions, use one of the following forms:

sudo lsof -nP -iTCP:$PORT | grep LISTEN
sudo lsof -nP -i:$PORT | grep LISTEN

用端口号或以逗号分隔的端口号列表替换 $PORT.

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

如果您需要有关 #1024 以下端口的信息,请在前面加上 sudo(后跟一个空格).

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

-n 标志用于显示 IP 地址而不是主机名.这使得命令执行得更快,因为获取主机名的 DNS 查找可能很慢(许多主机需要几秒或一分钟).

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).

-P 标志用于显示原始端口号而不是解析名称,如 httpftp 或更深奥的服务名称,如 dpserve, socalia.

The -P flag is for displaying raw port numbers instead of resolved names like http, ftp or more esoteric service names like dpserve, socalia.

更多选项见评论.

为了完整性,因为经常一起使用:

For completeness, because frequently used together:

杀死PID:

sudo kill -9 <PID>
# kill -9 60401

这篇关于谁在 Mac OS X 上监听给定的 TCP 端口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆