Win10/Win11任意位置安装WSL

Win10/Win11任意位置安装WSL
青夢Win10/Win11任意位置安装WSL
WSL的优势:WSL的Ubuntu允许在Windows中运行Linux工具链(如Git、Python、Node.js等)、Ubuntu子系统通过WSL直接调用Linux内核,避免了传统虚拟机的性能损耗;相较于完整虚拟机,启动速度更快;与Windows文件系统互通(
/mnt
目录挂载),可在Ubuntu中直接操作windows文件
安装在非默认C盘的优势:WSL默认是存储在
%LOCALAPPDATA%\Package
中,容易导致系统盘容量告急;通过wsl --export
和wsl --import
可将Ubuntu分发版迁移至任意路径,并自定义存储位置
安装Ubuntu
启用虚拟化与WSL功能
WSL2需要满足Windows10版本2004及以上(内部版本19241及以上)、Windows11
方法一
Win + R
输入optionalfeatures.exe
,勾选适用Linux的Windows子系统和虚拟机平台,重启系统验证虚拟化状态:任务管理器-性能-右下角“虚拟化”显示“已启用”
设置WSL默认版本
Win + X
以管理员身份运行Powershell,执行1
wsl --set-default-version 2
方法二
管理员身份运行powershell
1
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
启用虚拟机
1
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
重启计算机
下载WSL2内核更新包并进行安装
下载最新版本WSL
设置WSL2为默认版本
1
wsl --set-default-version 2
下载Windows终端(可选);访问设置默认终端应用程序修改默认终端程序,如果修改wsl字体需要使用到Windows终端
自定义位置安装Ubuntu
访问下载发行版,下载
.AppxBundle
安装包,这里我选择22.04
下载后将
.AppxBundle
后缀改为.zip
,然后解压,找到含x64.appx
文件,再次改后缀为.zip
,解压到指定目标目录,如D:\Ubuntu
初始化安装
双击运行解压到执行目录中的
ubuntu.exe
,根据提示完成初始化账户设置(建议使用普通用户,非root)列出所有WSL发行版及其状态
1
wsl -l -v
查看设定盘
1
2
3
4
5
6
7
8
9
10Get-ChildItem "HKCU:\Software\Microsoft\Windows\CurrentVersion\Lxss" | ForEach-Object {
# 从注册表项中获取属性值
$value = (Get-ItemProperty $_.PSPath)
# 判断发行版名称是否包含 "Ubuntu"
if ($value.DistributionName -match "Ubuntu") {
# 输出匹配项的安装路径
$value.BasePath
}输出手动指定的安装目录即可
系统配置与优化
更换Ubuntu国内镜像源
备份原始镜像源
1
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
编辑镜像源文件(使用nano编辑器或vim);删除原有的内容,黏贴以下任一镜像源配置
阿里云镜像源(推荐企业用户)
1
2
3
4
5deb http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse清华大学镜像源(教育网/科研用户首选)
1
2
3
4deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse中科大镜像源(低延迟网络优化)
1
2
3
4deb https://mirrors.ustc.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
保存并退出
- Nano:
Ctrl+O
->Enter
->Ctrl+X
- Vim:
ESC
->输入:wq
->Enter
- Nano:
更新软件列表
1
sudo apt update && sudo apt upgrade -y
配置开发环境
设置python别名
1
2
3
4
5
6echo 'alias python="/usr/bin/python3"' >> ~/.bashrc # 或 ~/.zshrc
source ~/.bashrc
sudo apt install python3-pip
sudo apt install python3.10-venv
如果对python3创建了软链接,使用虚拟环境注意手动指定虚拟环境的python解释器,如:.vevn\bin\python -m http.server安装基础工具链
1
sudo apt install build-essential git curl wget vim -y
安装增加Shell
1
2sudo apt install zsh # 安装zsh
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" # 安装并配置Oh My Zsh1
2
3echo $SHELL # 确认默认shell是zsh
chsh -s $(which zsh) # 如果不是,执行手动设置1
2
3
4编辑`~/.zshrc`,安装和启用插件
nano ~/.zshrc
plugins=(git z sudo zsh-autosuggestions zsh-syntax-highlighting)1
2
3
4
5
6
7
8
9
10
11
12
13
14
15修改主题和字体
nano ~/.zshrc
ZSH_THEME="agnoster"
source ~/.zshrc # 保存并退出后重新加载配置
windows访问[Nerd Fonts官网](https://www.nerdfonts.com/font-downloads),推荐下载`MesloLGS NF`
如果是`.zip`需解压,`.ttf`直接右键为所有用户安装
1、打开 Windows Terminal
2、点击右上角小箭头 ▼ → 选择“设置”
3、左侧选择你使用的 Ubuntu 终端(比如 “Ubuntu”)
4、找到“外观” → “字体面孔”(Font face)
5、输入你刚安装的字体名,比如:`MesloLGS NF`
6、保存设置,重启windows 终端
高级功能扩展
GUI支持(WSLg)
确保WSL版本为2,执行:
1
wsl --set-version Ubuntu 2
安装GUI应用测试
1
sudo apt install gnome-text-editor
开始菜单将会出现Linux应用入口
SSH远程访问
安装OpenSSH
1
2sudo apt install openssh-server
sudo service ssh restart查看IP:
ip addr show eth0
,使用工具即可连接
文件互操作
- Windows访问Linux:资源管理器地址栏输入
\\wsl$
- Linux访问Windows:文件挂载在
/mnt
目录下 - 在WSL中可以直接调用windows的程序,如
notepad.exe 文件名
即可使用windows编辑文件,使用explorer.exe .
即可使用windows的资源管理打开当前目录
安装原生Docker
开启systemd
1
2
3
4
5
6
7vim /etc/wsl.conf
添加以下内容,需要WSL0.67.6及以上版本
[boot]
systemd=true
wsl --shutdown # 重启生效更新系统包及安装必要的包
1
apt update && apt -y install --no-install-recommends apt-transport-https ca-certificates curl gnupg2
添加Docker官方的APT仓库
1
echo "deb [arch=$(dpkg --print-architecture)] https://download.docker.com/linux/ubuntu jammy stable" | tee /etc/apt/sources.list.d/docker.list
导入Docker的官方GPG密钥
1
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | tee /etc/apt/trusted.gpg.d/docker.asc
再次更新系统包列表,并安装Docker CE(社区版)、CLI工具、containerd.io、以及Docker的其他插件
1
apt update && apt -y install --no-install-recommends docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
验证是否安装成功
1
2docker --version
docker compose version
可选扩展
与Windows系统公用同一IP
进入C盘用户目录,新建
.wslconfig
,使用记事本添加并保存以下内容,然后使用powershell执行wsl --shutdown
,等待一段时间后重新启动即可,使用ifconfig
验证1
2[wsl2]
networkingMode=mirrored性能优化
限制内存使用:进入C盘用户目录创建
.wslconfig
,添加以下内容1
2
3[wsl2]
memory=4GB
processors=2使用
lsd
替代ls
:通过rust安装cargo install lsd
WSL开机启动,后台运行(需要使用systemd,且建议搭配限制内存使用)
Win+R
输入运行shell:startup
打开启动目录在此目录创建文件
wsl-startup.vbs
编辑写入以下内容
1
2
3
4set ws=wscript.CreateObject("wscript.shell")
ws.run "wsl -d Ubuntu", 0
# 命令中的`Ubuntu`是安装的发行版名称,使用wsl -l -v查看
启用代理
修改
~/.bashrc
或~/.zshrc
添加一下内容1
2
3
4
5
6
7自动设置代理 端口记得替换成使用的代理软件的
在代理软件设置代理端口,为混合代理端口、socks代理端口、http(s)代理端口并开启
代理软件开启局域网连接
host_ip=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2}')
export http_proxy="http://$host_ip:7897"
export https_proxy="http://$host_ip:7897"
export all_proxy="socks5://$host_ip:7897"重新打开WSL输入
echo $http_proxy
验证是否设置成功