有关适用于 Linux 的 Windows 子系统的常见问题解答Frequently Asked Questions about Windows Subsystem for Linux

什么是适用于 Linux 的 Windows 子系统 (WSL)?What is Windows Subsystem for Linux (WSL)?

适用于 Linux 的 Windows 子系统 (WSL) 是新增的 Windows 10 功能,使用它可以直接在 Windows 上运行本机 Linux 命令行工具,并可以运行传统的 Windows 桌面和新式 Store 应用。The Windows Subsystem for Linux (WSL) is a new Windows 10 feature that enables you to run native Linux command-line tools directly on Windows, alongside your traditional Windows desktop and modern store apps.

请参阅“关于”页了解更多详细信息。See the about page for more details.

WSL 面向哪些用户?Who is WSL for?

WSL 是主要面向开发人员的工具 -- 尤其是 Web 开发人员,以及处理和使用开源项目的开发人员。This is primarily a tool for developers -- especially web developers and those who work on or with open source projects. 想要/需要使用 Bash、常用 Linux 工具(sedawk 等)和许多 Linux 优先工具(Ruby、Python 等)的用户可以通过 WSL 在 Windows 上使用其工具链。This allows those who want/need to use Bash, common Linux tools (sed, awk, etc.) and many Linux-first tools (Ruby, Python, etc.) to use their toolchain on Windows.

WSL 有哪些作用?What can I do with WSL?

WSL 提供一个名为 Bash.exe 的应用程序,启动该应用程序后,会打开一个运行 Bash shell 的 Windows 控制台。WSL provides an application called Bash.exe that, when started, opens a Windows console running the Bash shell. 使用 Bash 可以运行命令行 Linux 工具和应用。Using Bash, you can run command-line Linux tools and apps. 例如,键入 lsb_release -a 并按 Enter 后,将会看到当前正在运行的 Linux 分发版的详细信息:For example, type lsb_release -a and hit enter; you’ll see details of the Linux distro currently running:

分发版详细信息的屏幕截图

还可以从 Linux Bash shell 内部访问本地计算机的文件系统 – 将会看到 /mnt 文件夹下装载的本地驱动器。You can also access your local machine’s filesystem from within the Linux Bash shell – you’ll find your local drives mounted under the /mnt folder. 例如,你的 C: 驱动器装载在 /mnt/c 下:For example, your C: drive is mounted under /mnt/c:

装载的 C 驱动器的屏幕截图

什么是 Bash?What is Bash?

Bash 是一个流行的基于文本的 shell,并且是一种命令语言。Bash is a popular text-based shell and command-language. 它是包含在 Ubuntu、其他 Linux 分发版和 macOS 中的默认 shell。It is the default shell included within Ubuntu and other Linux distros, and in macOS. 用户在 shell 中键入命令,即可执行脚本和/或运行命令与工具来完成许多任务。Users type commands into a shell to execute scripts and/or run commands and tools to accomplish many tasks.

WSL 的工作原理是怎样的?How does this work?

请查看我们的博客,其中详细介绍了底层技术。Check out our blog where we go into detail about the underlying technology.

在 VM 中为何要使用 WSL 而不是 Linux?Why would I use WSL rather than Linux in a VM?

WSL 所需的资源(CPU、内存和存储)少于完整虚拟机所需的资源。WSL requires fewer resources (CPU, memory, and storage) than a full virtual machine. WSL 还允许结合 Windows 命令行、桌面和 Store 应用运行 Linux 命令行工具与应用,并允许从 Linux 内部访问 Windows 文件。WSL also allows you to run Linux command-line tools and apps alongside your Windows command-line, desktop and store apps, and to access your Windows files from within Linux. 这样,你便可以根据需要针对相同的文件集使用 Windows 应用和 Linux 命令行工具。This enables you to use Windows apps and Linux command-line tools on the same set of files if you wish.

举例而言,我为何要在 Linux(而不是 Windows)上使用 Ruby?Why would I use, for example, Ruby on Linux instead of on Windows?

生成某些跨平台工具时,已假设其运行环境的行为类似于 Linux。Some cross-platform tools were built assuming that the environment in which they run behaves like Linux. 例如,某些工具假设它们能够访问很长的文件路径,或者特定的文件/文件夹存在。For example, some tools assume that they are able to access very long file paths or that specific files/folders exist. 这通常会在 Windows 上导致出现问题,因为 Windows 的行为通常与 Linux 不同。This often causes problems on Windows which often behaves differently from Linux.

许多语言(例如 Ruby 和 Node)通常已移植到 Windows,并且可以在 Windows 上非常顺利地运行。Many languages like Ruby and node are often ported to, and run great, on Windows. 但是,并非所有 Ruby Gem 或 Node/NPM 库所有者都会移植其库来支持 Windows,而许多库都与 Linux 之间存在特定的依赖关系。However, not all of the Ruby Gem or node/NPM library owners port their libraries to support Windows, and many have Linux-specific dependencies. 这经常导致使用此类工具和库生成的系统在 Windows 上遭遇到生成错误(有时是运行时错误),或者出现不需要的行为。This can often result in systems built using such tools and libraries suffering from build and sometimes runtime errors or unwanted behaviors on Windows.

这只是导致许多人要求 Microsoft 改进 Windows 命令行工具的一部分问题,也正是这些问题促使我们与 Canonical 展开合作,使得本机 Bash 和 Linux 命令行工具能够在 Windows 上运行。These are just some of issues that caused many people to ask Microsoft to improve Windows’ command-line tools and what drove us to partner with Canonical to enable native Bash and Linux command-line tools to run on Windows.

这对于 PowerShell 而言意味着什么?What does this mean for PowerShell?

处理 OSS 项目时,在很多情况下,从 PowerShell 提示符切换到 Bash 极其有用。While working with OSS projects, there are numerous scenarios where it’s immensely useful to drop into Bash from a PowerShell prompt. Bash 支持是互补性的,可以增强 Windows 上的命令行的价值,使 PowerShell 和 PowerShell 社区能够利用其他流行技术。Bash support is complementary and strengthens the value of the command-line on Windows, allowing PowerShell and the PowerShell community to leverage other popular technologies.

请参阅 PowerShell 团队博客了解详细信息 -- Bash for Windows:Why it’s awesome and what it means for PowerShell(为何它如此出色,它对 PowerShell 而言意味着什么)Read more on the PowerShell team blog -- Bash for Windows: Why it’s awesome and what it means for PowerShell

在 WSL 中是否可以运行所有 Linux 应用?Can I run ALL Linux apps in WSL?

不是!No! WSL 工具的目的是使用户能够视需要在 Windows 上运行 Bash 和核心 Linux 命令行工具。WSL is a tool aimed at enabling users who need them to run Bash and core Linux command-line tools on Windows.

WSL 并旨在支持 GUI 桌面或应用程序(例如 Gnome、KDE 等)。WSL does not aim to support GUI desktops or applications (e.g. Gnome, KDE, etc.)

此外,尽管你可以运行许多流行的服务器应用程序(例如 Redis),但我们不建议使用 WSL 来托管生产服务 – Microsoft 提供多种解决方案用于在 Azure、Hyper-V 和 Docker 中运行生产 Linux 工作负荷。Also, even though you will be able to run many popular server applications (e.g. Redis), we do not recommend WSL for hosting production services – Microsoft offers a variety of solutions for running production Linux workloads in Azure, Hyper-V, and Docker.

WSL 包含在哪些 Windows SKU 中?What Windows SKUs is WSL included in?

适用于 Windows 10 周年更新和创意者更新或更高版本的 Windows 桌面版中提供了适用于 Linux 的 Windows 子系统。Windows Subsystem for Linux is available on the desktop version of Windows for Windows 10 Anniversary and Creators update or later.

从 Fall Creators Update 开始,WSL 将在 Windows 桌面版和服务器版 SKU 中提供。Beginning in the Fall Creators update WSL will be available on both the desktop and server SKUs of Windows.

WSL 支持哪些处理器?What processors does WSL support?

WSL 支持 x64 和 ARM CPU。WSL supports x64 and ARM CPUs.

如何访问我的 C: 驱动器?How do I access my C: drive?

系统会自动为本地计算机上的硬盘驱动器创建装入点,通过这些装入点可以轻松访问 Windows 文件系统。Mount points for hard drives on the local machine are automatically created and provide easy access to the Windows filesystem.

/mnt/<驱动器号>//mnt/<drive letter>/

示例用法:运行 cd /mnt/c 访问 c:Example usage would be cd /mnt/c to access c:\

如何在 Linux 应用中使用 Windows 文件?How do I use a Windows file with a Linux app?

WSL 的优势之一是可以通过 Windows 和 Linux 应用或工具访问文件。One of the benefits of WSL is being able to access your files via both Windows and Linux apps or tools.

WSL 将计算机的固定驱动器装载到 Linux 分发版中的 /mnt/<drive> 文件夹下。WSL mounts your machine's fixed drives under the /mnt/<drive> folder in your Linux distros. 例如,你的 C: 驱动器装载在 /mnt/c/For example, your C: drive is mounted under /mnt/c/

例如,使用装载的驱动器,可以使用 Visual StudioVS Code 编辑 C:\dev\myproj\ 中的代码,并通过 /mnt/c/dev/myproj 访问相同的文件,在 Linux 中生成/测试该代码。Using your mounted drives, you can edit code in, for example, C:\dev\myproj\ using Visual Studio / or VS Code, and build/test that code in Linux by accessing the same files via /mnt/c/dev/myproj.

重要说明:使用 WSL 的一个重要限制是,不支持使用 Windows 应用或工具直接访问/更改 Linux 分发版文件系统中的文件。IMPORTANT NOTE: One of the key limitations of using WSL is that directly accessing/changing files in your Linux distros' filesystem using Windows apps or tools is not supported. 请参阅:不要使用 Windows 应用和工具更改 Linux 文件See: Do not change Linux files using Windows apps and tools

Linux 驱动器中的文件是否不同于装载的 Windows 驱动器中的文件?Are files in the Linux drive different from the mounted Windows drive?

  1. Linux 根目录(即 /)下的文件由模拟 Linux 特定行为的 WSL 控制,控制的内容和操作包括但不限于:Files under the Linux root (i.e. /) are controlled by WSL which mimics Linux specific behavior, including but not limited to:

    • 包含无效 Windows 文件名字符的文件Files which contain invalid Windows filename characters
    • 为非管理员用户创建的符号链接Symlinks created for non-admin users
    • 通过 chmod 和 chown 更改文件属性Changing file attributes through chmod and chown
    • 文件/文件夹的区分大小写状态File/folder case sensitivity
  2. 装载的驱动器中的文件由 Windows 控制,并具有以下行为:Files in mounted drives are controlled by Windows and have the following behaviors:

    • 支持区分大小写Support case sensitivity
    • 设置的所有权限可以最好地反映 Windows 权限All permissions are set to best reflect the Windows permissions

运行 apt-get upgrade 时为何会出现大量的错误?Why are there so many errors when I run apt-get upgrade?

某些包使用我们尚未实现的功能。Some packages use features that we haven't implemented yet. 例如,udev 尚不受支持,会导致多个 apt-get upgrade 错误。udev, for example, isn't supported yet and causes several apt-get upgrade errors.

若要解决与 udev 相关的问题,请执行以下步骤:To fix issues related to udev, follow the following steps:

  1. 将以下内容写入 /usr/sbin/policy-rc.d 并保存更改。Write the following to /usr/sbin/policy-rc.d and save your changes.

    #!/bin/sh
    exit 101
    
  2. 将执行权限添加到 /usr/sbin/policy-rc.dAdd execute permissions to /usr/sbin/policy-rc.d

    chmod +x /usr/sbin/policy-rc.d
    
  3. 运行以下命令Run the following commands

    dpkg-divert --local --rename --add /sbin/initctl
    ln -s /bin/true /sbin/initctl
    

如何卸载 WSL 分发版?How do I uninstall a WSL Distribution?

在 1709 (16299) 以前的内部版本中,打开命令提示符并运行:On builds prior to 1709 (16299) open a command prompt and run:

lxrun /uninstall /full

可以像卸载任何其他 Windows 应用一样来卸载从 Store 安装的 WSL 分发版:右键单击应用磁贴并单击“卸载”,或者通过 PowerShell 使用 Remove-AppxPackage cmdletWSL distributions installed from the store can be uninstalled like any other Windows app, by right-clicking on the app tile and clicking Uninstall, or via PowerShell using the Remove-AppxPackage cmdlet.

ping 命令为何生成权限被拒绝错误?Why does ping generate permission denied errors?

在低于 14926 的 WSL 内部版本中,ping 要求通过权限提升的控制台运行 WSL。In WSL builds < 14926, ping required WSL to run via an elevated Console. 此问题已在内部版本 14926 和更高版本中得到解决。This issue was fixed in Build 14926 and later.

如何运行 OpenSSH 服务器?How do I run an OpenSSH server?

在 WSL 中运行 OpenSSH 需要拥有 Windows 中的管理员特权。Administrator privileges in Windows are required to run OpenSSH in WSL. 若要运行 OpenSSH 服务器,请以管理员身份运行 Windows 上的 Ubuntu Bash,或使用管理员特权从 CMD/PowerShell 提示符运行 bash.exe。To run an OpenSSH server, run Bash on Ubuntu on Windows as an administrator, or run bash.exe from a CMD/PowerShell prompt with administrator privileges.

尝试安装时,为何会出现“错误:0x80040306”?Why do I get "Error: 0x80040306" when I try to install?

WSL 不支持在旧版控制台中运行。WSL does not support running in a legacy console. 若要关闭旧版控制台:To turn off legacy console:

  1. 打开 WSL、PowerShell 或 CmdOpen WSL, PowerShell, or Cmd
  2. 右键单击标题栏 -> 选择“属性”-> 取消选中“使用旧版控制台”Right click title bar -> Properties -> Uncheck "Use legacy console"
  3. 单击“确定”Click OK

在升级 Windows 后运行 bash.exe 时,为何会出现“错误:0x80040154”?Why do I get "Error: 0x80040154" when I run bash.exe after upgrading Windows?

在 Windows 更新期间可能禁用了“适用于 Linux 的 Windows 子系统”功能。The "Windows Subsystem for Linux" feature may be disabled during a Windows update. 如果出现这种情况,则必须重新启用 Windows 功能。If this happens the Windows feature must be re-enabled. 安装指南中可以找到有关启用“适用于 Linux 的 Windows 子系统”功能的说明。Instructions for enabling the "Windows Subsystem for Linux" feature can be found in the Installation Guide.

如何更改 WSL 的显示语言?How do I change the display language of WSL?

WSL 安装会尝试自动更改 Ubuntu 区域设置,使之与 Windows 安装的区域设置相匹配。WSL install will try to automatically change the Ubuntu locale to match the locale of your Windows install. 如果你不希望出现此行为,可以在安装完成后,运行此命令来更改 Ubuntu 区域设置。If you do not want this behavior you can run this command to change the Ubuntu locale after install completes. 必须重新启动 bash.exe 才能使此项更改生效。You will have to relaunch bash.exe for this change to take effect.

以下示例将区域设置更改为 en-US:The below example changes to locale to en-US:

sudo update-locale LANG=en_US.UTF8

为何无法从 WSL 进行 Internet 访问?Why do I not have internet access from WSL?

某些用户已报告特定的防火墙应用程序会阻止 WSL 中的 Internet 访问的问题。Some users have reported issues with specific firewall applications blocking internet access in WSL. 报告的防火墙包括:The firewalls reported are:

  1. KasperskyKaspersky
  2. AVGAVG
  3. AvastAvast

在某些情况下,关闭防火墙即可进行访问。In some cases turning off the firewall allows for access. 在某些情况下,只需让安装的防火墙在表面上阻止访问。In some cases simply having the firewall installed looks to block access.

如何从 Windows 中的 WSL 访问某个端口?How do I access a port from WSL in Windows?

WSL 共享 Windows 的 IP 地址,因为它在 Windows 上运行。WSL shares the IP address of Windows, as it is running on Windows. 因此,你可以访问 localhost 上的任何端口。例如,如果你在端口 1234 上提供 Web 内容,可以在 Windows 浏览器中输入 https://localhost:1234 。As such you can access any ports on localhost e.g. if you had web content on port 1234 you could https://localhost:1234 into your Windows browser.

如何备份 WSL 分发版?How can I back up my WSL distros?

Windows 版本 1809 和更高版本中提供了备份分发版的最佳方式。The best way to backup your distros is available in Windows Version 1809 and later. 可以使用 wsl --export 命令将整个分发版导出到 tarball。You can export your entire distribution to a tarball using the wsl --export command. 然后,可以使用 wsl --import 命令将此分发版导入回到 WSL,从而可以备份和保存 WSL 分发版的状态。You can then import this distro back into WSL using the wsl --import command, allowing you to backup and save states of your WSL distributions.

请注意,用于备份 Appdata 文件夹中的文件的传统备份服务(例如 Windows 备份)不会损坏 Linux 文件。Please note that traditional backup services that backup files in your Appdata folders (like Windows Backup) will not corrupt your Linux files.

可以在何处提供反馈?Where can I provide feedback?

可以通过多个渠道分享反馈和提问:反馈和问题将定向到:You can share feedback and ask questions through multiple channels: Feedback and questions should be directed to: