适用于 Linux 的 Windows 子系统的命令参考Command Reference for Windows Subsystem for Linux

与适用于 Linux 的 Windows 子系统交互的最佳方式是使用wsl.exe命令。The best way to interact with the Windows Subsystem for Linux is to use the wsl.exe command.

wsl.exe

下面是在使用 Windows 版本1903时使用wsl.exe的所有选项的列表。Below is a list containing all options when using wsl.exe as of Windows Version 1903.

利用wsl [Argument] [Options...] [CommandLine]Using: wsl [Argument] [Options...] [CommandLine]

用于运行 Linux 二进制文件的参数Arguments for running Linux binaries

  • 不带参数Without arguments

    如果未提供命令行, wsl 将启动默认 shell。If no command line is provided, wsl.exe launches the default shell.

  • --exec,-e <命令行 >--exec, -e <CommandLine>

    执行指定的命令, 而不使用默认的 Linux shell。Execute the specified command without using the default Linux shell.

  • --

    按原样传递剩余的命令行。Pass the remaining command line as is.

上述命令也接受以下选项:The above commands also accept the following options:

  • --分发,-d <发行版 >--distribution, -d <Distro>

    运行指定的分发。Run the specified distribution.

  • --user,-u <UserName >--user, -u <UserName>

    以指定用户身份运行。Run as the specified user.

用于管理适用于 Linux 的 Windows 子系统的参数Arguments for managing Windows Subsystem for Linux

  • --export <发行版 > <FileName >--export <Distro> <FileName>

    将分发导出到 tar 文件。Exports the distribution to a tar file. 文件名可以是-标准输出。The filename can be - for standard output.

  • --import <发行版 > <InstallLocation > <FileName >--import <Distro> <InstallLocation> <FileName>

    导入指定的 tar 文件作为新的分发。Imports the specified tar file as a new distribution. 文件名可以是-标准输入。The filename can be - for standard input.

  • --list,-l [Options]--list, -l [Options]

    列出分布。Lists distributions.

    选项:Options:

    • --all--all

      列出所有分发, 包括当前正在安装或卸载的分发。List all distributions, including distributions that are currently being installed or uninstalled.

    • --正在运行--running

      仅列出当前正在运行的分发。List only distributions that are currently running.

  • --set-default、-s <发行版 >--set-default, -s <Distro>

    将分布设置为默认值。Sets the distribution as the default.

  • --terminate,-t <发行版 >--terminate, -t <Distro>

    终止指定的分发。Terminates the specified distribution.

  • --取消<注册发行版 >--unregister <Distro>

    注销分布。Unregisters the distribution.

  • --帮助显示用法信息。--help Display usage information.

其他命令Additional Commands

还有历史命令可与适用于 Linux 的 Windows 子系统交互。There are also historic commands to interact with the Windows Subsystem for Linux. 它们的功能包含在wsl.exe中, 但仍可供使用。Their functionality is encompassed within wsl.exe, but they are still available for use.

wslconfig.exe

此命令可让你配置 WSL 分布。This command lets you configure your WSL distribution. 下面是其选项的列表。Below is a list of its options.

利用wslconfig [Argument] [Options...]Using: wslconfig [Argument] [Options...]

参数Arguments

  • /l,/list [选项]/l, /list [Options]

    列出已注册的分发。Lists registered distributions.

    选项:Options:

    • /all/all

      根据需要列出所有分发, 包括当前正在安装或卸载的分发。Optionally list all distributions, including distributions that are currently being installed or uninstalled.

    • /running/running

      仅列出当前正在运行的分发。List only distributions that are currently running.

  • /s、/setdefault <发行版 >/s, /setdefault <Distro>

    将分布设置为默认值。Sets the distribution as the default.

  • /t、/terminate <发行版 >/t, /terminate <Distro>

    终止分布。Terminates the distribution.

  • /u,/unregister <发行版 >/u, /unregister <Distro>

    注销分布。Unregisters the distribution.

  • /upgrade <发行版 >/upgrade <Distro>

    将分发升级到 WslFs 文件系统格式。Upgrades the distribution to the WslFs file system format.

bash.exe

此命令用于启动 bash shell。This command is used to start a bash shell. 下面是可用于此命令的选项。Below are the options you can use with this command.

利用bash [Options...]Using: bash [Options...]

  • 未给定选项No Option given

    在当前目录中启动 Bash shell。Launches the Bash shell in the current directory. 如果未自动安装 Bash shell, 则运行lxrun /installIf the Bash shell is not installed automatically runs lxrun /install

  • ~

    bash ~在用户的主目录中启动 bash shell。bash ~ launches the bash shell into the user's home directory. 类似于运行cd ~Similar to running cd ~.

  • -c "<command >"-c "<command>"

    运行命令, 打印输出并退出到 Windows 命令提示符。Runs the command, prints the output and exits back to the Windows command prompt.

    示例: bash -c "ls"Example: bash -c "ls".

弃用的命令Deprecated Commands

lxrun.exe是用于安装和管理适用于 Linux 的 Windows 子系统的第一个命令。The lxrun.exe was the first command used to install and manage the Windows Subsystem for Linux. 它在 Windows 10 1803 和更高版本中已弃用。It is deprecated as of Windows 10 1803 and later.

命令lxrun.exe可用于直接与适用于 Linux 的Windows 子系统 (WSL)交互。The command lxrun.exe can be used to interact with the Windows Subsystem for Linux (WSL) directly. 这些命令安装\Windows\System32在目录中, 可以在 Windows 命令提示符下或在 PowerShell 中运行。These commands are installed into the \Windows\System32 directory and may be run within a Windows command prompt or in PowerShell.

CommandCommand 描述Description
lxrun Lxrun 命令用于管理 WSL 实例。The lxrun command is used to manage the WSL instance.
lxrun /install 开始下载和安装过程。Starts the download and install process.
可以添加 /y来绕过所有提示。/y may be added to bypass all prompts. 系统会自动接受确认提示, 并将 "默认用户" 设置为 "root"。The confirmation prompt is automatically accepted and the default user is set to root.
lxrun /uninstall 卸载并删除 Ubuntu 映像。Uninstalls and deletes the Ubuntu image. 默认情况下, 这不会删除用户的 Ubuntu 主目录。By default this does not remove the user's Ubuntu home directory.
可以添加 /y来自动接受确认提示/y may be added to automatically accept the confirmation prompt
/full卸载并删除用户的 Ubuntu 主目录/full uninstalls and deletes the user's Ubuntu home directory
lxrun /setdefaultuser <userName> 设置 Ubuntu 用户上的默认 Bash。Sets the default Bash on Ubuntu user. 如果指定的用户不存在, 将提示输入密码。Will prompt for a password if the specified user does not exist. 有关详细信息, 请 https://aka.ms/wslusers 访问:。For more information visit: https://aka.ms/wslusers.
/y绕过密码的 promping。/y Bypasses promping for the password. 将创建不带密码的用户。The user will be created without a password.
lxrun /update 更新子系统的包索引Updates the subsystem's package index