分享

Basic commands for WSL | Microsoft Learn

 丝广里 2023-10-07 发布于湖南

Basic commands for WSL 适用于 WSL 的基本命令

  • Article

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

Install

PowerShell
wsl --install

Install WSL and the default Ubuntu distribution of Linux. Learn more. You can also use this command to install additional Linux distributions by running wsl --install <Distribution Name>. For a valid list of distribution names, run wsl --list --online.
安装 WSL 和默认的 Linux Ubuntu 发行版。 了解更多信息。您还可以使用此命令通过运行 wsl --install <Distribution Name> 来安装其他 Linux 发行版。有关分发名称的有效列表,请运行 wsl --list --online

Options include:

  • --distribution: Specify the Linux distribution to install. You can find available distributions by running wsl --list --online.
    --distribution :指定要安装的 Linux 发行版。您可以通过运行 wsl --list --online 来查找可用的发行版。
  • --no-launch: Install the Linux distribution but do not launch it automatically.
    --no-launch :安装 Linux 发行版,但不自动启动它。
  • --web-download: Install from an online source rather than using the Microsoft Store.
    --web-download :从联机源安装,而不是使用 Microsoft 应用商店。

When WSL is not installed options include:
未安装 WSL 时,选项包括:

  • --inbox: Installs WSL using the Windows component instead of using the Microsoft Store. (WSL updates will be received via Windows updates, rather than pushed out as-available via the store).
    --inbox :使用 Windows 组件而不是Microsoft存储安装 WSL。(WSL 更新将通过 Windows 更新接收,而不是通过应用商店推送为可用)。
  • --enable-wsl1: Enables WSL 1 during the install of the Microsoft Store version of WSL by also enabling the "Windows Subsystem for Linux" optional component.
    --enable-wsl1 :在安装Microsoft存储版本的 WSL 期间启用 WSL 1,方法是同时启用“Windows Subsystem for Linux”可选组件。
  • --no-distribution: Do not install a distribution when installing WSL.
    --no-distribution :安装 WSL 时不要安装分发。

Note

If you running WSL on Windows 10 or an older version, you may need to include the -d flag with the --install command to specify a distribution: wsl --install -d <distribution name>.
如果在 Windows 10 或旧版本上运行 WSL,则可能需要在 --install 命令中包含 -d 标志以指定分配: wsl --install -d <distribution name>

List available Linux distributions
列出可用的 Linux 发行版

PowerShell
wsl --list --online

See a list of the Linux distributions available through the online store. This command can also be entered as: wsl -l -o.
查看通过在线商店提供的 Linux 发行版列表。此命令也可以输入为: wsl -l -o

List installed Linux distributions
列出已安装的 Linux 发行版

PowerShell
wsl --list --verbose

See a list of the Linux distributions installed on your Windows machine, including the state (whether the distribution is running or stopped) and the version of WSL running the distribution (WSL 1 or WSL 2). Comparing WSL 1 and WSL 2. This command can also be entered as: wsl -l -v. Additional options that can be used with the list command include: --all to list all distributions, --running to list only distributions that are currently running, or --quiet to only show distribution names.
查看 Windows 计算机上安装的 Linux 发行版的列表,包括状态(分发是正在运行还是已停止)和运行该发行版的 WSL 版本(WSL 1 或 WSL 2)。 比较 WSL 1 和 WSL 2 .此命令也可以输入为: wsl -l -v 。可与 list 命令一起使用的其他选项包括: --all 列出所有分配, --running 仅列出当前正在运行的分配,或 --quiet 仅显示分配名称。

Set WSL version to 1 or 2
将 WSL 版本设置为 1 或 2

PowerShell
wsl --set-version <distribution name> <versionNumber>

To designate the version of WSL (1 or 2) that a Linux distribution is running on, replace <distribution name> with the name of the distribution and replace <versionNumber> with 1 or 2. Comparing WSL 1 and WSL 2. WSL 2 is only available in Windows 11 or Windows 10, Version 1903, Build 18362 or later.
若要指定运行 Linux 分发版的 WSL(1 或 2)版本,请将 <distribution name> 替换为分发版的名称,并将 <versionNumber> 替换为 1 或 2。 比较 WSL 1 和 WSL 2 .WSL 2 仅在 Windows 11 或 Windows 10 版本 1903、内部版本 18362 或更高版本中可用。

Set default WSL version 设置默认 WSL 版本

PowerShell
wsl --set-default-version <Version>

To set a default version of WSL 1 or WSL 2, replacing <Version> with either the number 1 or 2 to represent which version of WSL you would like the installation to default on for new Linux distribution installations. For example, wsl --set-default-version 2. Comparing WSL 1 and WSL 2. WSL 2 is only available in Windows 11 or Windows 10, Version 1903, Build 18362 or later.
若要设置 WSL 1 或 WSL 2 的默认版本,请将 <Version> 替换为数字 1 或 2,以表示你希望新 Linux 分发版安装默认安装的 WSL 版本。例如, wsl --set-default-version 2 . 比较 WSL 1 和 WSL 2 .WSL 2 仅在 Windows 11 或 Windows 10 版本 1903、内部版本 18362 或更高版本中可用。

