WSL教程-WSL安装与配置Ubuntu

Source

一、开启 Windows 功能

1.1 图形界面勾选

1.2 命令行开启

dism.exe /online /enable-feature /featurename:Microsoft-Hyper-V /all /norestart
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

Restart-Computer

1.3 关闭 / 卸载

dism.exe /online /disable-feature /featurename:Microsoft-Hyper-V /norestart
dism.exe /online /disable-feature /featurename:VirtualMachinePlatform /norestart
dism.exe /online /disable-feature /featurename:Microsoft-Windows-Subsystem-Linux /norestart

Restart-Computer

1.4 家庭版安装 Hyper-V

Windows 家庭版默认不含 Hyper-V,需手动添加:

pushd "%~dp0"
dir /b %SystemRoot%\servicing\Packages\*Hyper-V*.mum >hyper-v.txt
for /f %%i in ('findstr /i . hyper-v.txt 2^>nul') do dism /online /norestart /add-package:"%SystemRoot%\servicing\Packages\%%i"
del hyper-v.txt
Dism /online /enable-feature /featurename:Microsoft-Hyper-V -All /LimitAccess /ALL
pause

二、安装 Ubuntu

wsl --update
wsl --list --online
wsl --install -d <发行版名称>

三、设置 root 为默认用户

# 设置 root 密码
sudo passwd root

# 编辑 wsl.conf
sudo vim /etc/wsl.conf

wsl.conf 中添加:

[user]
default=root

退出当前用户使配置生效:

wsl --shutdown
wsl

四、更换 APT 源(清华镜像)

# 备份原有源
sudo mv /etc/apt/sources.list.d/ubuntu.sources /etc/apt/sources.list.d/ubuntu.sources.bak

# 编辑新源文件
vim /etc/apt/sources.list.d/ubuntu.sources

写入以下内容:

Types: deb
URIs: https://mirrors.tuna.tsinghua.edu.cn/ubuntu
Suites: noble noble-updates noble-backports
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

Types: deb-src
URIs: https://mirrors.tuna.tsinghua.edu.cn/ubuntu
Suites: noble noble-updates noble-backports
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

Types: deb
URIs: https://mirrors.tuna.tsinghua.edu.cn/ubuntu
Suites: noble-security
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

Types: deb-src
URIs: https://mirrors.tuna.tsinghua.edu.cn/ubuntu
Suites: noble-security
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

五、更新软件源

apt-get update -y
apt-get upgrade -y        # 安全升级:只更新已安装软件,不删除任何包
apt-get dist-upgrade -y   # 智能升级:自动处理冲突依赖,可跨版本升级