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

查看:436
本文介绍了谁在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 High Sierra和更高版本上,使用以下命令:

On macOS High Sierra and later, use this command:

lsof -nP -iTCP:$PORT | grep LISTEN

或仅查看IPv4:

lsof -nP -i4TCP:$PORT | grep LISTEN

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

On older versions, use one of the following forms:

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

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

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

添加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之类的已解析名称或诸如dpservesocalia之类的更深奥的服务名称.

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

查看评论以获取更多选项.

See the comments for more options.

出于完整性考虑,因为经常一起使用:

For completeness, because frequently used together:

要杀死PID:

kill -9 <PID>
# kill -9 60401

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

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