WSL 命令和启动配置WSL commands and launch configurations

WSL 的运行方式Ways to run WSL

安装了 WSL 后,可通过多种方式运行 Linux 分发版。There are several ways to run a Linux distribution with WSL once it's installed.

  1. 通过访问 Windows 的 "开始" 菜单并键入已安装的分发的名称,打开 Linux 分发版。Open your Linux distribution by visiting the Windows Start menu and typing the name of your installed distributions. 例如: "Ubuntu"。For example: "Ubuntu".
  2. 在 Windows 命令提示符或 PowerShell 中,输入已安装的分发的名称。From Windows Command Prompt or PowerShell, enter the name of your installed distribution. 例如:ubuntuFor example: ubuntu
  3. 在 Windows 命令提示符或 PowerShell 中,若要在当前命令行内打开默认的 Linux 分发,请输入: wsl.exeFrom Windows Command Prompt or PowerShell, to open your default Linux distribution inside your current command line, enter: wsl.exe.
  4. 在 Windows 命令提示符或 PowerShell 中,若要在当前命令行内打开默认的 Linux 分发,请输入: wsl [command]From Windows Command Prompt or PowerShell, to open your default Linux distribution inside your current command line, enter:wsl [command].

要使用的方法取决于执行的操作。Which method you should use depends on what you're doing. 如果已在 Windows 提示符或 PowerShell 窗口中打开 WSL 命令行并想要退出,请输入以下命令: exitIf you've opened a WSL command line within a Windows Prompt or PowerShell window and want to exit, enter the command: exit.

按分发版启动 WSLLaunch WSL by distribution

使用特定于分发版的应用程序运行某个分发版会在其自身的控制台窗口中将它启动。Running a distribution using it's distro-specific application launches that distribution in it's own console window.

从“开始”菜单启动 WSL

这相当于在 Microsoft Store 中单击“启动”。It is the same as clicking "Launch" in the Microsoft store.

从 Microsoft Store 启动 WSL

还可以在命令行中运行 [distribution].exe 来运行分发版。You can also run the distribution from the command line by running [distribution].exe.

以这种方式从命令行运行分发版的缺点是,会自动将工作目录从当前目录更改为分发版的主目录。The disadvantage of running a distribution from the command line in this way is that it will automatically change your working directory from the current directory to the distribution's home directory.

