559 lines
9.9 KiB
Markdown
559 lines
9.9 KiB
Markdown
---
|
||
icon: mdi:microsoft-windows
|
||
date: 2025-05-08
|
||
category:
|
||
- win10
|
||
tag:
|
||
- wsl
|
||
- http
|
||
title: WSL2
|
||
---
|
||
|
||
|
||
# WSL2完全配置指南:从安装到实用工具
|
||
Windows Subsystem for Linux (WSL2) 为Windows用户提供了无需双系统或虚拟机就能运行Linux环境的能力。本文将全面介绍WSL2的安装、配置和优化过程,包括网络设置、常用工具安装和问题排查等内容。
|
||
<!-- more -->
|
||
## 安装WSL2
|
||
|
||
1. **启用WSL功能**: 打开控制面板,启用"Windows Subsystem for Linux"和"虚拟机平台"功能,然后重启电脑。
|
||
|
||
2. **检查WSL版本**:
|
||
|
||
```powershell
|
||
wsl --status
|
||
wsl --update
|
||
```
|
||
|
||
3. **设置默认为WSL2**:
|
||
|
||
```powershell
|
||
wsl --set-default-version 2
|
||
```
|
||
|
||
4. **查看可用的Linux发行版**:
|
||
|
||
```powershell
|
||
wsl --list --online
|
||
```
|
||
|
||
5. **安装Linux发行版**:
|
||
|
||
```powershell
|
||
# 安装默认版本(Ubuntu)
|
||
wsl --install
|
||
|
||
# 或安装指定版本
|
||
wsl --install -d Ubuntu-24.04
|
||
```
|
||
|
||
6. **设置用户名和密码**,安装完成后系统会提示设置。
|
||
|
||
7. **更新系统**:
|
||
|
||
```bash
|
||
sudo apt update && sudo apt upgrade
|
||
```
|
||
|
||
8. **验证安装**:
|
||
|
||
```powershell
|
||
wsl -l -v
|
||
```
|
||
|
||
## 网络配置
|
||
|
||
### 配置DNS
|
||
|
||
WSL2使用的DNS服务器在`/etc/resolv.conf`文件中设置,为防止每次启动重置,需要进行以下配置:
|
||
|
||
1. 创建`/etc/wsl.conf`文件:
|
||
|
||
```bash
|
||
[network]
|
||
generateResolvConf = false
|
||
```
|
||
|
||
2. 删除原链接文件:
|
||
|
||
```bash
|
||
rm /etc/resolv.conf
|
||
```
|
||
|
||
3. 创建新配置:
|
||
|
||
```bash
|
||
vi /etc/resolv.conf
|
||
# 添加内容
|
||
nameserver 114.114.114.114
|
||
```
|
||
|
||
4. 重启WSL:
|
||
|
||
```bash
|
||
exit
|
||
wsl --shutdown
|
||
wsl
|
||
```
|
||
|
||
5. 测试网络:
|
||
|
||
```bash
|
||
ping www.baidu.com
|
||
```
|
||
|
||
### 配置桥接网络
|
||
|
||
1. **开启Hyper-V**后执行以下命令:
|
||
|
||
```powershell
|
||
Get-NetAdapter
|
||
New-VMSwitch -SwitchName "VETH" -NetAdapterName "以太网" -AllowManagementOS $True
|
||
```
|
||
|
||
2. 创建`.wslconfig`配置文件:
|
||
|
||
```powershell
|
||
cd ~
|
||
New-Item .wslconfig
|
||
notepad .\.wslconfig
|
||
```
|
||
|
||
3. 添加以下内容:
|
||
|
||
```
|
||
[wsl2]
|
||
networkingMode=bridged
|
||
vmSwitch=VETH
|
||
ipv6=true
|
||
```
|
||
|
||
### 配置网络代理
|
||
|
||
1. 关闭自动更新DNS:
|
||
|
||
```bash
|
||
#/etc/wsl.conf
|
||
[network]
|
||
generateResolvConf = false
|
||
```
|
||
|
||
2. 添加以下脚本至`.bashrc`或`.zshrc`:
|
||
|
||
```bash
|
||
vi ~/.bashrc
|
||
# 添加代理配置
|
||
export hostip=10.6.212.22 # 替换为你的代理IP
|
||
export hostport=7890 # 替换为你的代理端口
|
||
alias proxy='
|
||
export HTTPS_PROXY="http://${hostip}:${hostport}";
|
||
export HTTP_PROXY="http://${hostip}:${hostport}";
|
||
export ALL_PROXY="http://${hostip}:${hostport}";
|
||
echo -e "Acquire::http::Proxy \"http://${hostip}:${hostport}\";" | sudo tee -a /etc/apt/apt.conf.d/proxy.conf > /dev/null;
|
||
echo -e "Acquire::https::Proxy \"http://${hostip}:${hostport}\";" | sudo tee -a /etc/apt/apt.conf.d/proxy.conf > /dev/null;
|
||
'
|
||
alias unproxy='
|
||
unset HTTPS_PROXY;
|
||
unset HTTP_PROXY;
|
||
unset ALL_PROXY;
|
||
sudo sed -i -e '/Acquire::http::Proxy/d' /etc/apt/apt.conf.d/proxy.conf;
|
||
sudo sed -i -e '/Acquire::https::Proxy/d' /etc/apt/apt.conf.d/proxy.conf;
|
||
'
|
||
```
|
||
|
||
3. 执行命令启用/禁用代理:
|
||
|
||
```bash
|
||
# 启用代理
|
||
proxy
|
||
|
||
# 禁用代理
|
||
unproxy
|
||
```
|
||
|
||
4. 固定DNS配置:
|
||
|
||
```bash
|
||
sudo rm /etc/resolv.conf
|
||
sudo bash -c 'echo "nameserver 8.8.8.8" > /etc/resolv.conf'
|
||
sudo bash -c 'echo "[network]" > /etc/wsl.conf'
|
||
sudo bash -c 'echo "generateResolvConf = false" >> /etc/wsl.conf'
|
||
sudo chattr +i /etc/resolv.conf
|
||
```
|
||
|
||
5. 测试代理:
|
||
|
||
```bash
|
||
curl -vv google.com
|
||
```
|
||
|
||
## 基础功能配置
|
||
|
||
### 开启SSH服务
|
||
|
||
1. 安装SSH服务:
|
||
|
||
```bash
|
||
sudo apt update && sudo apt upgrade
|
||
sudo apt-get install openssh-server
|
||
```
|
||
|
||
2. 修改SSH配置:
|
||
|
||
```bash
|
||
sudo vi /etc/ssh/sshd_config
|
||
```
|
||
|
||
找到`PasswordAuthentication`行,确保设置为`yes`
|
||
|
||
3. 启动并设置开机自启:
|
||
|
||
```bash
|
||
sudo systemctl status ssh
|
||
sudo systemctl start ssh
|
||
sudo systemctl enable ssh
|
||
```
|
||
|
||
### 安装桌面环境
|
||
|
||
```bash
|
||
sudo apt update
|
||
sudo apt install ubuntu-desktop
|
||
# 安装远程桌面服务
|
||
sudo apt-get install xrdp
|
||
sudo systemctl start xrdp
|
||
sudo systemctl enable xrdp
|
||
```
|
||
|
||
### 映射Windows目录至WSL
|
||
|
||
```bash
|
||
# 创建挂载点
|
||
sudo mkdir /mnt/z
|
||
# 挂载Windows目录
|
||
sudo mount -t drvfs C:/User/xxx/Desktop/挂载文件 /mnt/z
|
||
```
|
||
|
||
## 系统管理
|
||
|
||
### 查看端口
|
||
|
||
查询端口占用有两种常用方法:
|
||
|
||
1. 使用`netstat`:
|
||
|
||
```bash
|
||
sudo apt-get install net-tools
|
||
sudo netstat -tunlp | grep 端口号
|
||
```
|
||
|
||
2. 使用`lsof`:
|
||
|
||
```bash
|
||
sudo apt-get install lsof
|
||
sudo lsof -i:端口号
|
||
```
|
||
|
||
### 修改主机名
|
||
|
||
1. 使用`hostnamectl`命令:
|
||
|
||
```bash
|
||
sudo hostnamectl set-hostname 新主机名
|
||
```
|
||
|
||
2. 修改配置文件:
|
||
|
||
```bash
|
||
sudo vi /etc/hostname
|
||
sudo vi /etc/hosts
|
||
```
|
||
|
||
在hosts文件中将`127.0.1.1`对应的旧主机名替换为新主机名
|
||
|
||
3. 重启系统(可选):
|
||
|
||
```bash
|
||
sudo reboot
|
||
```
|
||
|
||
## 开发环境配置
|
||
|
||
### IDEA中文乱码修复
|
||
|
||
1. 安装语言包:
|
||
|
||
```bash
|
||
sudo apt install language-pack-zh-hans
|
||
```
|
||
|
||
2. 配置语言环境:
|
||
|
||
```bash
|
||
sudo dpkg-reconfigure locales
|
||
# 选择en_US.UTF-8和zh_CN.UTF-8,并将zh_CN.UTF-8设为默认
|
||
```
|
||
|
||
3. 安装字体工具:
|
||
|
||
```bash
|
||
sudo apt install fontconfig
|
||
```
|
||
|
||
4. 配置Windows字体:
|
||
|
||
```bash
|
||
sudo vi /etc/fonts/local.conf
|
||
```
|
||
|
||
添加内容:
|
||
|
||
```xml
|
||
<?xml version="1.0"?>
|
||
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
|
||
<fontconfig>
|
||
<dir>/mnt/c/Windows/Fonts</dir>
|
||
</fontconfig>
|
||
```
|
||
|
||
5. 刷新字体缓存:
|
||
|
||
```bash
|
||
fc-cache -f -v
|
||
```
|
||
|
||
6. 重启WSL:
|
||
|
||
```bash
|
||
wsl --shutdown
|
||
```
|
||
|
||
### IDEA配置输入法
|
||
|
||
1. 安装fcitx输入法:
|
||
|
||
```bash
|
||
sudo apt install fcitx dbus-x11 im-config fcitx-sunpinyin
|
||
```
|
||
|
||
2. 编辑`/etc/locale.gen`:
|
||
|
||
```bash
|
||
vi /etc/locale.gen
|
||
# 取消注释行:zh_CN.UTF-8
|
||
```
|
||
|
||
3. 配置环境变量:
|
||
|
||
```bash
|
||
vi ~/.profile
|
||
# 添加内容
|
||
export GTK_IM_MODULE=fcitx
|
||
export QT_IM_MODULE=fcitx
|
||
export XMODIFIERS=@im=fcitx
|
||
export DefaultIMModule=fcitx
|
||
fcitx-autostart &>/dev/null
|
||
```
|
||
|
||
4. 更新配置:
|
||
|
||
```bash
|
||
source ~/.profile
|
||
```
|
||
|
||
5. 配置快捷键:
|
||
|
||
```bash
|
||
fcitx-config-gtk3
|
||
```
|
||
|
||
6. IDEA支持:编辑`idea.sh`启动脚本,添加:
|
||
|
||
```properties
|
||
export XMODIFIERS=@im=fcitx
|
||
export QT_IM_MODULE=fcitx
|
||
```
|
||
|
||
### 安装Docker
|
||
|
||
1. 更新系统包:
|
||
|
||
```bash
|
||
sudo apt update
|
||
```
|
||
|
||
2. 安装依赖:
|
||
|
||
```bash
|
||
sudo apt install ca-certificates curl gnupg lsb-release
|
||
```
|
||
|
||
3. 添加Docker官方GPG密钥:
|
||
|
||
```bash
|
||
sudo mkdir -p /etc/apt/keyrings
|
||
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
|
||
```
|
||
|
||
4. 添加Docker APT源:
|
||
|
||
```bash
|
||
echo \
|
||
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
|
||
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
|
||
```
|
||
|
||
5. 更新包列表:
|
||
|
||
```bash
|
||
sudo apt update
|
||
```
|
||
|
||
6. 安装Docker引擎:
|
||
|
||
```bash
|
||
sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
|
||
```
|
||
|
||
7. 启动并验证:
|
||
|
||
```bash
|
||
sudo systemctl start docker
|
||
sudo systemctl enable docker
|
||
sudo docker --version
|
||
```
|
||
|
||
### 安装1Panel
|
||
|
||
一键安装:
|
||
|
||
```bash
|
||
curl -sSL https://resource.fit2cloud.com/1panel/package/quick_start.sh -o quick_start.sh && sudo bash quick_start.sh
|
||
```
|
||
|
||
查看管理员密码:
|
||
|
||
```bash
|
||
sudo 1pctl user-info
|
||
```
|
||
|
||
### 安装SVN
|
||
|
||
```bash
|
||
sudo apt update
|
||
sudo apt install subversion
|
||
svn --version # 验证安装
|
||
```
|
||
|
||
## 问题记录
|
||
|
||
### SSH连接异常
|
||
|
||
症状:SSH服务启动失败,出现以下错误:
|
||
|
||
```
|
||
error: Bind to port 22 on 0.0.0.0 failed: Address already in use.
|
||
fatal: Missing privilege separation directory: /run/sshd
|
||
```
|
||
|
||
解决方法:
|
||
|
||
1. 检查端口占用:
|
||
|
||
```bash
|
||
sudo lsof -i:22
|
||
```
|
||
|
||
2. 终止占用进程:
|
||
|
||
```bash
|
||
sudo kill <PID>
|
||
```
|
||
|
||
3. 创建缺失目录:
|
||
|
||
```bash
|
||
sudo mkdir -p /run/sshd
|
||
sudo chmod 0755 /run/sshd
|
||
```
|
||
|
||
4. 重启SSH服务:
|
||
|
||
```bash
|
||
sudo systemctl restart ssh
|
||
```
|
||
|
||
## 常用指令
|
||
|
||
```powershell
|
||
# 列出可用的Linux发行版
|
||
wsl --list --online
|
||
|
||
# 列出已安装的发行版
|
||
wsl --list --verbose # 或 wsl -l -v
|
||
|
||
# 设置WSL版本
|
||
wsl --set-version <发行版名称> <版本号>
|
||
|
||
# 设置默认WSL版本
|
||
wsl --set-default-version <版本号>
|
||
|
||
# 设置默认Linux发行版
|
||
wsl --set-default <发行版名称>
|
||
|
||
# 运行特定发行版
|
||
wsl --distribution <发行版名称> --user <用户名>
|
||
|
||
# 更新WSL
|
||
wsl --update
|
||
|
||
# 检查WSL状态
|
||
wsl --status
|
||
|
||
# 检查WSL版本
|
||
wsl --version
|
||
|
||
# 以特定用户身份运行
|
||
wsl --user <用户名>
|
||
|
||
# 卸载Linux发行版
|
||
wsl --unregister <发行版名称>
|
||
|
||
# 标识IP地址
|
||
wsl hostname -I # 返回WSL2 IP地址
|
||
ip route show | grep -i default | awk '{ print $3}' # 返回Windows主机IP
|
||
|
||
# 更改默认用户
|
||
<发行版名称> config --default-user <用户名>
|
||
```
|
||
|
||
## 实用软件安装
|
||
|
||
### 安装适用于 Linux 的 Google Chrome
|
||
|
||
```bash
|
||
cd /tmp
|
||
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
|
||
sudo apt install --fix-missing ./google-chrome-stable_current_amd64.deb
|
||
```
|
||
|
||
启动命令:`google-chrome`
|
||
|
||
### 安装 VLC
|
||
|
||
```bash
|
||
sudo apt install vlc -y
|
||
```
|
||
|
||
启动命令:`vlc`
|
||
|
||
### 安装 X11 应用
|
||
|
||
```bash
|
||
sudo apt install x11-apps -y
|
||
```
|
||
|
||
启动命令示例:`xcalc`、`xclock`、`xeyes`
|
||
|
||
------
|
||
|
||
通过本文的配置指南,你可以构建一个功能完善的WSL2环境,满足日常开发、学习和娱乐需求。WSL2的灵活性使得Windows用户无需切换操作系统就能享受Linux的强大功能,是开发人员的理想工具。 |