如何找出哪个进程正在侦听 Windows 上的端口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:12:54  来源:igfitidea点击:

How can you find out which process is listening on a port on Windows?

windowsnetworkingport

提问by Readonly

How can you find out which process is listening on a port on Windows?

如何找出哪个进程正在侦听 Windows 上的端口?

回答by Brad Wilson

New answer, powershell

新答案,powershell

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

Old answer, cmd

旧答案,cmd

 C:\> netstat -a -b

(Add -nto stop it trying to resolve hostnames, which will make it a lot faster.)

(添加-n以阻止它尝试解析主机名,这将使它更快。)

Note Dane's recommendation for TCPView. It looks very useful!

注意 Dane 对TCPView的建议。它看起来非常有用!

-aDisplays all connections and listening ports.

-a显示所有连接和侦听端口。

-bDisplays the executable involved in creating each connection or listening port. In some cases well-known executables host multiple independent components, and in these cases the sequence of components involved in creating the connection or listening port is displayed. In this case the executable name is in [] at the bottom, on top is the component it called, and so forth until TCP/IP was reached. Note that this option can be time-consuming and will fail unless you have sufficient permissions.

-b显示创建每个连接或侦听端口所涉及的可执行文件。在某些情况下,众所周知的可执行文件包含多个独立组件,在这些情况下,会显示创建连接或侦听端口所涉及的组件序列。在这种情况下,可执行文件名称在底部的 [] 中,顶部是它调用的组件,依此类推,直到达到 TCP/IP。请注意,此选项可能很耗时,并且除非您有足够的权限,否则会失败。

-nDisplays addresses and port numbers in numerical form.

-n以数字形式显示地址和端口号。

-oDisplays the owning process ID associated with each connection.

-o显示与每个连接关联的拥有进程 ID。

回答by bcorso

There's a native GUI for Windows:

有一个适用于 Windows 的本机 GUI:

  • Start menu → All ProgramsAccessoriesSystem ToolsResource Monitor
  • 开始菜单 →所有程序附件系统工具资源监视器

Or Runresmon.exe, or from Task Manager'sperformance tab.

Runresmon.exe,或从任务管理器的性能选项卡。

Enter image description here

在此处输入图片说明

回答by Dane

Use TCPViewif you want a GUI for this. It's the old Sysinternalsapplication that Microsoft bought out.

如果您需要 GUI,请使用TCPView。这是微软收购的旧Sysinternals应用程序。

回答by aku

For Windows:

对于 Windows:

netstat -aon | find /i "listening"

回答by Ram Sharma

The -b switch mentioned in most answers requires you to have administrative privileges on the machine. You don't really need elevated rights to get the process name!

大多数答案中提到的 -b 开关要求您在机器上具有管理权限。您真的不需要提升权限来获取进程名称!

Find the pid of the process running in the port number (e.g., 8080)

在端口号(例如8080)中找到运行的进程的pid

netstat -ano | findStr "8080"

Find the process name by pid

通过pid查找进程名

tasklist /fi "pid eq 2216"

find process by TCP/IP port

通过 TCP/IP 端口查找进程

回答by Nathan24

You can get more information if you run the following command:

如果您运行以下命令,您可以获得更多信息:

netstat -aon | find /i "listening" |find "port"

using the 'Find' command allows you to filter the results. find /i "listening"will display only ports that are 'Listening'. Note, you need the /ito ignore case, otherwise you would type find "LISTENING". | find "port"will limit the results to only those containing the specific port number. Note, on this it will also filter in results that have the port number anywhere in the response string.

使用“查找”命令可以过滤结果。find /i "listening"将仅显示“正在侦听”的端口。请注意,您需要/i忽略大小写,否则您将键入 find "LISTENING"。| find "port"将结果限制为仅包含特定端口号的结果。请注意,在此方面,它还将过滤在响应字符串中任何位置具有端口号的结果。