示例:使用 PowerShell) (Example: (using PowerShell)

PS C:\Users\sarah> pwd

Path
----
C:\Users\sarah

PS C:\Users\sarah> ubuntu

scooley@scooley-elmer:~$ pwd
/home/scooley
scooley@scooley-elmer:~$ exit
logout

PS C:\Users\sarah>

wsl 和 wsl [命令]wsl and wsl [command]

从命令行运行 WSL 的最佳方式是使用 wsl.exeThe best way to run WSL from the command line is using wsl.exe.

示例:使用 PowerShell) (Example: (using PowerShell)

PS C:\Users\sarah> pwd

Path
----
C:\Users\sarah

PS C:\Users\sarah> wsl

scooley@scooley-elmer:/mnt/c/Users/sarah$ pwd
/mnt/c/Users/sarah

这样,wsl 不仅可以保留当前工作目录,而且还允许结合 Windows 命令运行单个命令。Not only does wsl keep the current working directory in place, it lets you run a single command along side Windows commands.

示例:使用 PowerShell) (Example: (using PowerShell)

PS C:\Users\sarah> Get-Date

Sunday, March 11, 2018 7:54:05 PM

PS C:\Users\sarah> wsl
scooley@scooley-elmer:/mnt/c/Users/sarah$ date
Sun Mar 11 19:55:47 DST 2018
scooley@scooley-elmer:/mnt/c/Users/sarah$ exit
logout

PS C:\Users\sarah> wsl date
Sun Mar 11 19:56:57 DST 2018

示例:使用 PowerShell) (Example: (using PowerShell)

PS C:\Users\sarah> Get-VM

Name            State CPUUsage(%) MemoryAssigned(M) Uptime   Status
----            ----- ----------- ----------------- ------   ------
Server17093     Off   0           0                 00:00:00 Opera...
Ubuntu          Off   0           0                 00:00:00 Opera...
Ubuntu (bionic) Off   0           0                 00:00:00 Opera...
Windows         Off   0           0                 00:00:00 Opera...


PS C:\Users\sarah> Get-VM | wsl grep "Ubuntu"
Ubuntu          Off   0           0                 00:00:00 Opera...
Ubuntu (bionic) Off   0           0                 00:00:00 Opera...
PS C:\Users\sarah>

管理多个 Linux 分发版Managing multiple Linux Distributions

在 Windows 10 版本 1903 及更高版本中,可以使用在 wsl.exe 适用于 Linux 的 WINDOWS 子系统 (WSL) 中管理分发,包括列出可用的分发、设置默认分发以及卸载分发。In Windows 10 Version 1903 and later, you can use wsl.exe to manage your distributions in the Windows Subsystem for Linux (WSL), including listing available distributions, setting a default distribution, and uninstalling distributions.

每个 Linux 分发版独立管理自身的配置。Each Linux distribution independently manages its own configurations. 若要查看特定于分发版的命令,请运行 [distro.exe] /?To see distribution-specific commands, run [distro.exe] /?. 例如,ubuntu /?For example ubuntu /?.

列出分发版List distributions

wsl -l , wsl --listwsl -l , wsl --list
列出可用于 WSL 的 Linux 分发版。Lists available Linux distributions available to WSL. 如果列出了某个分发版,表示该分发版已安装且可供使用。If a distribution is listed, it's installed and ready to use.

wsl --list --all 列出所有分发,包括当前不可用的分发。wsl --list --all Lists all distributions, including ones that aren't currently usable. 这些分发版可能正在安装、卸载或处于损坏状态。They may be in the process of installing, uninstalling, or are in a broken state.

wsl --list --running 列出当前正在运行的所有分发版。wsl --list --running Lists all distributions that are currently running.

设置默认分发版Set a default distribution

默认 WSL 分布版是在命令行中运行 wsl 时运行的分发版。The default WSL distribution is the one that runs when you run wsl on a command line.

wsl -s <DistributionName>, wsl --setdefault <DistributionName>wsl -s <DistributionName>, wsl --setdefault <DistributionName>

将默认分发版设置为 <DistributionName>Sets the default distribution to <DistributionName>.

示例:使用 PowerShell) (Example: (using PowerShell)
wsl -s Ubuntu 会将我的默认分发版设置为 Ubuntu。wsl -s Ubuntu would set my default distribution to Ubuntu. 现在,当我运行 wsl npm init 时,该分发版将在 Ubuntu 中运行。Now when I run wsl npm init it will run in Ubuntu. 如果我运行 wsl,它会打开 Ubuntu 会话。If I run wsl it will open an Ubuntu session.

取消注册和重新安装分发版Unregister and reinstall a distribution

尽管可以通过 Microsoft Store 安装 Linux 分发版,但无法通过 Store 将其卸载。While Linux distributions can be installed through the Microsoft store, they can't be uninstalled through the store. 可以通过 WSL Config 来取消注册/卸载分发版。WSL Config allows distributions to be unregistered/uninstalled.

取消注册后,还可以重新安装分发版。Unregistering also allows distributions to be reinstalled.

警告: 注销后,与该分发关联的所有数据、设置和软件都将永久丢失。Caution: Once unregistered, all data, settings, and software associated with that distribution will be permanently lost. 从 Store 重新安装会安装分发版的干净副本。Reinstalling from the store will install a clean copy of the distribution.

wsl --unregister <DistributionName>
从 WSL 中取消注册分发版,以便能够重新安装或清理它。Unregisters the distribution from WSL so it can be reinstalled or cleaned up.

例如:wsl --unregister Ubuntu 将从可用于 WSL 的分发版中删除 Ubuntu。For example: wsl --unregister Ubuntu would remove Ubuntu from the distributions available in WSL. 运行 wsl --list 时,不再会列出 Ubuntu。When I run wsl --list it will not be listed.

若要重新安装,请在 Microsoft Store 中找到该分发版,然后选择“启动”。To reinstall, find the distribution in the Microsoft store and select "Launch".

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

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

以指定用户的身份运行 WSL。Run WSL as the specified user. 请注意,该用户必须存在于 WSL 分发版中。Please note that user must exist inside of the WSL distribution.

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

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

更改分发登录的默认用户。Change the default user that for your distribution log-in. 为了成为默认用户,用户必须已在分发内存在。The user has to already exist inside the distribution in order to become the default user.

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

备注

如果在找出分发的名称时遇到问题,请参阅命令的 列表分发 以列出已安装的分发的正式名称。If you are having trouble figuring out the name of your distribution, see List distributions for the command to list the official name of the installed distributions.

运行特定的分发版Run a specific distribution

wsl -d <DistributionName>, wsl --distribution <DistributionName>wsl -d <DistributionName>, wsl --distribution <DistributionName>

运行 WSL 的指定分发版。可用于将命令发送到特定的分发版,而无需更改默认值。Run a specified distribution of WSL, can be used to send commands to a specific distribution without having to change your default.

在早期 Windows 版本中管理多个 Linux 分发Managing multiple Linux Distributions in earlier Windows versions

在 Windows 10 版本1903之前,应使用 WSL Config (wslconfig.exe) 命令行工具管理在适用于 linux 的 Windows 子系统 (WSL) 上运行的 linux 分发。In Windows 10 prior to version 1903, the WSL Config (wslconfig.exe) command-line tool should be used to manage Linux distributions running on the Windows Subsystem for Linux (WSL). 使用该工具可以列出可用分发版、设置默认分发和卸载分发版。It lets you list available distributions, set a default distribution, and uninstall distributions.

尽管 WSL Config 对于跨越或协调分发版的设置有帮助,但每个 Linux 分发版会独立管理自身的配置。While WSL Config is helpful for settings that span or coordinate distributions, each Linux distribution independently manages its own configurations. 若要查看特定于分发版的命令,请运行 [distro.exe] /?To see distribution-specific commands, run [distro.exe] /?. 例如,ubuntu /?For example ubuntu /?.

若要查看 wslconfig 的所有可用选项,请运行:wslconfig /?To see all available options for wslconfig, run: wslconfig /?

wslconfig.exe
Performs administrative operations on Windows Subsystem for Linux

Usage:
    /l, /list [/all] - Lists registered distributions.
        /all - Optionally list all distributions, including distributions that
               are currently being installed or uninstalled.
    /s, /setdefault <DistributionName> - Sets the specified distribution as the default.
    /u, /unregister <DistributionName> - Unregisters a distribution.

若要列出分发,请使用:To list distributions, use:

wslconfig /list
列出可用于 WSL 的 Linux 分发版。Lists available Linux distributions available to WSL. 如果列出了某个分发版,表示该分发版已安装且可供使用。If a distribution is listed, it's installed and ready to use.

wslconfig /list /all
列出所有分发版,包括当前不可用的分发版。Lists all distributions, including ones that aren't currently usable. 这些分发版可能正在安装、卸载或处于损坏状态。They may be in the process of installing, uninstalling, or are in a broken state.

设置在命令行上运行时运行的默认分发 wslTo set a default distribution that runs when you run wsl on a command line:

wslconfig /setdefault <DistributionName> 将默认分布设置为 <DistributionName>wslconfig /setdefault <DistributionName> Sets the default distribution to <DistributionName>.

示例:使用 PowerShell) (Example: (using PowerShell)
wslconfig /setdefault Ubuntu 会将我的默认分发版设置为 Ubuntu。wslconfig /setdefault Ubuntu would set my default distribution to Ubuntu. 现在,当我运行 wsl npm init 时,该分发版将在 Ubuntu 中运行。Now when I run wsl npm init it will run in Ubuntu. 如果我运行 wsl,它会打开 Ubuntu 会话。If I run wsl it will open an Ubuntu session.

若要注销并重新安装分发:To unregister and reinstall a distribution:

wslconfig /unregister <DistributionName>
从 WSL 中取消注册分发版,以便能够重新安装或清理它。Unregisters the distribution from WSL so it can be reinstalled or cleaned up.

例如:wslconfig /unregister Ubuntu 将从可用于 WSL 的分发版中删除 Ubuntu。For example: wslconfig /unregister Ubuntu would remove Ubuntu from the distributions available in WSL. 运行 wslconfig /list 时,不再会列出 Ubuntu。When I run wslconfig /list it will not be listed.

若要重新安装,请在 Microsoft Store 中找到该分发版,然后选择“启动”。To reinstall, find the distribution in the Microsoft store and select "Launch".

通过 wslconf 配置每个发行版启动设置Configure per distro launch settings with wslconf

在 Windows 版本17093及更高版本中可用Available in Windows Build 17093 and later

使用 wsl.conf 自动配置 WSL 中每次启动子系统时要应用的某些功能。Automatically configure certain functionality in WSL that will be applied every time you launch the subsystem using wsl.conf.

目前,这包括自动装载选项和网络配置。Right now, this includes automount options and network configuration.

wsl.conf 位于每个 Linux 分发版的 /etc/wsl.conf 中。wsl.conf is located in each Linux distribution in /etc/wsl.conf. 如果该文件不存在,你可以自行创建。If the file is not there, you can create it yourself. WSL 会检测该文件是否存在,并读取其内容。WSL will detect the existence of the file and will read its contents. 如果该文件缺失或格式不正确(即,标记格式不正确),WSL 将继续如常启动。If the file is missing or malformed (that is, improper markup formatting), WSL will continue to launch as normal.

下面是可以 wsl.conf 添加到分发的示例文件:Here is a sample wsl.conf file you could add into your distributions:

# Enable extra metadata options by default
[automount]
enabled = true
root = /windir/
options = "metadata,umask=22,fmask=11"
mountFsTab = false

# Enable DNS – even though these are turned on by default, we'll specify here just to be explicit.
[network]
generateHosts = true
generateResolvConf = true

配置选项Configuration Options

为了遵守 .ini 约定,键将在某个节下声明。In keeping with .ini conventions, keys are declared under a section.

WSL 支持两个节:automountnetworkWSL supports two sections: automount and network.

automountautomount

节:[automount]Section: [automount]

key valuevalue 默认default 备注notes
已启用enabled 布尔值boolean truetrue true 导致固定驱动器(即true causes fixed drives (i.e C:/D:/)自动装载到 DrvFs 中的 /mnt 下。C:/ or D:/) to be automatically mounted with DrvFs under /mnt. false 表示不会自动装入驱动器,但你仍然可以手动或通过手动装载它们 fstabfalse means drives won't be mounted automatically, but you could still mount them manually or via fstab.
mountFsTabmountFsTab 布尔值boolean truetrue true 设置启动 WSL 时要处理的 /etc/fstabtrue sets /etc/fstab to be processed on WSL start. /etc/fstab 是可在其中声明其他文件系统的文件,类似于 SMB 共享。/etc/fstab is a file where you can declare other filesystems, like an SMB share. 因此,在启动时,可以在 WSL 中自动装载这些文件系统。Thus, you can mount these filesystems automatically in WSL on start up.
rootroot 字符串String /mnt/ 设置固定驱动器要自动装载到的目录。Sets the directory where fixed drives will be automatically mounted. 例如,如果 WSL 中的某个目录位于 /windir/,而你将该目录指定为根目录,则固定驱动器预期会装载到 /windir/cFor example, if you have a directory in WSL at /windir/ and you specify that as the root, you would expect to see your fixed drives mounted at /windir/c
选项options 逗号分隔值列表comma-separated list of values 空字符串empty string 此值将追加到默认的 DrvFs 装载选项字符串。This value is appended to the default DrvFs mount options string. 只能指定特定于 DrvFs 的选项。Only DrvFs-specific options can be specified. 通常由装载二进制文件分析成标志的选项不受支持。Options that the mount binary would normally parse into a flag are not supported. 若要显式指定这些选项,必须在 /etc/fstab 中包含要对其执行此操作的每个驱动器。If you want to explicitly specify those options, you must include every drive for which you want to do so in /etc/fstab.

默认情况下,WSL 会将 uid 和 gid 设置为默认用户的值(在 Ubuntu 分发版中,默认用户是使用 uid=1000,gid=1000 创建的)。By default, WSL sets the uid and gid to the value of the default user (in Ubuntu distro, the default user is created with uid=1000,gid=1000). 如果用户通过此键显式指定了 gid 或 uid 选项,将覆盖关联的值。If the user specifies a gid or uid option explicitly via this key, the associated value will be overwritten. 否则,将始终追加默认值。Otherwise, the default value will always be appended.

注意: 对于所有自动装载的驱动器,这些选项将应用为装载选项。Note: These options are applied as the mount options for all automatically mounted drives. 如果只想更改特定驱动器的选项,请改用 /etc/fstab。To change the options for a specific drive only, use /etc/fstab instead.

装载选项Mount options

为 Windows 驱动器 (DrvFs) 设置不同的装载选项可以控制为 Windows 文件计算文件权限的方式。Setting different mount options for Windows drives (DrvFs) can control how file permissions are calculated for Windows files. 你可使用以下选项:The following options are available:

Key 说明Description 默认值Default
uiduid 用于所有文件的所有者的用户 IDThe User ID used for the owner of all files WSL 发行版的默认用户 ID(第一次安装时,此项默认为 1000)The default User ID of your WSL distro (On first installation this defaults to 1000)
gidgid 用于所有文件的所有者的组 IDThe Group ID used for the owner of all files WSL 发行版的默认组 ID(第一次安装时,此项默认为 1000)The default group ID of your WSL distro (On first installation this defaults to 1000)
umaskumask 要对所有文件和目录排除的权限的八进制掩码An octal mask of permissions to exclude for all files and directories 000000
fmaskfmask 要对所有文件排除的权限的八进制掩码An octal mask of permissions to exclude for all files 000000
dmaskdmask 要对所有目录排除的权限的八进制掩码An octal mask of permissions to exclude for all directories 000000
metadatametadata 是否向 Windows 文件添加了元数据以支持 Linux 系统权限Whether metadata is added to Windows files to support Linux system permissions enabledenabled

注意: 在应用到文件或目录之前,会通过逻辑或操作来完成权限掩码。Note: The permission masks are put through a logical OR operation before being applied to files or directories.

networknetwork

节标签:[network]Section label: [network]

keykey valuevalue defaultdefault 说明notes
generateHostsgenerateHosts booleanboolean true true 将 WSL 设置为生成 /etc/hoststrue sets WSL to generate /etc/hosts. hosts 文件包含主机名对应的 IP 地址的静态映射。The hosts file contains a static map of hostnames corresponding IP address.
generateResolvConfgenerateResolvConf booleanboolean true true 将 WSL 设置为生成 /etc/resolv.conftrue set WSL to generate /etc/resolv.conf. resolv.conf 包含能够将给定主机名解析为其 IP 地址的 DNS 列表。The resolv.conf contains a DNS list that are capable of resolving a given hostname to its IP address.

interopinterop

节标签:[interop]Section label: [interop]

这些选项在预览体验成员内部版本 17713 和更高版本中可用。These options are available in Insider Build 17713 and later.

keykey valuevalue defaultdefault 说明notes
enabledenabled 布尔值boolean true 设置此键可确定 WSL 是否支持启动 Windows 进程。Setting this key will determine whether WSL will support launching Windows processes.
appendWindowsPathappendWindowsPath booleanboolean true 设置此键可确定 WSL 是否会将 Windows 路径元素添加到 $PATH 环境变量。Setting this key will determine whether WSL will add Windows path elements to the $PATH environment variable.

useruser

节标签:[user]Section label: [user]

这些选项在版本18980和更高版本中可用。These options are available in Build 18980 and later.

keykey valuevalue defaultdefault 说明notes
defaultdefault stringstring 首次运行时创建的初始用户名The initial username created on first run 设置此项将指定在首次启动 WSL 会话时要运行的用户。Setting this key specifies which user to run as when first starting a WSL session.

用 wslconfig 配置全局选项。Configure global options with .wslconfig

在 Windows 版本19041及更高版本中可用Available in Windows Build 19041 and later

可以通过将文件放入用户文件夹的根目录来配置全局 WSL 选项 .wslconfigC:\Users\<yourUserName>\.wslconfigYou can configure global WSL options by placing a .wslconfig file into the root directory of your users folder: C:\Users\<yourUserName>\.wslconfig. 其中许多文件都与 WSL 2 相关,请记住,你可能需要运行 wsl --shutdown 以关闭 WSL 2 VM,然后重新启动 WSL 实例,这些更改才会生效。Many of these files are related to WSL 2, please keep in mind you may need to run wsl --shutdown to shut down the WSL 2 VM and then restart your WSL instance for these changes to take affect.

下面是 wslconfig 文件的示例:Here is a sample .wslconfig file:

[wsl2]
kernel=C:\\temp\\myCustomKernel
memory=4GB # Limits VM memory in WSL 2 to 4 GB
processors=2 # Makes the WSL 2 VM use two virtual processors

此文件可以包含以下选项:This file can contain the following options:

WSL 2 设置WSL 2 Settings

节标签:[wsl2]Section label: [wsl2]

这些设置会影响支持任何 WSL 2 分发的虚拟机。These settings affect the VM that powers any WSL 2 distribution.

keykey valuevalue defaultdefault 说明notes
内核 (kernel)kernel stringstring Microsoft 构建的内核提供的收件箱The Microsoft built kernel provided inbox 自定义 Linux 内核的绝对 Windows 路径。An absolute Windows path to a custom Linux kernel.
内存memory 大小size Windows 或8GB 上的总内存的50%,取两者中的较小者;在20175之前的版本中,在 Windows 上的内存总量为80%50% of total memory on Windows or 8GB, whichever is less; on builds before 20175: 80% of your total memory on Windows 要分配给 WSL 2 VM 的内存量。How much memory to assign to the WSL 2 VM.
processors numbernumber Windows 上的处理器数相同The same number of processors on Windows 要分配给 WSL 2 VM 的处理器数量。How many processors to assign to the WSL 2 VM.
localhostForwardinglocalhostForwarding booleanboolean true 布尔值,指定是否应通过 localhost: port 将绑定到 WSL 2 VM 中的通配符或 localhost 的端口连接到主机。Boolean specifying if ports bound to wildcard or localhost in the WSL 2 VM should be connectable from the host via localhost:port.
kernelCommandLinekernelCommandLine stringstring 空白Blank 其他内核命令行参数。Additional kernel command line arguments.
swapswap 大小size Windows 上的25% 内存大小向上舍入到最接近的 GB25% of memory size on Windows rounded up to the nearest GB 要添加到 WSL 2 VM 的交换空间量,0表示没有交换文件。How much swap space to add to the WSL 2 VM, 0 for no swap file.
交换文件swapFile stringstring %USERPROFILE%\AppData\Local\Temp\swap.vhdx%USERPROFILE%\AppData\Local\Temp\swap.vhdx 交换虚拟硬盘的绝对 Windows 路径。An absolute Windows path to the swap virtual hard disk.
  • 注意:对于 Windows 版本19041,此值为 true,并可能在预览体验计划中的 Windows 版本中有所不同Note: This value is true for Windows Build 19041 and may be different in Windows builds in the Insiders program

值为的项 path 必须是包含转义反斜杠的 Windows 路径,例如: C:\\Temp\\myCustomKernelEntries with the path value must be Windows paths with escaped backslashes, e.g: C:\\Temp\\myCustomKernel

具有值的项 size 必须是后跟一个单位的大小,例如 8GB512MBEntries with the size value must be a size followed by a unit, for example 8GB or 512MB.