Set default Linux distribution 设置默认的 Linux 发行版

PowerShell
wsl --set-default <Distribution Name>

To set the default Linux distribution that WSL commands will use to run, replace <Distribution Name> with the name of your preferred Linux distribution.
若要设置 WSL 命令将用于运行的默认 Linux 分发版,请将 <Distribution Name> 替换为首选 Linux 分发版的名称。

Change directory to home 将目录更改为主页

PowerShell
wsl ~

The ~ can be used with wsl to start in the user's home directory. To jump from any directory back to home from within a WSL command prompt, you can use the command: cd ~.
~ 可以与 wsl 一起使用,以在用户的主目录中启动。若要从 WSL 命令提示符内从任何目录跳转到主目录,可以使用以下命令: cd ~

Run a specific Linux distribution from PowerShell or CMD
从PowerShell或CMD运行特定的Linux发行版

PowerShell
wsl --distribution <Distribution Name> --user <User Name>

To run a specific Linux distribution with a specific user, replace <Distribution Name> with the name of your preferred Linux distribution (ie. Debian) and <User Name> with the name of an existing user (ie. root). If the user doesn't exist in the WSL distribution, you will receive an error. To print the current user name, use the command whoami.
若要与特定用户一起运行特定的 Linux 发行版,请将 <Distribution Name> 替换为首选 Linux 发行版的名称(即。Debian) 和 <User Name> 与现有用户(即 root)的名称。如果用户在 WSL 分发中不存在,你将收到错误。要打印当前用户名,请使用命令 whoami

Update WSL

PowerShell
wsl --update

Update your WSL version to the latest version. Options include:
将 WSL 版本更新到最新版本。选项包括:

  • --web-download: Download the latest update from the GitHub rather than the Microsoft Store.
    --web-download :从 GitHub 而不是Microsoft存储下载最新更新。

Check WSL status 检查 WSL 状态

PowerShell
wsl --status

See general information about your WSL configuration, such as default distribution type, default distribution, and kernel version.
请参阅有关 WSL 配置的常规信息,例如默认分发类型、默认分发和内核版本。

Check WSL version 检查 WSL 版本

PowerShell
wsl --version

Check the version information about WSL and its components.
检查有关 WSL 及其组件的版本信息。

Help command

PowerShell
wsl --help

See a list of options and commands available with WSL.
请参阅 WSL 可用的选项和命令列表。

Run as a specific user 以特定用户身份运行

PowerShell
wsl -u <Username>`, `wsl --user <Username>

To run WSL as a specified user, replace <Username> with the name of a user that exists in the WSL distribution.
若要以指定用户身份运行 WSL,请将 <Username> 替换为 WSL 分发中存在的用户的名称。

Change the default user for a distribution
更改分配的默认用户

PowerShell
<DistributionName> config --default-user <Username>

Change the default user for your distribution log-in. The user has to already exist inside the distribution in order to become the default user.
更改分发登录的默认用户。用户必须已经存在于发行版中才能成为默认用户。

For example: ubuntu config --default-user johndoe would change the default user for the Ubuntu distribution to the "johndoe" user.
例如: ubuntu config --default-user johndoe 会将 Ubuntu 发行版的默认用户更改为“johndoe”用户。

Note

If you are having trouble figuring out the name of your distribution, use the command wsl -l.
如果您在确定分配的名称时遇到问题,请使用命令 wsl -l

Warning

This command will not work for imported distributions, because these distributions do not have an executable launcher. You can instead change the default user for imported distributions using the /etc/wsl.conf file. See the Automount options in the Advanced Settings Configuration doc.
此命令不适用于导入的发行版,因为这些发行版没有可执行启动器。您可以改为使用 /etc/wsl.conf 文件更改导入分配的默认用户。请参阅高级设置配置文档中的自动挂载选项。

Shutdown

PowerShell
wsl --shutdown

Immediately terminates all running distributions and the WSL 2 lightweight utility virtual machine. This command may be necessary in instances that require you to restart the WSL 2 virtual machine environment, such as changing memory usage limits or making a change to your .wslconfig file.
立即终止所有正在运行的分发和 WSL 2 轻型实用工具虚拟机。在需要重新启动 WSL 2 虚拟机环境(例如更改内存使用限制或更改 .wslconfig 文件)的情况下,可能需要此命令。

Terminate

PowerShell
wsl --terminate <Distribution Name>

To terminate the specified distribution, or stop it from running, replace <Distribution Name> with the name of the targeted distribution.
若要终止指定的分配或停止其运行,请将 <Distribution Name> 替换为目标分配的名称。

