准备
Enable the Windows Subsystem for Linux
管理员运行 powershell:
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
Enable Virtual Machine feature
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
Download the Linux kernel update package
WSL2 Linux kernel update package for x64 machines
Set WSL default version
wsl --set-default-version 2
WSL2 安装到 D 盘
Installing WSL Distro to a different/custom location https://docs.microsoft.com/zh-cn/windows/wsl/install-manual#step-4—-download-the-linux-kernel-update-package
# 管理员运行 powershelldism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart# Enable Virtual Machine featuredism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart# 安装 wsl_update_x64.msi# 1. 下载 Ubuntu 20.04Invoke-WebRequest -Uri https://aka.ms/wslubuntu2004 -OutFile Ubuntu.appx -UseBasicParsing# 2. 解压move .\Ubuntu.appx .\Ubuntu.zipExpand-Archive .\Ubuntu.zip# 3. 安装(这种方式默认用户是 root)wsl.exe --import <DistributionName> <InstallLocation> <FileName># 例如wsl.exe --import Ubuntu2004 D:\Ubuntu2004 .\install.tar.gz# 验证wsl -l -v# 4. 进入 wsl2,创建用户bash$ sudo adduser <username># 查看用户 uid$ id <username># 5. 退出 wsl2 后,修改默认用户Get-ItemProperty Registry::HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Lxss\*\ DistributionName | Where-Object -Property DistributionName -eq <UbuntuCustom> | Set-ItemProperty -Name DefaultUid -Value <uid>
Webstorm 配置
在 C:\Users\
<descriptor><id>UBUNTU2004</id><microsoft-id>Ubuntu2004</microsoft-id><executable-path>D:\WSL\Ubuntu\ubuntu2004.exe</executable-path><presentable-name>Ubuntu 20.04</presentable-name></descriptor>
- Terminal sell path: “wsl.exe”
 - git 配置:\wsl$\Ubuntu2004\usr\bin\git
 node.js and NPM 配置:Add WSL -> Ubuntu2004 -> node (需要先在 wsl2 中安装 nvm)
WSL2 配置
windows 中需要配置防火墙,wsl2 才能 ping 主机。
Windows Defender 防火墙 -> 入站规则->新建规则:规则类型:自定义
- 程序:所有程序
 - 协议和端口:默认即可
 - 作用域:
- 本地ip处选择“任何IP地址”
 - 远程ip处选择“下列IP地址”,并将wsl2的IP添加进去。(请根据自己 wsl2 的 ip 进行计算,例如 172.22.176.0/24)
 
 - 操作:允许连接
 - 配置文件:三个全选
 - 名称描述:请自定义
 
注意:这一步完成后,从wsl2 ping主机的ip应该可以ping通了。
wsl2 中安装软件前,
sudo service dbus start
