
标题: Win10上安装WSL1
创建: 2020-10-29 10:02
更新:
链接: http://scz.617.cn:8/windows/202010291002.txt
目录:
☆ WSL简介
1) 什么是WSL
2) WSL1 vs WSL2
☆ 安装WSL1
1) 启用或关闭Windows功能
2) 创建Linux发行版的安装目录
3) 手工下载Linux发行版
4) 手工安装Linux发行版
5) 进bash
6) 在多个Linux发行版之间切换
☆ 迁移WSL1
1) LxRunOffline
2) 手工迁移已安装发行版
3) 不同主机共用一枚U盘
☆ 安装Windows Terminal
☆ 简单升级Linux发行版
☆ 坑
☆ WSL简介
1) 什么是WSL
What is the Windows Subsystem for Linux?
https://docs.microsoft.com/en-us/windows/wsl/about
简单点说,装了WSL,就可以在Windows中执行ELF
2) WSL1 vs WSL2
Comparing WSL 1 and WSL 2
https://docs.microsoft.com/en-us/windows/wsl/compare-versions
这里比较了WSL1、WSL2,MS推荐使用WSL2。
原始需求是用现成的Linux命令行工具,并不打算在WSL中搞正经开发或者用GUI程序,所以WSL1足够了。在网上搜了搜,WSL2多多少少与VMware有冲突,不可能为了WSL2放弃VMware。
☆ 安装WSL1
Windows Subsystem for Linux Installation Guide for Windows 10
https://docs.microsoft.com/en-us/windows/wsl/install-win10
1) 启用或关闭Windows功能
有多种进入途径:
control
Programs and Features (程序和功能)
Turn Windows features on or off (启用或关闭Windows功能)
Settings
Apps
Apps & features
Programs and Features
在此选中:
适用于Linux的Windows子系统
这个默认没有选中。为了让它生效,可能需要重启OS,总之根据提示来。
可以命令行启用,在管理员级的PowerShell中执行:
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
可以看到WSL是否已启用:
Path :
Online : True
RestartNeeded : False
我早就重启过OS,所以此次显示不需要重启。
还可以:
dism.exe /Online /Enable-Feature /FeatureName:Microsoft-Windows-Subsystem-Linux /ALL /NoRestart
2) 创建Linux发行版的安装目录
Microsoft Store
https://aka.ms/wslstore
在Chrome中访问上述URL,提示打开本机Microsoft Store,有点意思。
WSL只是个Windows子系统,最终要用的是Microsoft Store中提供的某种Linux发行版。网上大多数简明教程都是缺省安装流程,此时这些Linux发行版缺省被安装到:
%LOCALAPPDATA%\Packages\<package name>\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
不要用这种方式安装:
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进入bash,不过这样进入后的当前目录有所不同。".\debian.exe"进bash后当前目录是HOME或者"/home/scz",bash进bash后当前目录是"/mnt/d/WSL/Debian"。
在资源管理器中选中某目录,按住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盘插入两台不同主机时获取的盘符是同一个,应该能共用之。
☆ 安装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
$ cat /etc/apt/sources.list
deb http://deb.debian.org/debian stretch main
deb http://deb.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
这些是新装
用缺省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"才全部释放。以后再不手欠了。




