二度尝鲜-wsl

分割线

指南

官方的


名词解释

  • 下面牵扯到很多名词,乍一看会蒙圈.

  • 放一个关系网络

    - hyper-v
    |- Docker(hype-v引擎)
    |- Windows
    |- wsl2
    |- wsl-linux
    |- Docker(wsl引擎)
    |- Nginx
    |- Node.js
    |- 各种系统和服务
  • hyper-v是微软家的虚拟机平台,定位于VMware相同,虽然它看起来是 Windows 内部的软件,但是开启 hyper-v 后 Windows 本身也会成为其内部的虚拟机.

  • wsl 是基于 Windows 下实现的 Linux 虚拟化技术,全称Windows Subsystem for Linux,所以这里面装的系统肯定是各种 linux,2只是版本号,一般 wsl2 简称 wsl,两个版本功能上有些差别.

    • wsl-linux就是指在 Store 里面像是 Ubuntu,Debian 等等的 linux 系统

Docker 的话详见 🌈 初探 Docker.

分割线

安装步骤

常用指令

# 打开 Windows 功能
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

wsl --list --online
wsl --install -d Ubuntu-22.04
wsl --update
wsl --unregister xxx
wsl --list --verbose

netsh winsock reset

系统

  • 首先 Windows64 位系统版本号必须大于等于 1903.

  • 系统类型只有专业版(好像家庭版和企业版不行)


功能

  • [x] 启用适用于 Linux 的 Windows 子系统

  • [x] 启用虚拟机功能


  • 如图上面两个必须开启,其他的您看着来~

    20210201160810

配置

  • 下载并安装

    下载适用于 x64 计算机的 WSL2 Linux 内核更新包

  • 然后,将 WSL 2 设置为默认版本

    • 在 PowerShell 输入

      wsl --set-default-version 2
    • 没出问题的话会提示 有关与 WSL 2 的主要区别的信息,请访问 https://aka.ms/wsl2

  • 最后,打开Microsoft Store,安装想安装的 Linux 系统

    • Ubuntu 16.04 LTS

    • Ubuntu 18.04 LTS

    • Ubuntu 20.04 LTS (主流推荐)

    • openSUSE Leap 15.1

    • SUSE Linux Enterprise Server 12 SP5

    • SUSE Linux Enterprise Server 15 SP1

    • Kali Linux

    • Debian GNU/Linux

    • Fedora Remix for WSL

    • Pengwin

    • Pengwin Enterprise

    • Alpine WSL

  • 小插曲,如果 Store 加载不出来数据,报错0x80131500,那么,请关掉你的科学上网软件 🤣


wsl-代理

可以用 clash 的 TUN 模式: https://docs.cfw.lbyczf.com/contents/tun.html#windows

分割线

安装完成

  • 第一次打开时他会先安装一小会儿,然后让你输入用户名和密码,创建新用户

    • 注意 Linux 系统输入密码时不会显示****
    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: weidows
    New password:
    Retype new password:
    passwd: password updated successfully
    Installation successful!

wsl 软件安装

apt?

Linux 中 apt 与 apt-get 命令的区别与解释

简单来说就是:apt = apt-get、apt-cache 和 apt-config 中最常用命令选项的集合。


dpkg

怎么理解 ubuntu 中的软件包管理器 apt 和 dpkg


地址映射

  • 安装 wsl 之后就可以通过\\wsl$访问到自己的虚拟机

    20210201173448
  • 映射

    20210201174422
  • 映射之后出现新盘符

    20210201174646
  • 有个奇妙的事是mnt/目录下会反过来映射你的所有本地盘符

    • 看上面那个图,我有四个盘符,然后 mnt 目录下就有c,d,e,f四个文件夹

    • 这几个映射文件夹在 Windows 文件管理器无法打开,但是在终端里可以进入

    20210201174957

分割线

VScode+wsl

Terminal

  • 网上大多数人推荐Windows Terminal,我觉得也不错,不过没用,直接用的VScode

  • 如图,点选择默认shell里面就可以选择wsl

    20210201165532

Remote 开发

  • wsl 相当于在内网开了一台电脑,会有一个内网ip

  • 我们不需要获取这个 IP,直接在 VScode 里安装Remote - WSL这个插件

  • 甚至都不需要配置,直接点左下角打开就行了

    20210201173024
  • 当然,如果不嫌麻烦拿 ssh 连接 wsl 也可以,需要在 wsl 系统内配置静态 ip,但是很多功能性上不如上面那个插件.

分割线

分割线

IDEA+wsl

terminal

  • 设置里打开 File -> Settings -> Tools -> Terminal

  • shell path 填上

    "cmd.exe" /k "wsl.exe"
  • 如图

    20210201221447

分割线

分割线

报错