Identify IP address 识别 IP 地址

  • wsl hostname -i for the IP address of your Linux distribution installed via WSL 2 (the WSL 2 VM address)
    wsl hostname -i 表示通过 WSL 2 安装的 Linux 分发版的 IP 地址(WSL 2 VM 地址)
  • cat /etc/resolv.conf for the IP address of the Windows machine as seen from WSL 2 (the WSL 2 VM)
    cat /etc/resolv.conf 表示从 WSL 2(WSL 2 VM)看到的 Windows 计算机的 IP 地址

Import and export a distribution 导入和导出分配

PowerShell
wsl --export <Distribution Name> <FileName>
PowerShell
wsl --import <Distribution Name> <InstallLocation> <FileName>

Imports and exports the specified tar file as a new distribution. The filename can be - for standard input. Options include:
将指定的 tar 文件作为新分发导入和导出。对于标准输入,文件名可以是 -。选项包括:

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

Import a distribution in place 就地导入分配

PowerShell
wsl --import-in-place <Distribution Name> <FileName>

Imports the specified .vhdx file as a new distribution. The virtual hard disk must be formatted in the ext4 filesystem type.
将指定的 .vhdx 文件作为新分发导入。虚拟硬盘必须格式化为 ext4 文件系统类型。

Unregister or uninstall a Linux distribution
取消注册或卸载 Linux 发行版

While Linux distributions can be installed through the Microsoft Store, they can't be uninstalled through the store.
虽然可以通过Microsoft商店安装 Linux 发行版,但无法通过商店卸载它们。

To unregister and uninstall a WSL distribution:
若要注销和卸载 WSL 分发,请执行以下操作:

PowerShell
wsl --unregister <DistributionName>

Replacing <DistributionName> with the name of your targeted Linux distribution will unregister that distribution from WSL so it can be reinstalled or cleaned up. Caution: Once unregistered, all data, settings, and software associated with that distribution will be permanently lost. Reinstalling from the store will install a clean copy of the distribution. For example, wsl --unregister Ubuntu would remove Ubuntu from the distributions available in WSL. Running wsl --list will reveal that it is no longer listed.
<DistributionName> 替换为目标 Linux 分发的名称将从 WSL 中注销该分发,以便可以重新安装或清理它。注意:取消注册后,与该发行版关联的所有数据、设置和软件将永久丢失。从存储重新安装将安装发行版的干净副本。例如, wsl --unregister Ubuntu 将从 WSL 中可用的发行版中删除 Ubuntu。运行 wsl --list 将显示它不再列出。

You can also uninstall the Linux distribution app on your Windows machine just like any other store application. To reinstall, find the distribution in the Microsoft Store and select "Launch".
您还可以像卸载任何其他商店应用程序一样卸载 Windows 计算机上的 Linux 分发应用程序。要重新安装,请在Microsoft商店中找到发行版,然后选择“启动”。

Mount a disk or device 装载磁盘或设备

PowerShell
wsl --mount <DiskPath>

Attach and mount a physical disk in all WSL2 distributions by replacing <DiskPath> with the directory\file path where the disk is located. See Mount a Linux disk in WSL 2. Options include:
通过将 <DiskPath> 替换为磁盘所在的目录\文件路径,在所有 WSL2 分发版中附加和装载物理磁盘。请参阅 在 WSL 2 中装载 Linux 磁盘 。选项包括:

  • --vhd: Specifies that <Disk> refers to a virtual hard disk.
    --vhd :指定 <Disk> 引用虚拟硬盘。
  • --name: Mount the disk using a custom name for the mountpoint
    --name :使用装入点的自定义名称装载磁盘
  • --bare: Attach the disk to WSL2, but don't mount it.
  • --type <Filesystem>: Filesystem type to use when mounting a disk, if not specified defaults to ext4. This command can also be entered as: wsl --mount -t <Filesystem>.You can detect the filesystem type using the command: blkid <BlockDevice>, for example: blkid <dev/sdb1>.
  • --partition <Partition Number>: Index number of the partition to mount, if not specified defaults to the whole disk.
  • --options <MountOptions>: There are some filesystem-specific options that can be included when mounting a disk. For example, ext4 mount options like: wsl --mount -o "data-ordered" or wsl --mount -o "data=writeback. However, only filesystem-specific options are supported at this time. Generic options, such as ro, rw, or noatime, are not supported.

Note

If you're running a 32-bit process in order to access wsl.exe (a 64-bit tool), you may need to run the command in the following manner: C:\Windows\Sysnative\wsl.exe --command.

Unmount disks

PowerShell
wsl --unmount <DiskPath>

Unmount a disk given at the disk path, if no disk path is given then this command will unmount and detach ALL mounted disks.

Deprecated WSL commands

PowerShell
wslconfig.exe [Argument] [Options]
PowerShell
bash [Options]
PowerShell
lxrun /[Argument]

These commands were the original wsl syntax for configuring Linux distributions installed with WSL, but have been replaced with the wsl or wsl.exe command syntax.

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多