安装WSL2 开启Linux子系统
以管理员身份运行 PowerShell,执行以下命令(PowerShell入口右键开始菜单,然后点击Windows PowerShell管理员)
1 2 3 4
| wsl --install # wsl --install 执行的比较久,耐心等待 dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
|
执行完成后重启,一定要重启,若选择关机,然后再开机则会命令不全
1
| wsl --set-default-version 2
|
安装完成后可以开启ubuntu
引用来源
安装centos
wsl默认不带有centos源的,需要从第三方下载https://github.com/mishamosher/CentOS-WSL
下载完成后解压到自己想要的地方,以后centos数据存储的地方就会是相同的目录, 因此谨慎选择
双击CentOS7.exe运行,会生成一个ext4.vhdx
完成后即安装成功,使用wsl --list可以查看是否已经安装成功了
1 2 3
| PS C:\Windows\system32> wsl --list 适用于 Linux 的 Windows 子系统分发版: CentOS7 (默认)
|
引用来源
切换hyper-v
WSL依赖于hyper-v必须开启,而VMware不依赖这个,必须关闭
开启:bcdedit /set hypervisorlaunchtype auto
关闭:bcdedit /set hypervisorlaunchtype off
执行之后重启电脑
访问wsl内部
有时候使用localhost访问不到wsl内的服务器,需要执行下面命令打通映射关系
1 2 3 4 5 6 7 8 9 10 11 12
| # 获取 WSL2 IP $wsl_ip = (wsl hostname -I).trim()
# 添加 Redis 端口转发 netsh interface portproxy add v4tov4 listenport=3306 listenaddress=0.0.0.0 connectport=3306 connectaddress=$wsl_ip
New-NetFirewallRule -DisplayName "Allow MySQL WSL" -Direction Inbound -Protocol TCP -LocalPort 3306 -Action Allow
# 需要移除的话则移除 # Remove-NetFirewallRule -DisplayName "Allow MySQL WSL" -ErrorAction SilentlyContinue
|
1 2 3 4 5 6 7 8 9
| $ports = @(3306, 6379, 3000, 8080)
foreach ($port in $ports) { $name = "WSL - Port $port" Remove-NetFirewallRule -DisplayName $name -ErrorAction SilentlyContinue New-NetFirewallRule -DisplayName $name -Direction Inbound -Protocol TCP -LocalPort $port -Action Allow Write-Host "✅ 已放行 $port" }
|