有关适用于 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 桌面和新式应用程序。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?

这主要是一种面向开发人员的工具, 尤其是 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 等) 在 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 的应用程序, 该应用程序在启动时将打开运行 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.

这是如何实现的?How does this work?

请查看我们的博客, 了解基础技术的详细信息。Check out our blog where we go into detail about the underlying technology.

为什么使用 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 命令行、桌面和应用商店应用程序运行 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 出现问题, 这些问题通常与 Linux 的行为不同。This often causes problems on Windows which often behaves differently from Linux.

许多语言 (例如 Ruby 和 node) 通常在 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 命令行工具的一些问题, 并使我们与合作伙伴合作, 使本机 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:为什么它非常出色, 它对 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.

从秋季创意者更新开始, 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 filesystem 的轻松访问。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:\

如何实现将 Windows 文件用于 Linux 应用?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/

使用已装载的驱动器, 你可以在中编辑代码 (例如C:\dev\myproj\ , 使用Visual Studio /或VS Code), 并/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 的一个重要限制是, 不支持直接访问/更改 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 根下的文件 (即/) 由 WSL 控制, 它模拟 Linux 特定的行为, 包括但不限于: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 时出现如此多的错误-获取升级?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.

    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

通过右键单击 "应用" 磁贴并单击 "卸载", 或使用 Remove-AppxPackage cmdlet通过 PowerShell, 可以像任何其他 Windows 应用一样卸载从应用商店安装的 WSL 分发。WSL 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?

在 WSL 生成 < 14926 中, 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?

Windows 中的管理员权限需要在 WSL 中运行 OpenSSH。Administrator privileges in Windows are required to run OpenSSH in WSL. 若要运行 OpenSSH 服务器, 请在 Windows 上的 Ubuntu 上以管理员身份运行 Bash, 或从具有管理员权限的 CMD/PowerShell 提示符运行 bash。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 后, 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 子系统" 功能的说明, 请参阅[安装指南](https://msdn.microsoft.com/en-us/commandline/wsl/install_guide#enable-the-windows-subsystem-for-linux-feature-gui https://msdn.microsoft.com/en-us/commandline/wsl/install_guide#enable-the-windows-subsystem-for-linux-feature-gui)。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。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. Kaspersky 等Kaspersky
  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 上的任何端口 (例如, 如果你的 web 内容位于端口 1234 https://localhost:1234 上, 你可以进入 Windows 浏览器中)。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: