20

Windows/Linux 系统中获取端口被哪个应用程序占用

 4 years ago
source link: https://blog.walterlv.com/post/get-port-owning-process.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

管理服务程序的时候,可能会查询某个端口当前被哪个进程占用。不仅能找出有问题的进程将其处理掉,也可以用来辅助检查某个程序是否开启了服务并在监听端口。

Windows 系统

Windows 系统上可以使用 PowerShell 命令来查询占用某个端口的程序。

比如,我们需要查询 5000 端口被占用的进程是谁,可以在 PowerShell 中输入命令:

Get-Process -Id (Get-NetTCPConnection -LocalPort 5000).OwningProcess

q2uUVbq.png!web

Linux 系统

在终端中输入命令 lsof 可以查询占用某个端口的进程。

lsof -i:端口号

比如,我们需要查询 5000 端口被占用的进程是谁,可以在中断中输入命令:

walterlv@localhost:~# lsof -i:5000
COMMAND        PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
dotnet_serve   731 root    3u  IPv6  12890      0t0  TCP *:5000

或者使用 netstat 查询。

netstat -tunpl | grep 端口号

举例:

walterlv@localhost:~# netstat -tunpl | grep 35412
tcp6   0   0 :::5000     :::*             731/dotnet_serve

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK