管理和配置适用于 Linux 的 Windows 子系统Manage and configure Windows Subsystem for Linux

适用于 Windows 10 Fall Creators Update 和更高版本。Applies to Windows 10 Fall Creators Update and later. 若要试用新的管理功能并开始从 Microsoft Store 运行多个 Linux 分发版,请参阅更新的安装指南See our updated installation guide to try new management features and start running multiple Linux distributions from the Microsoft store.

WSL 的运行方式Ways to run WSL

可通过多种方式配合适用于 Linux 的 Windows 子系统运行 Linux。There are many ways to run Linux with the Windows Subsystem for Linux.

  1. [distro],例如 ubuntu[distro], for example ubuntu
  2. wsl.exebash.exewsl.exe or bash.exe
  3. wsl [command]bash -c [command]wsl [command] or bash -c [command]

要使用的方法取决于执行的操作。Which method you should use depends on what you're doing.

按分发版启动 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.

示例:Example:

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.

示例:Example:

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.

示例:Example:

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:56:57 DST 2018
scooley@scooley-elmer:/mnt/c/Users/sarah$ exit
logout

PS C:\Users\sarah> wsl date
Sun Mar 11 19:55:47 DST 2018

示例:Example:

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 和更高版本Windows 10 Version 1903 and later

可以使用 wsl.exe 管理适用于 Linux 的 Windows 子系统 (WSL) 中的分发版,包括列出可用分发版、设置默认分发版,以及卸载分发版。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 -lwsl --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
列出所有分发版,包括当前不可用的分发版。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
列出当前正在运行的所有分发版。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>.

示例:Example:
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.

运行特定的分发版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 10 版本 1903 的版本Versions Earlier than Windows 10 Version 1903

WSL Config (wslconfig.exe) 是一个命令行工具,用于管理在适用于 Linux 的 Windows 子系统 (WSL) 上运行的 Linux 分发版。WSL Config (wslconfig.exe) is a command-line tool for managing 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.

列出分发版List distributions

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.

设置默认分发版Set a default distribution

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

wslconfig /setdefault <DistributionName>

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

示例:Example:
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.

取消注册和重新安装分发版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.

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".

设置 WSL 启动设置Set WSL launch settings

在 Windows 预览体验成员内部版本 17093 和更高版本中可用Available in Windows Insider 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 distros:

# 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 defaultdefault 备注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 表示驱动器不会自动装载,但你仍可以手动或通过 fstab 装载驱动器。false 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

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

网络network

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

key valuevalue defaultdefault 备注notes
generateHostsgenerateHosts 布尔值boolean 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 布尔值boolean 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.

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