适用于 Linux 的 Windows 子系统的命令参考Command Reference for Windows Subsystem for Linux
09/15/2020
本文内容
与适用于 Linux 的 Windows 子系统交互的最佳方式是使用 wsl.exe 命令。The best way to interact with the Windows Subsystem for Linux is to use the wsl.exe command.
将 WSL 2 设置为默认版本Set WSL 2 as your default version
安装新的 Linux 分发版时,请在 Powershell 中运行以下命令,以将 WSL 2 设置为默认版本:Run the following command in Powershell to set WSL 2 as the default version when installing a new Linux distribution:
wsl --set-default-version 2
备注
如果你正在运行 32 位进程来访问 wsl.exe(一种 64 位工具),那么你可能需要按如下方式运行上述命令:C:\Windows\Sysnative\wsl.exe --set-default-version 2If you're running a 32-bit process in order to access wsl.exe (a 64-bit tool), you may have to run the above command in the following manner: C:\Windows\Sysnative\wsl.exe --set-default-version 2
将分发版版本设置为 WSL 1 或 WSL 2Set your distribution version to WSL 1 or WSL 2
可以打开 PowerShell 命令行并输入以下命令(仅在 Windows 内部版本 19041 或更高版本中可用),来检查分配给每个已安装的 Linux 分发版的 WSL 版本:wsl -l -vYou can check the WSL version assigned to each of the Linux distributions you have installed by opening the PowerShell command line and entering the command (only available in Windows Build 19041 or higher): wsl -l -v
wsl --list --verbose
若要将分发版设置为受某一 WSL 版本支持,请运行:To set a distribution to be backed by either version of WSL please run:
wsl --set-version
请确保将 替换为你的分发版的实际名称,并将 替换为数字“1”或“2”。Make sure to replace with the actual name of your distribution and with the number '1' or '2'. 可以随时更改回 WSL 1,方法是运行与上面相同的命令,但将“2”替换为“1”。You can change back to WSL 1 at anytime by running the same command as above but replacing the '2' with a '1'.
此外,如果要使 WSL 2 成为你的默认体系结构,可以通过此命令执行该操作:Additionally, if you want to make WSL 2 your default architecture you can do so with this command:
wsl --set-default-version 2
这会将安装的任何新分发版的版本设置为 WSL 2。This will set the version of any new distribution installed to WSL 2.
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 commands
不带参数Without arguments
如果未提供命令行,wsl.exe 将启动默认 shell。If no command line is provided, wsl.exe launches the default shell.
--exec, -e --exec, -e
执行指定的命令,但不使用默认的 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:
--distribution, -d --distribution, -d
运行指定的分发版。Run the specified distribution.
--user, -u --user, -u
以指定用户的身份运行。Run as the specified user.
用于管理适用于 Linux 的 Windows 子系统的参数Arguments for managing Windows Subsystem for Linux
--export --export
将分发版导出到 tar 文件。Exports the distribution to a tar file. 在标准输出中,文件名可以是 -。The filename can be - for standard output.
--import --import
导入指定的 tar 文件作为新的分发版。Imports the specified tar file as a new distribution. 在标准输入中,文件名可以是 -。The filename can be - for standard input.
--list、-l [选项]--list, -l [Options]
列出分发版。Lists distributions.
选项:Options:
--all--all
列出所有分发版,包括当前正在安装或卸载的分发版。List all distributions, including distributions that are currently being installed or uninstalled.
--running--running
仅列出当前正在运行的分发版。List only distributions that are currently running.
--set-default, -s --set-default, -s
将分发版设置为默认值。Sets the distribution as the default.
--terminate, -t --terminate, -t
终止指定的分发版。Terminates the specified distribution.
--unregister --unregister
注销分发版。Un-register the distribution.
--help 显示用法信息。--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 Sets the distribution as the default.
/t, /terminate 终止发行版。/t, /terminate Terminates the distribution.
/u, /unregister 注销发行版。/u, /unregister Un-registers the distribution.
/upgrade 将发行版升级为 WslFs 文件系统格式。/upgrade 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 ""-c ""
运行命令,列显输出,并返回到 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.
命令Command
说明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
设置默认的 Bash on Ubuntu 用户。Sets the default Bash on Ubuntu user. 如果指定的用户不存在,该命令会提示输入密码。Will prompt for a password if the specified user does not exist. For more information visit: https://aka.ms/wslusers.
/y 绕过密码提示。/y Bypasses promping for the password. 将创建不带密码的用户。The user will be created without a password.
lxrun /update
更新子系统的包索引Updates the subsystem's package index