admin管理员组

文章数量:1613763

Windows Subsystem for Linux [WSL2] - WSL 的基本命令

  • 1. Windows Subsystem for Linux
    • 1.1. WSL2
    • 1.2. Comparing WSL 1 and WSL 2
  • 2. Basic commands for WSL
  • References

Windows Subsystem for Linux Documentation
https://learn.microsoft/en-us/windows/wsl/

Windows Subsystem for Linux (WSL) lets developers run a GNU/Linux environment - including most command-line tools, utilities, and applications - directly on Windows, unmodified, without the overhead of a traditional virtual machine or dual-boot setup.
Windows Subsystem for Linux (WSL) 可让开发人员直接在 Windows 上按原样运行 GNU/Linux 环境 (包括大多数命令行工具、实用工具和应用程序),且不会产生传统虚拟机或双启动设置开销。

1. Windows Subsystem for Linux

  • Use GPU acceleration for machine learning, data science scenarios and more
    将 GPU 加速用于机器学习、数据科学场景等

  • Run GNU/Linux graphical applications integrated directly to your Windows desktop
    运行直接集成到 Windows 桌面的 GNU/Linux 图形应用程序

  • Invoke GNU/Linux applications on Windows
    在 Windows 上调用 GNU/Linux 应用程序。

  • Invoke Windows applications using a Unix-like command-line shell
    使用类似于 Unix 的命令行 shell 调用 Windows 应用程序

  • Install additional software using your own GNU/Linux distribution package manager
    使用自己的 GNU/Linux 分发包管理器安装其他软件

  • Run Bash shell scripts and GNU/Linux command-line applications
    运行 Bash shell 脚本和 GNU/Linux 命令行应用程序

  • Run common command-line tools such as grep, sed, awk, or other ELF-64 binaries
    运行常用的命令行软件工具 (例如 grep、sed、awk) 或其他 ELF-64 二进制文件

  • Choose your favorite GNU/Linux distributions from the Microsoft Store
    在 Microsoft Store 中选择你偏好的 GNU/Linux 分发版

1.1. WSL2

WSL 2 is a new version of the Windows Subsystem for Linux architecture that powers the Windows Subsystem for Linux to run ELF64 Linux binaries on Windows. Its primary goals are to increase file system performance, as well as adding full system call compatibility.
WSL 2 是适用于 Linux 的 Windows 子系统体系结构的一个新版本,它支持适用于 Linux 的 Windows 子系统在 Windows 上运行 ELF64 Linux 二进制文件。 它的主要目标是提高文件系统性能,以及添加完全的系统调用兼容性。

这一新的体系结构改变了这些 Linux 二进制文件与Windows 和计算机硬件进行交互的方式,但仍然提供与 WSL 1 中相同的用户体验。

单个 Linux 分发版可以在 WSL 1 或 WSL 2 体系结构中运行。每个分发版可随时升级或降级,并且你可以并行运行 WSL 1 和 WSL 2 分发版。WSL 2 使用全新的体系结构,该体系结构受益于运行真正的 Linux 内核。

1.2. Comparing WSL 1 and WSL 2

WSL 1 和 WSL 2 之间的主要区别在于,在托管 VM 内使用实际的 Linux 内核、支持完整的系统调用兼容性以及跨 Linux 和 Windows 操作系统的性能。WSL 2 是安装 Linux 发行版时的当前默认版本,它使用最新最好的虚拟化技术在轻量级实用工具虚拟机 (VM) 内运行 Linux 内核。

Comparing features


WSL 2 will work with VMware 15.5.5+ and VirtualBox 6+.

WSL 2 是对基础体系结构的一次重大改造,它使用虚拟化技术和 Linux 内核来实现其新功能。 此更新的主要目标是提高文件系统性能和添加完全的系统调用兼容性。

Full Linux kernel
WSL 2 中的 Linux 内核是 Microsoft 根据最新的稳定版分支 (基于 kernel 上提供的源代码) 构建的。此内核已专门针对 WSL 2 进行了调整,针对大小和性能进行了优化,以便在 Windows 上提供良好的 Linux 体验。内核将由 Windows 更新提供服务,这意味着你将获得最新的安全修补程序和内核改进功能,而无需自行管理它。WSL 2 Linux 内核是开源的。

我们建议使用 WSL 2,因为它提供更快的性能和100% 的系统调用兼容性。

Consider trying the VS Code Remote WSL Extension to enable you to store your project files on the Linux file system, using Linux command line tools, but also using VS Code on Windows to author, edit, debug, or run your project in an internet browser without any of the performance slow-downs associated with working across the Linux and Windows file systems.
https://learn.microsoft/en-us/windows/wsl/tutorials/wsl-vscode
请考虑尝试 VS Code 远程 WSL 扩展,以便使你不仅能够使用 Linux 命令行工具将项目文件存储在 Linux 文件系统上,而且还可以使用 Windows 上的 VS Code 在 Internet 浏览器中创作、编辑、调试或运行项目,而不会造成任何与跨 Linux 和 Windows 文件系统工作相关联的性能下降。

2. Basic commands for WSL

The WSL commands below are listed in a format supported by PowerShell or Windows Command Prompt. To run these commands from a Bash / Linux distribution command line, you must replace wsl with wsl.exe. For a full list of commands, run wsl --help.
以下 WSL 命令以 PowerShell 或 Windows 命令提示符支持的格式列出。若要通过 Bash/Linux 发行版命令行运行这些命令,必须将 wsl 替换为 wsl.exe。若要获取完整的命令列表,请运行 wsl --help

  • List available Linux distributions - 列出可用的 Linux 发行版
C:\Users\cheng>wsl --list --online
以下是可安装的有效分发的列表。
请使用“wsl --install -d <分发>”安装。

NAME                                   FRIENDLY NAME
Ubuntu                                 Ubuntu
Debian                                 Debian GNU/Linux
kali-linux                             Kali Linux Rolling
Ubuntu-18.04                           Ubuntu 18.04 LTS
Ubuntu-20.04                           Ubuntu 20.04 LTS
Ubuntu-22.04                           Ubuntu 22.04 LTS
OracleLinux_8_5                        Oracle Linux 8.5
OracleLinux_7_9                        Oracle Linux 7.9
SUSE-Linux-Enterprise-Server-15-SP4    SUSE Linux Enterprise Server 15 SP4
openSUSE-Leap-15.4                     openSUSE Leap 15.4
openSUSE-Tumbleweed                    openSUSE Tumbleweed

C:\Users\cheng>
C:\Users\cheng>wsl -l -o
以下是可安装的有效分发的列表。
请使用“wsl --install -d <分发>”安装。

NAME                                   FRIENDLY NAME
Ubuntu                                 Ubuntu
Debian                                 Debian GNU/Linux
kali-linux                             Kali Linux Rolling
Ubuntu-18.04                           Ubuntu 18.04 LTS
Ubuntu-20.04                           Ubuntu 20.04 LTS
Ubuntu-22.04                           Ubuntu 22.04 LTS
OracleLinux_8_5                        Oracle Linux 8.5
OracleLinux_7_9                        Oracle Linux 7.9
SUSE-Linux-Enterprise-Server-15-SP4    SUSE Linux Enterprise Server 15 SP4
openSUSE-Leap-15.4                     openSUSE Leap 15.4
openSUSE-Tumbleweed                    openSUSE Tumbleweed

C:\Users\cheng>
  • List installed Linux distributions - 列出已安装的 Linux 发行版

查看安装在 Windows 计算机上的 Linux 发行版列表,其中包括状态和运行发行版的 WSL 版本 (WSL 1 或 WSL 2)。--all (列出所有发行版)、--running (仅列出当前正在运行的发行版) 或 --quiet (仅显示发行版名称)。

C:\Users\cheng>wsl --list --verbose
  NAME            STATE           VERSION
* Ubuntu-20.04    Running         2

C:\Users\cheng>
C:\Users\cheng>wsl -l -v
  NAME            STATE           VERSION
* Ubuntu-20.04    Running         2

C:\Users\cheng>
  • Set default WSL version - 设置默认 WSL 版本
wsl --set-default-version <Version>

wsl --set-default-version 1
wsl --set-default-version 2

若要将默认版本设置为 WSL 1 或 WSL 2,请将 <Version> 替换为数字 1 或 2,表示对于安装新的 Linux 发行版,你希望默认使用哪个版本的 WSL。

  • Set default Linux distribution - 设置默认 Linux 发行版
wsl --set-default <Distribution Name>

wsl --set-default Ubuntu-20.04
wsl --set-default Ubuntu-22.04

若要设置 WSL 命令将用于运行的默认 Linux 发行版,请将 <Distribution Name> 替换为你首选的 Linux 发行版的名称。

  • Check WSL status - 检查 WSL 状态
C:\Users\cheng>wsl --status
默认分发:Ubuntu-20.04
默认版本:2

适用于 Linux 的 Windows 子系统最后更新于 2023/2/15
适用于 Linux 的 Windows 子系统内核可以使用“wsl --update”手动更新,但由于你的系统设置,无法进行自动更新。
 若要接收自动内核更新,请启用 Windows 更新设置:“在更新 Windows 时接收其他 Microsoft 产品的更新”。
 有关详细信息,请访问https://aka.ms/wsl2kernel。

内核版本: 5.10.16

C:\Users\cheng>
  • Shutdown - 关闭

立即终止所有正在运行的发行版和 WSL 2 轻量级实用工具虚拟机。

wsl --shutdown
  • Install
wsl --install --distribution <Distribution Name>
wsl --install -d <Distribution Name>

运行 wsl --install <Distribution Name> 来安装其他 Linux 发行版。 若要获取发行版名称的有效列表,请运行 wsl --list --online

  • Import and export a distribution (导入和导出)
wsl --export <Distribution Name> <FileName>
wsl --import <Distribution Name> <InstallLocation> <FileName>

Imports and exports the specified tar file as a new distribution. The filename can be - for standard input.
将指定 tar 文件导入和导出为新的发行版。在标准输入中,文件名可以是 -

Options include:
--vhd: Specifies the import/export distribution should be a .vhdx file instead of a tar file (指定导入/导出发行版应为 .vhdx 文件,而不是 tar 文件)
--version: For import only, specifies whether to import the distribution as a WSL 1 or WSL 2 distribution (仅导入,指定将发行版导入为 WSL 1 还是 WSL 2 发行版)

References

[1] Yongqiang Cheng, https://yongqiang.blog.csdn/
[2] Windows Subsystem for Linux Documentation, https://learn.microsoft/en-us/windows/wsl/

本文标签: 命令SubsystemWindowsLinuxWSL