回答by Cyborg

  1. Open a command prompt window (as Administrator) From "Start\Search box" Enter "cmd" then right-click on "cmd.exe" and select "Run as Administrator"

  2. Enter the following text then hit Enter.

    netstat -abno

    -aDisplays all connections and listening ports.

    -bDisplays the executable involved in creating each connection or listening port. In some cases well-known executables host multiple independent components, and in these cases the sequence of components involved in creating the connection or listening port is displayed. In this case the executable name is in [] at the bottom, on top is the component it called, and so forth until TCP/IP was reached. Note that this option can be time-consuming and will fail unless you have sufficient permissions.

    -nDisplays addresses and port numbers in numerical form.

    -oDisplays the owning process ID associated with each connection.

  3. Find the Port that you are listening on under "Local Address"

  4. Look at the process name directly under that.

  1. 打开命令提示符窗口(以管理员身份)从“开始\搜索框”输入“cmd”然后右键单击“cmd.exe”并选择“以管理员身份运行”

  2. 输入以下文本,然后按 Enter。

    netstat -abno

    -a显示所有连接和侦听端口。

    -b显示创建每个连接或侦听端口所涉及的可执行文件。在某些情况下,众所周知的可执行文件包含多个独立组件,在这些情况下,会显示创建连接或侦听端口所涉及的组件序列。在这种情况下,可执行文件名称在底部的 [] 中,顶部是它调用的组件,依此类推,直到达到 TCP/IP。请注意,此选项可能很耗时,并且除非您有足够的权限,否则会失败。

    -n以数字形式显示地址和端口号。

    -o显示与每个连接关联的拥有进程 ID。

  3. 在“本地地址”下找到您正在侦听的端口

  4. 查看直接在其下的进程名称。

NOTE: To find the process under Task Manager

注意:在任务管理器下查找进程

  1. Note the PID (process identifier) next to the port you are looking at.

  2. Open Windows Task Manager.

  3. Select the Processes tab.

  4. Look for the PID you noted when you did the netstat in step 1.

    • If you don't see a PID column, click on View / Select Columns. Select PID.

    • Make sure “Show processes from all users” is selected.

  1. 请注意您正在查看的端口旁边的 PID(进程标识符)。

  2. 打开 Windows 任务管理器。

  3. 选择进程选项卡。

  4. 查找您在步骤 1 中执行 netstat 时记下的 PID。

    • 如果您没有看到 PID 列,请单击“查看/选择列”。选择PID。

    • 确保选中“显示所有用户的进程”。

回答by ROMANIA_engineer

Get PID and Image Name

获取 PID 和图像名称

Use only one command:

只使用一个命令:

for /f "tokens=5" %a in ('netstat -aon ^| findstr 9000') do tasklist /FI "PID eq %a"

where 9000should be replaced by your port number.

where9000应该替换为您的端口号。

The outputwill contain something like this:

输出将包含类似这样:

Image Name                     PID Session Name        Session#    Mem Usage
========================= ======== ================ =========== ============
java.exe                      5312 Services                   0    130,768 K


Explanation:

解释:

  • it iterates through every line from the output of the following command:

    netstat -aon | findstr 9000
    
  • from every line, the PID (%a- the name is not important here) is extracted (PID is the 5thelement in that line) and passed to the following command

    tasklist /FI "PID eq 5312"
    
  • 它遍历以下命令输出的每一行:

    netstat -aon | findstr 9000
    
  • 从每一行中,%a提取PID(- 名称在这里不重要)(PID 是该行中的5第 th 个元素)并传递给以下命令

    tasklist /FI "PID eq 5312"
    


If you want to skipthe headerand the return of the command prompt, you can use:

如果你想跳过头部和返回命令提示符下,你可以使用:

echo off & (for /f "tokens=5" %a in ('netstat -aon ^| findstr 9000') do tasklist /NH /FI "PID eq %a") & echo on

Output:

输出:

java.exe                      5312 Services                   0    130,768 K

回答by Pankaj Pateriya

First we find the process id of that particular task which we need to eliminate in order to get the port free:

首先,我们找到该特定任务的进程 id,我们需要消除该进程以释放端口:

Type

类型

netstat -n -a -o

After executing this command in the Windows command line prompt (cmd), select the pid which I think the last column. Suppose this is 3312.

在 Windows 命令行提示符 (cmd) 中执行此命令后,选择我认为最后一列的 pid。假设这是 3312。

Now type

现在输入

taskkill /F /PID 3312

You can now cross check by typing the netstatcommand.

您现在可以通过键入netstat命令进行交叉检查。

NOTE: sometimes Windows doesn't allow you to run this command directly on CMD, so first you need to go with these steps:

注意:有时 Windows 不允许您直接在 CMD 上运行此命令,因此首先您需要执行以下步骤:

From the start menu -> command prompt (right click on command prompt, and run as administrator)

从开始菜单 -> 命令提示符(右键单击命令提示符,并以管理员身份运行)

回答by Monis Majeed

To get a list of all the owning process IDs associated with each connection:

要获取与每个连接关联的所有拥有进程 ID 的列表:

netstat -ao |find /i "listening"

If want to kill any process have the ID and use this command, so that port becomes free

如果要杀死任何有ID的进程并使用此命令,则该端口变为空闲

Taskkill /F /IM PID of a process