WSL中与DNS相关的问题

场景:配置claude code

一、问题一:curl / npm / apt 报错,提示域名无法解析

问题

  • curl: Could not resolve host
  • npm ERR! getaddrinfo EAI_AGAIN
  • apt: Temporary failure resolving 'archive.ubuntu.com'

原因

  • WSL 内部的 DNS 解析不稳定 / 失败
  • 常见于:
    • WSL2 网络初始化异常
    • DNS 被代理 / 网络环境干扰
    • 系统在 IPv6 / IPv4 间切换导致解析异常

答案

  • 先验证 DNS 是否真的失效:

    1
    2
    getent hosts google.com
    getent hosts registry.npmjs.org
  • 若能解析但应用报错,通常不是 DNS 本身,而是 后续网络出口问题

  • 必要时重启 WSL:

    1
    wsl --shutdown

二、问题二:npm 能访问,但 Claude Code 仍然无法连接

问题

  • claude 报错:

    1
    2
    3
    Unable to connect to Anthropic services
    Failed to connect to api.anthropic.com
    ERR_BAD_REQUEST
  • 但:

    • npm install 已成功
    • claude -v 能输出版本号

原因

  • Claude Code CLI 会直连 api.anthropic.com
  • Anthropic API 按“网络出口国家”做访问控制
  • 与 DNS 是否可解析无关

答案

  • 判断当前网络出口国家:

    1
    curl -s https://ipinfo.io/country
  • 若输出:

    • CN → Claude Code 必然不可用
    • US / JP / SG / EU → 可用

三、问题三:浏览器 Claude 能用,但 WSL / CLI 不能

问题

  • 浏览器中 Claude.ai 可正常登录
  • WSL 中 claude 仍提示地区不支持

原因

  • 浏览器使用的是 浏览器代理
  • CLI / WSL 使用的是 系统网络出口
  • 两者并不共享代理配置

答案

  • 必须让 WSL 的流量也走代理

  • 判断标准永远只有一句话:

    1
    curl -s https://ipinfo.io/country

四、问题四:已使用 Clash Verge,但 WSL 仍然是 CN

问题

  • Windows 下已开启 Clash Verge

  • Clash Verge也已经开启LAN

  • WSL 中设置代理后:

    1
    curl https://ipinfo.io/country

    无输出或仍为 CN

原因

  1. WSL2 与 Windows 使用不同的网络栈
  2. WSL 内的 127.0.0.1 ≠ Windows 的 127.0.0.1
  3. Clash 监听在 Windows 本机,但 WSL 无法通过 localhost 访问

答案

  • 使用 Windows 的真实局域网 IP,而不是 127.0.0.1
  • 例如 Windows WLAN IP 为 172.26.176.1

在 WSL 中设置:

1
2
export HTTP_PROXY=http://172.26.176.1:7897
export HTTPS_PROXY=http://172.26.176.1:7897
  • 同时确保 Clash Verge:

    • ✅ Allow LAN = ON
    • ✅ System Proxy = ON
    • (可选)TUN Mode = ON
  • 重启 WSL:

    1
    wsl --shutdown

永久性配置代理:

1
2
3
echo 'export http_proxy=http://192.168.43.186:7890' >> ~/.bashrc
echo 'export https_proxy=http://192.168.43.186:7890' >> ~/.bashrc
source ~/.bashrc

代理配置

1
2
3
4
5
6
# 1️⃣ 清空旧代理
unset http_proxy https_proxy all_proxy HTTP_PROXY HTTPS_PROXY ALL_PROXY

# 2️⃣ 强制使用 HTTP 代理(即使 Clash 实际是 mixed-port)
export http_proxy=http://172.26.176.1:7897
export https_proxy=http://172.26.176.1:7897

最终成功步骤

第 1 步:关掉 WSL 的自动 DNS

1
2
3
4
sudo tee /etc/wsl.conf << 'EOF'
[network]
generateResolvConf = false
EOF

第 2 步:手动写 DNS(稳定方案)

1
2
3
4
5
sudo rm -f /etc/resolv.conf
sudo tee /etc/resolv.conf << 'EOF'
nameserver 1.1.1.1
nameserver 8.8.8.8
EOF

第三步:上锁

1
sudo chattr -i /etc/resolv.conf

第四步:验证

1
2
3
ping google.com
curl https://google.com
curl https://claude.ai

如果 curl 能返回 HTML / 重定向信息,说明 DNS 已彻底修好

验证dns是否真的稳了

for i in {1..5}; do nslookup registry.npmjs.org; done

sudo apt update
sudo apt install -y dnsutils

验证能否走代理:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
eightjiu89@LAPTOP-JG60Q7QB:~$ # 安装
npm install node-fetch@2

测试

node -e "
const fetch = require('node-fetch');
const { HttpsProxyAgent } = require('https-proxy-agent');
const agent = new HttpsProxyAgent('http://172.26.176.1:7897');
fetch('https://ipinfo.io/country', { agent })
.then(r => r.text())
.then(console.log)
.catch(console.error);
"

added 4 packages, and audited 9 packages in 21s

found 0 vulnerabilities
SG

验证 Node 是否真的走代理

1
node -e "fetch('https://ipinfo.io/country').then(r=>r.text()).then(console.log)"

export HTTP_PROXY=”http://172.26.176.1:7897

export HTTPS_PROXY=”http://172.26.176.1:7897

API

直接在WSL中配置:

1
2
3
4
5
6
7
export ANTHROPIC_API_KEY="sk-ant-xxx"

npm install @anthropic-ai/sdk

node index.js

npm官方源:npm config set registry https://registry.npmjs.org/

然后启动

1
claude