Files
nexus/raw/Home Office/WSL2 启动与网络配置指南.md
2026-04-27 16:26:34 +08:00

139 lines
3.1 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: WSL2 启动与网络配置指南
source:
author: shenwei
published:
created:
description:
tags:
---
#wsl #ubuntu #windows
# WSL2 启动与网络配置指南
## 一、 WSL2 基础启动操作
### 1. 首次安装与启用
- **快速安装命令**(管理员模式 PowerShell
PowerShell
```
wsl --install
```
_注默认安装 Ubuntu完成后必须重启电脑。_
### 2. 状态检查与版本转换
- **查看已安装分发版及状态**
PowerShell
```
wsl -l -v
```
- **强制将分发版设置为 WSL2**
PowerShell
```
wsl --set-version <分发版名称> 2
```
- **日常进入 Linux 终端**
PowerShell
```
wsl -d <分发版名称> # 启动指定版本
wsl --shutdown # 强制停止所有运行中的 WSL 实例
```
## 二、 网络进阶配置(解决 GitHub/海外连接问题)
由于 WSL2 默认使用 NAT 模式常会出现“localhost 代理无法镜像”或无法访问海外资源的情况。
### 1. 启用“镜像网络模式”(推荐方案)
这是最稳妥的方案,使 WSL2 与 Windows 共享网络堆栈。
- **操作步骤**
1. 打开 Windows 用户目录(`C:\Users\<你的用户名>\`)。
2. 创建或编辑 `.wslconfig` 文件。
3. 写入以下配置:
Ini, TOML
```
[wsl2]
networkingMode=mirrored
dnsTunneling=true
firewall=true
autoProxy=true
```
4. 在 PowerShell 执行 `wsl --shutdown` 后重启 WSL。
### 2. 手动配置终端代理
如果未开启镜像模式,需手动指向 Windows 宿主机 IP
- **获取宿主机 IP**`cat /etc/resolv.conf | grep nameserver`
- **设置临时变量**(假设端口为 7890
Bash
```
export https_proxy="http://<宿主机IP>:7890"
export http_proxy="http://<宿主机IP>:7890"
```
### 3. 使用国内镜像加速(针对 Hermes/uv 安装)
若 GitHub 访问受限,可使用代理地址:
- **手动安装 uv**
Bash
```
curl -LsSf https://mirror.ghproxy.com/https://github.com/astral-sh/uv/releases/latest/download/uv-installer.sh | sh
```
- **使用镜像安装 Hermes Agent**
Bash
```
curl -fsSL https://mirror.ghproxy.com/https://raw.githubusercontent.com/NousResearch/hermes-agent/main/scripts/install.sh | bash
```
## 三、 常见故障排查
- **错误码 Wsl/Service/WSL_E_VM_MODE_INVALID_STATE**
- 通常由于虚拟化未开启或服务死锁。
- **解决**:检查 BIOS 中的 `Virtualization Technology` 是否为 `Enabled`;在 Windows 功能中勾选“虚拟机平台”。
- **文件权限问题**
- 避免在 `/mnt/c/`Windows 挂载目录)下执行复杂的 Linux 脚本安装。
- **建议**:始终先执行 `cd ~` 进入 Linux 原生家目录后再进行环境配置。
---
_记录日期2026-04-17_