标题: Win10中安装WSL1 创建: 2020-10-29 10:02 更新: 2024-01-23 21:53 链接: https://scz.617.cn/windows/202010291002.txt -------------------------------------------------------------------------- 目录: ☆ WSL简介 1) 什么是WSL 2) WSL1 vs WSL2 2.1) 禁用Hyper-V 3) 查看当前WSL版本 ☆ 安装WSL1 1) 启用或关闭Windows功能 2) 创建Linux发行版的安装目录 3) 手工下载Linux发行版 4) 手工安装Linux发行版 5) 进bash 6) 在多个Linux发行版之间切换 ☆ 迁移WSL1 1) LxRunOffline 2) 手工迁移已安装发行版 3) 不同主机共用一枚U盘 ☆ WSL1中访问网络映射盘或VHD ☆ 安装Windows Terminal ☆ 简单升级Linux发行版 ☆ 文件系统的坑 ☆ "Windows Subsystem for Linux"组件的一些讨论 -------------------------------------------------------------------------- ☆ WSL简介 1) 什么是WSL -------------------------------------------------------------------------- What is the Windows Subsystem for Linux? https://learn.microsoft.com/en-us/windows/wsl/about Frequently Asked Questions about Windows Subsystem for Linux https://learn.microsoft.com/en-us/windows/wsl/faq -------------------------------------------------------------------------- 简单点说,装了WSL (Windows Subsystem for Linux),就可以在Windows中执行ELF 2) WSL1 vs WSL2 -------------------------------------------------------------------------- Comparing WSL 1 and WSL 2 https://docs.microsoft.com/en-us/windows/wsl/compare-versions Comparing WSL Versions https://learn.microsoft.com/en-us/windows/wsl/compare-versions (现在是这个URL) -------------------------------------------------------------------------- 这里比较了WSL1、WSL2,MS推荐使用WSL2。 原始需求是用现成的Linux命令行工具,并不打算在WSL中搞正经开发或者用GUI程序, 所以WSL1足够了。在网上搜了搜,WSL2多多少少与VMware有冲突,不可能为了WSL2放 弃VMware。 问个问题,假设启用WSL2,VMware 16或更高版本的这三个选项勾中后Guest能启动吗? Virtual Machine Settings Hardware Processors Virtualization engine Virtualize Intel VT-x/EPT or AMD-V/RVI Virtualize CPU performance counters Virtualize IOMMU (IO memory management unit) 想说WSL2与VMware 16全兼容之前,先试试这个。有几位网友反馈,第三项可以选, 前两项选中后无法启动Guest。 据ZYH说,启用WSL2需要启用Hyper-V,VMware 15.5之后的版本支持WinHV API,可以 正常启动Guest,但是不支持嵌套虚拟化,前两项不能选与此有关。 2.1) 禁用Hyper-V 可用PowerShell或DISM查看Hyper-V的状态、禁用或启用Hyper-V。我不想用WSL2,索 性禁用Hyper-V。 Get-WindowsOptionalFeature -Online | Where-Object {$_.FeatureName -eq "Microsoft-Hyper-V-All"} | Select-Object FeatureName, State Get-WindowsOptionalFeature -Online | Where-Object {$_.FeatureName -like "*Hyper-V*"} | Select-Object FeatureName, State Get-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V-All Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V-All -All Disable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V-All dism.exe /Online /Get-Features | find "Hyper-V" dism.exe /Online /Enable-Feature /FeatureName:Microsoft-Hyper-V-All /All /NoRestart dism.exe /Online /Disable-Feature /FeatureName:Microsoft-Hyper-V-All /NoRestart 3) 查看当前WSL版本 wsl --list -v wsl --list --all -v wsl --list --verbose wsl --list --all --verbose VERSION列指明当前WSL版本。更改新分发的默认安装版本: wsl --set-default-version 1 ☆ 安装WSL1 参看: -------------------------------------------------------------------------- Windows Subsystem for Linux Installation Guide for Windows 10 https://docs.microsoft.com/en-us/windows/wsl/install-win10 How to install Linux on Windows with WSL https://learn.microsoft.com/en-us/windows/wsl/install (现在是这个URL) Manual installation steps for older versions of WSL https://learn.microsoft.com/en-us/windows/wsl/install-manual Troubleshooting Windows Subsystem for Linux https://learn.microsoft.com/en-us/windows/wsl/troubleshooting WSL product repo issues https://github.com/Microsoft/wsl/issues WSL docs repo issues https://github.com/MicrosoftDocs/wsl/issues -------------------------------------------------------------------------- 1) 启用或关闭Windows功能 有多种进入途径: -------------------------------------------------------------------------- control Programs and Features (程序和功能) Turn Windows features on or off (启用或关闭Windows功能) OptionalFeatures.exe (实际执行该程序,可以命令行直接启动) -------------------------------------------------------------------------- Settings Apps Apps & features Programs and Features -------------------------------------------------------------------------- 在此选中: 适用于Linux的Windows子系统 (中文版) Windows Subsystem for Linux (英文版) 这个默认没有选中。为了让它生效,可能需要重启OS,总之根据提示来。 可以命令行启用,在管理员级PowerShell中执行: Get-WindowsOptionalFeature -Online | Where-Object {$_.FeatureName -eq "Microsoft-Windows-Subsystem-Linux"} | Select-Object FeatureName, State Get-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux -All Disable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux 还可以: dism.exe /Online /Get-Features | find "Microsoft-Windows-Subsystem-Linux" dism.exe /Online /Enable-Feature /FeatureName:Microsoft-Windows-Subsystem-Linux /All /NoRestart dism.exe /Online /Disable-Feature /FeatureName:Microsoft-Windows-Subsystem-Linux /NoRestart 2) 创建Linux发行版的安装目录 Microsoft Store https://aka.ms/wslstore 在Chrome中访问上述URL,提示打开本机Microsoft Store,有点意思。 WSL只是个Windows子系统,最终要用的是Microsoft Store中提供的某种Linux发行版。 网上大多数简明教程都是缺省安装流程,此时这些Linux发行版缺省被安装到: %LOCALAPPDATA%\Packages\\LocalState\rootfs\ 我想安装到其他指定目录。 New-Item D:\WSL -ItemType Directory Set-Location D:\WSL 就是简单的创建目录、切换目录,不用PowerShell用cmd、Explorer也可以。 3) 手工下载Linux发行版 Manually download Windows Subsystem for Linux distro packages https://docs.microsoft.com/en-us/windows/wsl/install-manual 这里给出了各Linux发行版的下载链接。可以在PowerShell中下载: Invoke-WebRequest -Uri https://aka.ms/wsl-debian-gnulinux -OutFile Debian.appx -UseBasicParsing 我用Chrome直接下: https://aka.ms/wsl-debian-gnulinux 最终看到的下载链接是: https://wsldownload.azureedge.net/DebianGNULinux_1-1-3-0_x64__76v4gfsz19hv4.Appx 4) 手工安装Linux发行版 Windows Server Installation Guide https://docs.microsoft.com/en-us/windows/wsl/install-on-server https://learn.microsoft.com/en-us/windows/wsl/install-on-server 不要用这种方式安装: Add-AppxPackage .\DebianGNULinux_1-1-3-0_x64__76v4gfsz19hv4.Appx 这种跟GUI安装一样,无法安装到指定目录。 Rename-Item .\DebianGNULinux_1-1-3-0_x64__76v4gfsz19hv4.Appx .\Debian.zip Expand-Archive .\Debian.zip .\Debian 就是改名后解压到指定目录,所以不用PowerShell也行,7-Zip操作没毛病,可以在下 载时直接重命名。 cd .\Debian\ .\debian.exe 将看到如下提示,根据提示做即可。 Installing, this may take a few minutes... Please create a default UNIX user account. The username does not need to match your Windows username. For more information visit: https://aka.ms/wslusers Enter new UNIX username: scz (不需要与Win10用户同名) Enter new UNIX password: Retype new UNIX password: passwd: password updated successfully Installation successful! 最终发行版根文件系统位于: D:\WSL\Debian\rootfs\ 所以$HOME位于: D:\WSL\Debian\rootfs\home\scz\ 5) 进bash 安装结束时自动进入bash,exit退回PowerShell。再次执行".\debian.exe",再次进 入bash。 在bash中通过/mnt/目录访问Windows的盘符。 假设用前述办法只安装了一个Linux发行版,可以直接执行bash或wsl进入bash,不过 这样进入后的当前目录有所不同。".\debian.exe"进bash后当前目录是$HOME或者 "/home/scz",bash或wsl进bash后当前目录同cmd。 在资源管理器中选中某目录,按住Shift键,右键菜单里有"在此处打开Linux shell", 这个就是进bash后切换到某目录。 6) 在多个Linux发行版之间切换 WSL commands and launch configurations https://docs.microsoft.com/en-us/windows/wsl/wsl-config 有可能用前述办法安装过多个Linux发行版,直接执行bash只会进缺省发行版。 $ wslconfig /list /all 适用于 Linux 的 Windows 子系统分发版: Debian (默认) $ wsl --list --all 适用于 Linux 的 Windows 子系统分发版: Debian (默认) 用wslconfig或wsl可以指定缺省发行版: $ wslconfig /setdefault Debian $ wsl --set-default Debian 早期使用wslconfig,现在好像都用wsl。 上面的操作只是方便bash直接进bash,完全可以切换目录后用相应的exe进相应的发 行版。 ☆ 迁移WSL1 虽然前面已经将Linux发行版安装到指定目录,但仍可能面临迁移到其他目录的需求; 或者之前通过GUI缺省安装到C盘了,现在C盘空间紧张,想迁移到其他盘。 1) LxRunOffline 据说开源项目LxRunOffline可用来迁移发行版,但我没有测试它,列于此间备忘。 https://github.com/DDoSolitary/LxRunOffline https://github.com/DDoSolitary/LxRunOffline/releases https://github.com/DDoSolitary/LxRunOffline/releases/download/v3.5.0/LxRunOffline-v3.5.0-msvc.zip 查看已安装发行版 LxRunOffline.exe list 迁移已安装指定发行版到指定目录 LxRunOffline.exe move -n Debian -d X:\WSL\Debian 查看已安装指定发行版所在目录 LxRunOffline.exe get-dir -n Debian 2) 手工迁移已安装发行版 $ wsl --list --all -v NAME STATE VERSION * Debian Stopped 1 假设有个窗口已经进入bash,上述STATE列将是Running,VERSION列应该是说现在用 的是WSL1,而不是WSL2。先退出bash再干后面的事。 wsl --export Debian T:\Debian.tar (中转文件) wsl --unregister Debian wsl --import Debian X:\WSL\Debian T:\Debian.tar --version 1 cd /d X:\WSL\Debian .\debian.exe config --default-user scz (指定成最初安装时创建的用户) del T:\Debian.tar (不再需要) 3) 不同主机共用一枚U盘 曾经有人问过,把发行版装到U盘上,能不能在两台主机间共用这枚U盘上的发行版? 没有实测过,瞎猜一下,通过diskmgmt.msc给U盘指定盘符,确保U盘插入两台不同主 机时获取的盘符是同一个,应该能共用之。 ☆ WSL1中访问网络映射盘或VHD Q: 意外发现Win10的VHD在WSL1中看不到,"/mnt/x"是空的,"net use"映射的盘在WSL1 中也看不到。 A: How to Mount Windows Network Drives in WSL https://www.public-health.uiowa.edu/it/support/kb48568/ 这两种情况不会自动映射进WSL1,得在WSL1中手工mount。假设盘符是T,在WSL1中执 行 sudo mount -t drvfs T: /mnt/t 反命令是 sudo umount /mnt/t 若想持久化,在/etc/fstab中新增一行 T: /mnt/t drvfs defaults 0 0 执行如下命令重新加载fstab,热生效 sudo mount -a ☆ 安装Windows Terminal Install and set up Windows Terminal https://docs.microsoft.com/en-us/windows/terminal/get-started https://aka.ms/terminal 通过"Windows Terminal"进bash时当前目录是"/mnt/c/Users/scz"。bluerust推荐使 用,测试后果然不错。 ☆ 简单升级Linux发行版 既然Linux发行版已经安装到非系统盘,可以放心大胆地升级并安装软件,不怕空间 不够。 $ cat /etc/os-release PRETTY_NAME="Debian GNU/Linux 9 (stretch)" NAME="Debian GNU/Linux" VERSION_ID="9" VERSION="9 (stretch)" ID=debian HOME_URL="https://www.debian.org/" SUPPORT_URL="https://www.debian.org/support" BUG_REPORT_URL="https://bugs.debian.org/" $ cat /etc/debian_version 9.5 参看 Debian 9/stretch moved to archive.debian.org - [2023-03-27] https://lists.debian.org/debian-devel-announce/2023/03/msg00006.html $ cat /etc/apt/sources.list # deb http://deb.debian.org/debian stretch main # deb http://deb.debian.org/debian stretch-updates main deb http://archive.debian.org/debian stretch main deb http://archive.debian.org/debian stretch-updates main # deb http://security.debian.org/debian-security/ stretch/updates main sudo apt-get update sudo apt-get install apt-file sudo apt-file update sudo apt-get install xxd sed wget coreutils gcc g++ 这些是升级 sudo apt-get install binutils gawk gdb python3-minimal python3-pip python-minimal python-pip perl-base sudo apt-get install curl socat openssh-client openssl nmap tcpdump net-tools git maven 这些是新装 sudo apt-get install gnutls-bin dnsutils netcat-openbsd john man-db zip p7zip-full rhash 用缺省sources.list找不到这些包: radare2 ☆ 文件系统的坑 在WSL1中"touch /tmp/some",在Win10中删了"D:\WSL\Debian\rootfs\tmp\some", 然后发现Host、Guest文件系统不同步了。Guest中还能看到"/tmp/some",但也删不 掉它,总报找不到文件: $ rm /tmp/some rm: cannot remove '/tmp/some': No such file or directory ls又能看到,退出bash也无法恢复同步状态。我知道不应该这样干,但已经手欠过了, 怎么办?本想看看重启Host后如何,结果发现关闭"Windows Terminal"之后恢复正常。 估计之前只退出bash仍有句柄被占用,关闭"Windows Terminal"才全部释放。以后再 不手欠了。 ☆ "Windows Subsystem for Linux"组件的一些讨论 出于各种个人喜好,综合考量后,相比WSL2,更喜欢WSL1。若只用WSL1,下列组件无 需安装: -------------------------------------------------------------------------- 中文版: Hyper-V (下面有子结点) 虚拟机平台 Windows虚拟机监控程序平台 -------------------------------------------------------------------------- 英文版: Hyper-V (下面有子结点) Virtual Machine Platform Windows Hypervisor Platform -------------------------------------------------------------------------- 执行OptionalFeatures.exe,GUI方式挑选组件。按微软官方文档说法,WSL1只与一 个组件相关: 适用于Linux的Windows子系统 (中文版) Windows Subsystem for Linux (英文版) 查看这些组件的状态: Get-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V-All Get-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform Get-WindowsOptionalFeature -Online -FeatureName HypervisorPlatform Get-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux dism.exe /Online /Get-Features | find "Hyper-V" dism.exe /Online /Get-Features | find "VirtualMachinePlatform" dism.exe /Online /Get-Features | find "HypervisorPlatform" dism.exe /Online /Get-Features | find "Microsoft-Windows-Subsystem-Linux"