WSL教程-WSL安装与配置Ubuntu

一、开启 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   # 智能升级:自动处理冲突依赖,可跨版本升级
相关推荐
躺不平的理查德22 分钟前
Nginx与Ajax 备忘录
linux·运维·服务器
惜离殇30 分钟前
从零开始的敲代码生活--Linux应用软件(文件操作基础2)
linux·文件io·目录io
Sunqk566539 分钟前
将开发板串口连接到Ubuntu后未找到对应的设备文件?
linux·运维·ubuntu
Sophnet云平台41 分钟前
MCP与A2A双协议解析:2026年Agent互操作的技术选型
linux·服务器·网络·上下文·mcp·大模型测评·sophnet
晚风叙码44 分钟前
Linux(二)权限问题
linux·服务器
BI专家之路3 小时前
安装linux/hadoop/jdk/hive
linux·hive·hadoop
GeW15 小时前
超详细!RHCE10版通用备考策略,内含评分细则解读
linux
用户0413284651316 小时前
Linux 服务开机自启动怎么配置?systemctl enable 原理详解
linux
草莓熊Lotso1 天前
【Redis 初阶】Set 类型深度解析:去重集合的运算能力与实战场景
linux·网络·数据库·windows·redis·tcp/ip·缓存