Windows 10/WSL

Windows 10/WSL

在 control - turn on / off windows features 里启用 WSL (Windows subsystem for Linux)

(或通过 PowerShell:
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux)

下载 Linux 发行版 for Windows 10:

将下载的Appx文件后缀改为 zip,解压缩到某个目录,执行里面的 ubuntu1804.exe 进行安装即可。请注意直接安装在当前目录里。

如果安装的是非 LTSC 版 Windows 10,也可以直接从 Store 下载 Ubuntu。

安装后可以在 Windows 资源管理器 shift + 右键选择“Open Linux shell here”。这个 context menu 实际上做的是在当前目录的 cmd 里执行 "linuxdist.exe run"。

wsl2

默认安装的 linux 发行版使用 wsl1。要使用 wsl2:

Start a powershell as admin

Run wsl -l -v to check your versions and installations. Example output:

PS C:\WINDOWS\system32> wsl -l -v
NAME            STATE           VERSION
* Ubuntu-18.04    Stopped         1
Ubuntu-20.04    Stopped         1

Upgrade with wsl --set-version Ubuntu-20.04 2. Example:

PS C:\WINDOWS\system32> wsl --set-version Ubuntu-20.04 2
Conversion in progress, this may take a few minutes...
For information on key differences with WSL 2 please visit https://aka.ms/wsl2
Conversion complete.

If you get this error:

PS C:\WINDOWS\system32> wsl --set-version Ubuntu-20.04 2
Conversion in progress, this may take a few minutes...
For information on key differences with WSL 2 please visit https://aka.ms/wsl2
The requested operation could not be completed due to a virtual disk system limitation.  Virtual hard disk files must be uncompressed and unencrypted and must not be sparse.

Go to the Ubuntu directory in your profile folder (something like %USERPROFILE%\AppData\Local\Packages\CanonicalGroupLimited...).Right click on "LocalState", Properties, Advanced, Deselect "Compress contents" (and I suppose "Encrypt contents" as well if that's checked). When it asks whether you want to apply to just this folder or to all subfolders and files, you can say "just this folder", because all you're doing is clearing that "compress" flag. After that the wsl --set-version command should work.

管理已安装的 Linux 发行版

# list
wslconfig /l

# unregister
wslconfig /u Ubuntu-18.04

Tips

fs

fsutil.exe file queryCaseSensitiveInfo
fsutil.exe file setCaseSensitiveInfo [enable | disable]

npm package fails to install "npm ERR! Error: EACCES: permission denied"

npm config set unsafe-perm=true

sign into “root” account by default on Windows Subsystem for Linux Bash terminal

<distro> config --default-user root

Substitute the distro's exe name for <distro> in the command above.

设置后需要运行一次 exe 生效。

所以对于 ubuntu1804:

ubuntu1804 config --default-user root & ubuntu1804.exe

如果 PATH 里没有 ubuntu1804 命令,请到 wsl linux 发行版的安装目录的 cmd 里执行。

备注:对于旧版 Windows 10 的 WSL, For versions of WSL up to the Fall Creators Update, run the following command in an elevated Windows command prompt:

lxrun /setdefaultuser root

"Wrong __data_start/_end pair"

wsl1 的linux里运行某些程序可能报上面这个错误。解决方法是升级到 wsl2。


Last update: 2021-01-12 02:41:26 UTC