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   # 智能升级:自动处理冲突依赖,可跨版本升级
相关推荐
未济7 小时前
linux 配置环境变量
linux
傲世仙尊7 小时前
目录即文件-Ext文件系统收尾篇
linux·c语言
_艾伦 耶格尔.9 小时前
进程间通信
linux
Liuqy-0510 小时前
Linux IO编程——静态库、动态库
linux
彧azz10 小时前
Linux 环境下 Redis 学习总结:数据类型、持久化、锁、事务、主从与缓存问题
linux·redis·笔记·学习·面试
-梅11 小时前
linux(8) 软硬链接
linux·运维·服务器
AIgorithmGEEK11 小时前
[Linux]线程三部曲(上):一个执行流的诞生——从操作系统一路拆到 pthread_create
linux·线程·pid
琥珀色糖12 小时前
SimlpeHttp
linux·服务器
qeen8712 小时前
【Linux】操作系统之进程介绍(二)
linux·笔记·学习·进程
Zenova EdgeOS12 小时前
Linux ss 工业边缘网络分析实战
linux·python·边缘计算·工业网关