有关适用于 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 驱动器的屏幕截图

你能否描述一下整合了 WSL 的典型开发工作流?Could you describe a typical development workflow that incorporates WSL?

WSL 面向开发人员受众,旨在用作内部开发流程的一部分。WSL targets a developer audience with the intent to be used as part of an inner development loop. 假设何石要创建一个 CI/CD 管道(持续集成和持续交付),而且他想先在本地计算机(笔记本电脑)上测试它,然后再将它部署到云中。Let's say that Sam is creating a CI/CD pipeline (Continuous Integration & Continuous Delivery) and wants to test it first on a local machine (laptop) before deploying it to the cloud. 何石可启用 WSL(和 WSL 2 来提高速度和性能),然后在本地(在笔记本电脑上)将正版 Linux Ubuntu 实例与所需的任何 Bash 命令和功能搭配使用。Sam can enable WSL (& WSL 2 to improve speed and performance), and then use a genuine Linux Ubuntu instance locally (on the laptop) with whatever Bash commands and tools they prefer. 在本地验证开发管道后,何石可将该 CI/CD 管道向上推送到云中(也就是 Azure 中),方法是将它放入 Docker 容器并将该容器推送到云实例,使其在生产就绪的 Ubuntu VM 上运行。Once the development pipeline is verified locally, Sam can then push that CI/CD pipeline up to the cloud (ie Azure) by making it into a Docker container and pushing the container to a cloud instance where it runs on a production-ready Ubuntu VM.

什么是 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/<drive letter>//mnt/<drive letter>/

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

如何设置 Git 凭据管理器?How do I set up Git Credential Manager? (如何在 WSL 中使用我的 Windows Git 权限?)(How do I use my Windows Git permissions in WSL?)

使用 Git 凭据管理器可以对远程 Git 服务器进行身份验证,即使你采用了 Azure Active Directory 或双因素身份验证等复杂身份验证模式。Git Credential Manager enables you to authenticate a remote Git server, even if you have a complex authentication pattern like Azure Active Directory or two-factor authentication. Git 凭据管理器集成在 GitHub 等服务的身份验证流程中,并且在你通过托管提供程序身份验证后就会请求新的身份验证令牌。Git Credential Manager integrates into the authentication flow for services like GitHub and, once you're authenticated to your hosting provider, requests a new authentication token. 然后,它会将令牌安全地存储在 Windows 凭据管理器中。It then stores the token securely in the Windows Credential Manager. 首次之后,可以使用 git 与托管提供程序通信,而无需重新进行身份验证。After the first time, you can use git to talk to your hosting provider without needing to re-authenticate. 它将只需访问 Windows 凭据管理器中的令牌。It will just access the token in the Windows Credential Manager.

若要设置 Git 凭据管理器以便与 WSL 分发版配合使用,请打开分发版,然后输入以下命令:To set up Git Credential Manager for use with a WSL distribution, open your distribution and enter this command:

git config --global credential.helper "/mnt/c/Program\ Files/Git/mingw64/libexec/git-core/git-credential-manager.exe"

现在,在 WSL 分发版中执行的任何 git 操作都将使用凭据管理器。Now any git operation you perform within your WSL distribution will use the credential manager. 如果已为主机缓存凭据,那么它会从凭据管理器访问这些凭据。If you already have credentials cached for a host, it will access them from the credential manager. 如果尚未缓存凭据,你将收到一个请求凭据的对话响应,即使你处于 Linux 控制台中也是如此。If not, you'll receive a dialog response requesting your credentials, even if you're in a Linux console.

此支持依赖于适用于 Linux 的 Windows 子系统与 Windows 本身之间的互操作性This support relies on the interoperability between Windows Subsystem for Linux and Windows itself.

如何在 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.

    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
  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, or move them from one drive to another?

Windows 版本 1809 和更高版本中提供了备份或移动分发版的最佳方式:执行 export/import 命令。The best way to backup or move your distros is via the export/import commands 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 分发版的状态或者移动 WSL 分发版。You can then import this distro back into WSL using the wsl --import command, which can name a new drive location for the import, allowing you to backup and save states of (or move) 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.

如果有技术问题,或者想要请求新功能,请转到 Github 问题跟踪器:If you have technical issues, or want to request new features please go to our Github issue tracker:

如果想要随时了解最新的 WSL 新闻,可以访问:If you'd like to stay up to date with the latest WSL news you can do so with: