Windows11下安装WSL
Windows11下安装WSL
一、WSL是什么?
开发人员可以在 Windows 计算机上同时访问 Windows 和 Linux 的强大功能。 通过适用于 Linux 的 Windows 子系统 (WSL),开发人员可以安装 Linux 发行版(例如 Ubuntu、OpenSUSE、Kali、Debian、Arch Linux 等),并直接在 Windows 上使用 Linux 应用程序、实用程序和 Bash 命令行工具,不用进行任何修改,也无需承担传统虚拟机或双启动设置的费用。
二、安装步骤
确保电脑虚拟化开启
控制面板->程序->启用或关闭 windows 功能,开启 Windows 虚拟化和 Linux 子系统(WSL2)以及Hyper-V。由于在Windows11中并没有Hyper-V,需要进行手动配置
配置Hyper-V
家庭版windows11没有Hyper-V,需要配置Hyper-V。打开vs code创建Hyper-7.cmd,复制以下内容并保存后执行。
1
2
3
4
5pushd "%~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
系统安装
win11 使用 WSL2 安装 linux 子系统 ubuntu 出现错误:无法解析服务器的名称或地址。原本显示报错:ConnectionError: Couldn’t reach https://raw.githubusercontent.com/huggingfac
无法访问。解决方法:
修改 本地 host 文件。
记事本打开 C:\Windows\System32\drivers\etc\hosts 文件,添加如下解析地址(4个中有一个好用就添加它)在https://www.ipaddress.com这个网站中的查询框中输入:raw.githubusercontent.com
在里面找到相应的的ipv4地址,这四个地址随便选一个即可(好用的):185.199.108.133 raw.githubusercontent.com
185.199.109.133 raw.githubusercontent.com
185.199.110.133 raw.githubusercontent.com
185.199.111.133 raw.githubusercontent.com
在CMD中刷新 DNS 解析缓存
ipconfig /flushdns再次运行查看或安装命令
查看可安装的 WSL1
wsl -l -o
列出已安装版本
1
wsl -l -v
wsl –install -d(安装):
1
wsl --install -d Ubuntu-22.04
wsl -d (运行):
1
wsl -d Ubuntu-22.04
修改到D盘
由于默认是到C盘,现修改到C盘。
关闭子系统
关闭界面/输入如下命令关闭子系统。
1
wsl --shutdown Ubuntu-22.04
导出子系统
1
wsl --export Ubuntu-22.04 D:/WSL/Ubuntu22.04.tar
注销原子系统
1
wsl --unregister Ubuntu-22.04
从D盘导入
1
wsl --import Ubuntu-22.04 D:/WSL/Ubuntu-22.04.tar
再次运行导入好的系统