概要
博客
原帖收藏于IT老兵博客
前言
在 Linux 下,查看占用端口的进程可以使用netstat -antpl | grep
port
,但是在 Mac 下,这个netstat
的功能缩水了(很多命令在 Mac 下都缩水了),所以怎么查看占用端口的进程呢?
正文
搜索了一遍,感觉可以使用lsof -i tcp:{port}
这个命令来实现,{port}
表示端口号。
1 | -i [i] selects the listing of files any of whose Internet address |
查看一下参考中的手册,-i
的参数是用于检索 Internet 相关的信息,例如地址、协议等,上面那个用法就是检索tcp协议,端口号为指定端口号的句柄。
查看所有监听的 tcp 端口。1
sudo lsof -nP -iTCP -sTCP:LISTEN
-n - 用数字来显示端口,而不是约定的服务名。
-p - 显示数字化的 IP 地址,而不是域名。
-iTCP -sTCP:LISTEN - 只显示监听状态的网络文件.
参考
http://man7.org/linux/man-pages/man8/lsof.8.html
https://linuxize.com/post/check-listening-ports-linux/#:~:text=Check%20Listening%20Ports%20with%20lsof%20%23%20lsof%20is,-iTCP%20-sTCP%3ALISTEN.%20The%20options%20used%20are%20as%20