谁在 Mac OS X 上监听给定的 TCP 端口?
在 Linux 上,我可以使用 netstat -pntl | grep $PORT 或 fuser -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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(19)
在 macOS
Big Sur
及更高版本上,使用此命令:或者仅查看 IPv4:
在旧版本上,使用以下形式之一:
将
$PORT
替换为端口号或以逗号分隔的端口号列表。如果您需要 1023 以上端口的信息,则可能不需要 sudo。
-n
标志用于显示 IP 地址而不是主机名。这使得命令执行速度更快,因为获取主机名的 DNS 查找可能很慢(对于许多主机来说需要几秒或一分钟)。-P
标志用于显示原始端口号,而不是显示解析名称,例如http
、ftp
或更深奥的服务名称,例如dpserve< /code>,
socalia
。请参阅评论以获取更多选项。
为了完整起见,因为经常一起使用:
杀死 PID:
On macOS
Big Sur
and later, use this command:or to just see just IPv4:
On older versions, use one of the following forms:
Substitute
$PORT
with the port number or a comma-separated list of port numbers.sudo
may not be needed if you need information on ports above 1023.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).The
-P
flag is for displaying raw port numbers instead of resolved names likehttp
,ftp
or more esoteric service names likedpserve
,socalia
.See the comments for more options.
For completeness, because frequently used together:
To kill the PID:
macOS 的每个版本都支持此功能:
sudo lsof -iTCP -sTCP:LISTEN -n -P
就个人而言,我最终在我的
~/.bash_profile
中得到了这个简单的函数:然后
listening
命令会为您提供在某个端口上侦听的进程列表,并且listening smth
将其greps为某种模式。有了这个,就可以很容易地询问特定进程,例如
listening dropbox
,或端口,例如listening 22
。lsof 命令有一些专门的选项用于询问端口、协议、进程等。但我个人发现上述功能更方便,因为我不需要记住所有这些低级选项。 lsof 是一个非常强大的工具,但不幸的是使用起来不太舒服。
Every version of macOS supports this:
sudo lsof -iTCP -sTCP:LISTEN -n -P
Personally I've end up with this simple function in my
~/.bash_profile
:Then
listening
command gives you a listing of processes listening on some port andlistening smth
greps this for some pattern.Having this, it's quite easy to ask about particular process, e.g.
listening dropbox
, or port, e.g.listening 22
.lsof
command has some specialized options for asking about port, protocol, process etc. but personally I've found above function much more handy, since I don't need to remember all these low-level options.lsof
is quite powerful tool, but unfortunately not so comfy to use.您还可以使用:
这适用于 Mavericks。
You can also use:
This works in Mavericks.
2016 年 1 月更新
真的很惊讶没有人建议:
获取所需的基本信息。例如,检查端口 1337:
其他变化,具体取决于情况:
您可以轻松地在此基础上提取 PID 本身。例如:(
结果)与此命令等效:
快速说明:
为了完整性,因为经常一起使用:
杀死 PID:
或作为单行:
Update January 2016
Really surprised no-one has suggested:
to get the basic information required. For instance, checking on port 1337:
Other variations, depending on circumstances:
You can easily build on this to extract the PID itself. For example:
which is also equivalent (in result) to this command:
Quick illustration:
For completeness, because frequently used together:
To kill the PID:
or as a one liner:
对于 LISTEN、ESTABLISHED 和 CLOSED 端口:
仅适用于 LISTEN 端口:
对于特定 LISTEN 端口(例如端口 80):
或者,如果您只想要一个紧凑的摘要 [未描述服务/应用程序],请使用
netstat.这里好的一面是,不需要 sudo:
解释使用
lsof
选项的选项(有关更多详细信息,请参阅
man lsof
):- 主机名
-i
-P
省略端口名称netstat
选项(有关更多详细信息,请参阅< code>man netstat):-a
对于所有套接字-n
不解析名称,将网络地址显示为数字在 High Sierra 10.13 上测试.3 和 Mojave 10.14.3
最后一个语法
netstat
也适用于 Linux [apt install net-tools]lsof 在某些 Linux 上默认存在分布
For the LISTEN, ESTABLISHED and CLOSED ports:
For the LISTEN ports only:
For a specific LISTEN port (e.g. port 80):
Or if you just want a compact summary [no service/apps described], go by
netstat
. The good side here is, no sudo needed:Explaining the options used
lsof
options (for more details seeman lsof
):-n
suppress the host name-i
for IPv4 and IPv6 protocols-P
omit port namesnetstat
options (for more details seeman netstat
):-a
for all sockets-n
don't resolve names, show network addresses as numbersTested on High Sierra 10.13.3 and Mojave 10.14.3
The last syntax
netstat
works on Linux too [apt install net-tools]lsof comes by default on some Linux distribution
在 OS X 上,您可以使用 netstat 的 -v 选项来给出关联的 pid。
类型:
输出将如下所示:
PID 是最后一列之前的数字,本例为 3105
on OS X you can use the -v option for netstat to give the associated pid.
type:
the output will look like this:
The PID is the number before the last column, 3105 for this case
在 macOS 上,以下是使用 netstat 获取正在侦听特定端口的进程 ID 的简单方法。此示例查找在端口 80 上提供内容的进程:
find server running on port 80
示例输出
最后一列中的第二列是 PID。在上面,它是715。
options
-a
- 显示所有端口,包括服务器使用的端口-n
- 显示数字,不查找名称。这使得命令速度很多更快-v
- 详细输出,以获取进程 ID-w
- 搜索词。否则,该命令将返回端口 8000 和 8001 的信息,而不仅仅是“80”LISTEN
- 仅提供处于 LISTEN 模式的端口(即服务器)的信息On macOS, here's an easy way to get the process ID that's listening on a specific port with netstat. This example looks for a process serving content on port 80:
find server running on port 80
sample output
The 2nd from the last column is the PID. In above, it's 715.
options
-a
- show all ports, including those used by servers-n
- show numbers, don't look up names. This makes the command a lot faster-v
- verbose output, to get the process IDs-w
- search words. Otherwise the command will return info for ports 8000 and 8001, not just "80"LISTEN
- give info only for ports in LISTEN mode, i.e. servers在最新的 macOS 版本上,您可以使用此命令:
如果您发现很难记住,那么也许您应该创建一个 bash 函数并使用更友好的名称将其导出,如下所示
,然后向其中添加以下行文件并保存。
现在,您可以在终端中输入
listening_on 80
并查看哪个进程正在侦听端口80
。On the latest macOS version you can use this command:
If you find it hard to remember then maybe you should create a
bash
function and export it with a friendlier name like soand then add the following lines to that file and save it.
Now you can type
listening_on 80
in your Terminal and see which process is listening on port80
.我想分享这个命令,它运行良好并打印标题,以便您知道哪一列是哪一列:
示例输出:
I wanted to share this command which works well and prints the headers so you know which column is which:
Example Output:
在 Snow Leopard (OS X 10.6.8) 上,运行“man lsof”会产生:(
实际手动输入是“lsof -i 4 -a -p 1234”)
前面的答案在 Snow Leopard 上不起作用,但我正在尝试使用“netstat -nlp”,直到我在 pts 的答案中看到“lsof”的使用。
On Snow Leopard (OS X 10.6.8), running 'man lsof' yields:
(actual manual entry is 'lsof -i 4 -a -p 1234')
The previous answers didn't work on Snow Leopard, but I was trying to use 'netstat -nlp' until I saw the use of 'lsof' in the answer by pts.
我是一个 Linux 人。在 Linux 中,使用 netstat -ltpn 或这些字母的任意组合非常容易。但在 Mac OS X 中
netstat -an | grep LISTEN
是最人性化的。其他的则非常丑陋,并且在故障排除时很难记住。I am a Linux guy. In Linux it is extremely easy with
netstat -ltpn
or any combination of those letters. But in Mac OS Xnetstat -an | grep LISTEN
is the most humane. Others are very ugly and very difficult to remember when troubleshooting.这显示谁在做什么。删除 -n 以查看主机名(有点慢)。
This displays who's doing what. Remove -n to see hostnames (a bit slower).
签出此项目/工具: procs
在 MacO 上安装:
brew install procs
这允许您可以使用
procs
控制显示内容。要查看 TCP/UDP 端口,请在安装该工具后将以下内容添加到
~/.procs.toml
中。以下是示例输出:
checkout this project/tool: procs
install on MacOs:
brew install procs
This allows you control what to display with
procs
.To see TCP/UDP Ports, add below to
~/.procs.toml
after installing the tool.Here is a sample output:
这做到了我所需要的。
This did what I needed.
我制作了一个小脚本,不仅可以查看谁在哪里收听,还可以显示已建立的联系以及与哪些国家/地区的联系。适用于 OSX Siera
这可能有助于检查您是否连接到朝鲜! ;-)
I made a small script to see not only who is listening where but also to display established connections and to which countries. Works on OSX Siera
This may be useful to check if you are connected to north-korea! ;-)
这是 macOS High Sierra 上的好方法:
This is a good way on macOS High Sierra:
受到用户 Brent Self 的启发:
lsof -i 4 -a | grep 听
Inspired by user Brent Self:
lsof -i 4 -a | grep LISTEN
对于 macOS,我同时使用两个命令来显示有关在计算机上侦听的进程和连接到远程服务器的进程的信息。换句话说,要检查主机上的侦听端口和当前(TCP)连接,您可以一起使用以下两个命令
我想添加我的输入,希望它最终可以帮助某人。
For macOS I use two commands together to show information about the processes listening on the machine and process connecting to remote servers. In other words, to check the listening ports and the current (TCP) connections on a host you could use the two following commands together
Thought I would add my input, hopefully it can end up helping someone.
只是对Michał Kalinowski的回答稍有改进(我没有足够的声誉在那里发表评论):如果你是尝试查找侦听编号为 255 及以下的端口的进程时,
grep
命令可能会打印与 IP 地址相关的行,但这些行与所需的结果不符。对于任意编号的端口,grep
命令也可能错误地匹配设备的 MAC 地址或 PID。为了改进这一点,我建议将命令更改为grep --color ":$1 "
Just a slight improvement on Michał Kalinowski's answer (I don't have enough reputation to leave a comment there): if you are trying to find the process listening on a port numbered 255 and below, the
grep
command might print lines related to the IP address, and which do not correspond to the desired result. For a port with any number, thegrep
command might also erroneously match the device's MAC address or PID. To improve on this, I suggest changing the command togrep --color ":$1 "