445 字
2 分钟
解决部分应用在 Windows 平台下端口被随机占用的问题

English Version: Solving the issue of ports being randomly occupied by some applications on the Windows platform

太长不看#

  1. 进入管理员终端,PowerShell 或者 CMD 都可以。
  2. 键入 netsh int ipv4 show dynamicport tcp 查看起始端口号是否为 1024,如果是,恭喜你遇到了和我一样的问题,不是则自求多福。
  3. 键入 netsh int ipv4 set dynamic tcp start=49152 num=16384netsh int ipv6 set dynamic tcp start=49152 num=16384 重设起始端口号。
  4. 重启电脑

原因#

Windows 可能在某次更新中会意外的重设动态端口范围至 1024,这其实和 Hyper-V 无关,但是!Hyper-V会随机占用在这个范围内的几千个端口作为保留服务,你还不能通过 netstat -ano 发现这一点。正常的动态端口范围应是 49152-6553512

显然,很多 APP 都会使用地位端口,比如 VBAN 默认使用的 6980,如果这个端口极为幸运的被 Hyper-V 所使用,其他应用就不能使用了。如果你的代理或者其他下载软件出现了这个问题,可以尝试使用 netsh int ipv4 show excludedportrange protocol=tcp 来检查是否被占用。

English#

TL;DR#

  1. Open an administrator terminal (PowerShell or CMD).
  2. Type netsh int ipv4 show dynamicport tcp to check if the starting port number is 1024. If it is, you’ve encountered the same issue as me. If not, you’re on your own.
  3. Type netsh int ipv4 set dynamic tcp start=49152 num=16384 and netsh int ipv6 set dynamic tcp start=49152 num=16384 to reset the starting port number.
  4. Restart your computer.

Reason#

Windows may accidentally reset the dynamic port range to 1024 during an update. This is actually unrelated to Hyper-V, but Hyper-V will randomly occupy several thousand ports within this range as reserved services, and you can’t even detect this using netstat -ano. The normal dynamic port range should be 49152-6553512.

Obviously, many apps use lower ports, such as VBAN which defaults to 6980. If this port is unluckily used by Hyper-V, other applications won’t be able to use it. If your proxy or other download software encounters this problem, you can try using netsh int ipv4 show excludedportrange protocol=tcp to check if it’s being occupied.

脚注#

  1. https://learn.microsoft.com/en-us/troubleshoot/windows-server/networking/default-dynamic-port-range-tcpip-chang 2

  2. https://www.techtarget.com/searchnetworking/definition/dynamic-port-numbers#:~:text=Dynamic%20ports%2C%20also%20known%20as%20private%20or%20ephemeral,ports%2049152%20to%2065535%20and%20are%20never%20assigned. 2

解决部分应用在 Windows 平台下端口被随机占用的问题
https://nptr.cc/posts/resolve-some-app-cannot-use-port-in-randomely/
作者
Nullpinter
发布于
2024-09-03