惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园_首页
大猫的无限游戏
大猫的无限游戏
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Apple Machine Learning Research
Apple Machine Learning Research
B
Blog
B
Blog RSS Feed
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
P
Proofpoint News Feed
MyScale Blog
MyScale Blog
Engineering at Meta
Engineering at Meta
量子位
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
Tailwind CSS Blog
Stack Overflow Blog
Stack Overflow Blog
N
Netflix TechBlog - Medium
T
The Blog of Author Tim Ferriss
U
Unit 42
aimingoo的专栏
aimingoo的专栏
博客园 - 叶小钗
博客园 - 【当耐特】
云风的 BLOG
云风的 BLOG
博客园 - Franky
博客园 - 聂微东

钟意博客

国家统计局数据查询 MCP 照见·硅·肆 涌现之前 照见·硅·叁 换行重启 照见·硅·贰 钢筋水泥 照见·硅·壹 你好世界 照见·硅·零 序言 大语言模型的不确定性 科艺知识库 ARM64 胶东半岛观察 海光 K100 DCU VLLM 推理环境构建 博客多平台负载均衡方案 浅谈RAG Web技术构建桌面应用方案 Spring WebSocket 错误 SpringBoot WebSocket 代理模式、客户端模式 Spring AOP 调用自身失效 Coze同插件不同工具之间代码复用 Pachelbel's Greatest Hit: The Ultimate Canon - 纪念帕海贝尔:终极卡农 圣诞快乐,劳伦斯先生 Merry Christmas Mr. Lawrence
window 端口占用但是查不到
钟意 · 2025-03-07 · via 钟意博客

错误情景

  • 系统:
    • window11
    • docker run in wsl
  • 操作:
    • idea run nacos

错误诱因

Window 默认预留的 TCP 动态端口范围与需要启动的服务端口冲突导致。

所以查不来的原因是端口确实未使用,但是保留。

解决方案

  1. 查看windows保留端口序列是否在冲突范围,默认应该是1024开始,步长为13977。所以我nacos的8848、9848都在里面

    查看保留端口序列
    1
    netsh int ipv4 show dynamicport tcp
  2. 修改端口默认起始与步长,设置为自己不常用的区间。

  • start: 起始值
  • num: 长度
    修改保留端口序列
    1
    netsh int ipv4 set dynamicport tcp start=30000 num=13977

结尾

分享一个如果端口存在就kill端口的命令 e.g. killIf 8848

killIf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
function killIf {
param (
[Parameter(Mandatory = $true)]
[int]$Port,

[switch]$l
)

try {
# 获取 netstat 输出并过滤与端口相关的信息
$processInfo = netstat -ano | Select-String -Pattern ":\b${Port}\b"

# 判空处理
if ($null -eq $processInfo -or $processInfo.Length -eq 0) {
Write-Host "端口 $Port 未被占用。" -ForegroundColor Yellow
Write-Host "提示:无法使用该端口,请检查是否有其他服务在使用,或尝试重启电脑。" -ForegroundColor Green
return
}

# 提取唯一的进程ID
$processIds = $processInfo | ForEach-Object { ($_ -split '\s+')[-1] } | Select-Object -Unique

foreach ($ProcId in $processIds) {
try {
# 确认 PID 是否为有效的数字
if ($ProcId -match '^\d+$') {
if ($l) {
Write-Host "端口 $Port 存在进程号: ${ProcId}" -ForegroundColor Yellow
}
# 尝试终止进程
taskkill /PID $ProcId /F
Write-Host "已终止进程 ${ProcId}, 释放端口 $Port 完毕。" -ForegroundColor Green
} else {
Write-Host "不正确的进程号: ${ProcId}" -ForegroundColor Red
}
}
catch {
Write-Host "终止进程 ${ProcId} 失败: $($_.Exception.Message)" -ForegroundColor Red
}
}
}
catch {
Write-Host "发生错误: $($_.Exception.Message)" -ForegroundColor Red
}
}

它是一个​PowerShell 脚本​(扩展名为 .ps1),放到 $PROFILE 这个变量下面就行,直接在命令行输入 $PROFILE 有地址。