<3>WSL (10) ERROR: CreateProcessParseCommon:711: Failed to translate C:\Windows\System32
<3>WSL (10) ERROR: CreateProcessParseCommon:757: getpwuid(0) failed 2
<3>WSL (10) ERROR: UtilTranslatePathList:2866: Failed to translate D:\Scoop\apps\pwsh\current
<3>WSL (10) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Windows\system32
<3>WSL (10) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Windows
<3>WSL (10) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Windows\System32\Wbem
<3>WSL (10) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Windows\System32\WindowsPowerShell\v1.0\
<3>WSL (10) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Windows\System32\OpenSSH\
<3>WSL (10) ERROR: UtilTranslatePathList:2866: Failed to translate D:\Scoop\apps\docker-desktop-with-compose\139021\docker\resources\bin
<3>WSL (10) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Program Files (x86)\Windows Kits\10\Windows Performance Toolkit\
<3>WSL (10) ERROR: UtilTranslatePathList:2866: Failed to translate D:\Scoop\apps\miniconda3\current\scripts
<3>WSL (10) ERROR: UtilTranslatePathList:2866: Failed to translate D:\Scoop\apps\miniconda3\current\Library\bin
<3>WSL (10) ERROR: UtilTranslatePathList:2866: Failed to translate D:\Scoop\apps\vscode\current\bin
<3>WSL (10) ERROR: UtilTranslatePathList:2866: Failed to translate D:\Scoop\apps\rustup\current\.cargo\bin
<3>WSL (10) ERROR: UtilTranslatePathList:2866: Failed to translate D:\Scoop\apps\nvm\current\nodejs\nodejs
<3>WSL (10) ERROR: UtilTranslatePathList:2866: Failed to translate D:\Scoop\apps\nvm\current\pnpm\bin
<3>WSL (10) ERROR: UtilTranslatePathList:2866: Failed to translate D:\Scoop\apps\nvm\current\yarn\global\node_modules\.bin
<3>WSL (10) ERROR: UtilTranslatePathList:2866: Failed to translate D:\Scoop\apps\maven\current\bin
<3>WSL (10) ERROR: UtilTranslatePathList:2866: Failed to translate D:\Scoop\apps\latex\current\texmfs\install\miktex\bin\x64
<3>WSL (10) ERROR: UtilTranslatePathList:2866: Failed to translate D:\Scoop\apps\jvms\current\store\current\bin
<3>WSL (10) ERROR: UtilTranslatePathList:2866: Failed to translate D:\Scoop\apps\gvm\current\.g\go\bin
<3>WSL (10) ERROR: UtilTranslatePathList:2866: Failed to translate D:\Scoop\apps\gvm\current\.go\GOPATH\bin
<3>WSL (10) ERROR: UtilTranslatePathList:2866: Failed to translate D:\Scoop\apps\encore\current\bin
<3>WSL (10) ERROR: UtilTranslatePathList:2866: Failed to translate D:\Scoop\apps\docker-desktop-with-compose\current\docker\resources\bin
<3>WSL (10) ERROR: UtilTranslatePathList:2866: Failed to translate D:\Scoop\apps\cudnn\current\bin
<3>WSL (10) ERROR: UtilTranslatePathList:2866: Failed to translate D:\Scoop\apps\cuda11.3\current\bin
<3>WSL (10) ERROR: UtilTranslatePathList:2866: Failed to translate D:\Scoop\apps\cuda11.3\current\libnvvp
<3>WSL (10) ERROR: UtilTranslatePathList:2866: Failed to translate D:\Scoop\shims
<3>WSL (10) ERROR: UtilTranslatePathList:2866: Failed to translate D:\Scoop\apps\cygwin-libs-hentai\current\root\bin
<3>WSL (10) ERROR: UtilTranslatePathList:2866: Failed to translate C:\Users\Weidows\AppData\Local\Microsoft\WindowsApps
Processing fstab with mount -a failed.
Failed to mount C:\, see dmesg for more details.
Failed to mount D:\, see dmesg for more details.

<3>WSL (10) ERROR: CreateProcessEntryCommon:334: getpwuid(0) failed 2
<3>WSL (10) ERROR: CreateProcessEntryCommon:505: execvpe /bin/sh failed 2
<3>WSL (10) ERROR: CreateProcessEntryCommon:508: Create process not expected to return

这是因为 wsl 默认指向了 docker-desktop-data

分割线

借物表

[1]: 本篇文章会介绍 win10 中 wsl2 的安装和使用以及遇到的常见问题比如如何固定 wsl2 地址等问题的总结。

[2]: _wsl 打不开_zhjs_abc 的博客-CSDN 博客

[3]: Docker 安装时提示 Existing installation is up to date_一株禾的博客-CSDN 博客