排查适用于 Linux 的 Windows 子系统问题Troubleshooting Windows Subsystem for Linux

要获得 WSL 相关问题的支持,请参阅 GitHub 存储库:For support with issues related to WSL, please see our GitHub repo:

有关技术问题,请使用产品存储库: https://github.com/Microsoft/wsl/issuesFor technical issues, use the product repo: https://github.com/Microsoft/wsl/issues

对于与本文档内容相关的问题,请使用文档存储库: https://github.com/MicrosoftDocs/wsl/issuesFor issues related to the contents of this documentation, use the docs repo: https://github.com/MicrosoftDocs/wsl/issues

提交 Bug 报告Submit a bug report

对于与 WSL 函数或功能相关的 bug,请在产品存储库中创建问题: https://github.com/Microsoft/wsl/issuesFor bugs related to WSL functions or features, file an issue in the product repo: https://github.com/Microsoft/wsl/issues

提交功能请求Submit a feature request

若要请求与 WSL 功能或兼容性相关的新功能,请在产品存储库中创建问题: https://github.com/Microsoft/wsl/issuesTo request a new feature related to WSL functionality or compatibility, file an issue in the product repo: https://github.com/Microsoft/wsl/issues

参与编辑文档Contribute to the docs

若要参与撰写 WSL 文档,请在文档存储库中提交拉取请求: https://github.com/MicrosoftDocs/wsl/issuesTo contribute to the WSL documentation, submit a pull request in the docs repo: https://github.com/MicrosoftDocs/wsl/issues

终端或命令行Terminal or Command Line

最后,如果你的问题与 Windows 终端、Windows 控制台或命令行 UI 相关,请使用 Windows 终端存储库: https://github.com/microsoft/terminalLastly, if your issue is related to the Windows Terminal, Windows Console, or the command-line UI, use the Windows terminal repo: https://github.com/microsoft/terminal

常见问题Common issues

无法从 Windows 访问 WSL 文件Cannot access WSL files from Windows

9p 协议文件服务器在 Linux 端提供服务,以允许 Windows 访问 Linux 文件系统。A 9p protocol file server provides the service on the Linux side to allow Windows to access the Linux file system. 如果在 Windows 上无法使用 \\wsl$ 访问 WSL,则可能是因为 9P 无法正确启动。If you cannot access WSL using \\wsl$ on Windows, it could be because 9P did not start correctly.

要检查这一点,可以使用 dmesg |grep 9p 来检查启动日志,这将显示任何错误。To check this, you can check the start up logs using: dmesg |grep 9p, and this will show you any errors. 成功的输出如下所示:A successfull output looks like the following:

[    0.363323] 9p: Installing v9fs 9p2000 file system support
[    0.363336] FS-Cache: Netfs '9p' registered for caching
[    0.398989] 9pnet: Installing 9P2000 support

请参阅本 Github 线程,获取有关此问题的进一步讨论。Please see this Github thread for further discussion on this issue.

无法启动 WSL 2 发行版,仅在输出中看到“WSL 2”Can't start WSL 2 distro and only see 'WSL 2' in output

如果你的显示语言不是英语,则可能会看到截断的错误文本。If your display language is not English, then it is possible you are seeing a truncated version of an error text.


要解决此问题,请访问 https://aka.ms/wsl2kernel,按照该文档页面上的指示手动安装内核。To resolve this issue, please visit https://aka.ms/wsl2kernel and install the kernel manually by following the directions on that doc page.

请启用 Virtual Machine Platform Windows 功能,并确保在 BIOS 中启用了虚拟化。Please enable the Virtual Machine Platform Windows feature and ensure virtualization is enabled in the BIOS.

  1. 请查看 Hyper-V 系统要求Check the Hyper-V system requirements
  2. 如果你的计算机是 VM,请手动启用嵌套虚拟化If your machine is a VM, please enable nested virtualization manually. 使用 admin 启动 powershell,并运行:Launch powershell with admin, and run:
Set-VMProcessor -VMName <VMName> -ExposeVirtualizationExtensions $true
  1. 请按照电脑制造商提供的虚拟化启用指南进行操作。Please follow guidelines from your PC's manufacturer on how to enable virtualization. 通常,这可能涉及使用系统 BIOS 来确保在 CPU 上启用了这些功能。In general, this can involve using the system BIOS to ensure that these features are enabled on your CPU.
  2. 启用 Virtual Machine Platform 可选组件后,请重启计算机。Restart your machine after enabling the Virtual Machine Platform optional component.

Bash 在连接到 VPN 后断开网络连接Bash loses network connectivity once connected to a VPN

如果在连接到 Windows 上的 VPN 后 bash 断开网络连接,请尝试从 bash 内部解决问题。If after connecting to a VPN on Windows, bash loses network connectivity, try this workaround from within bash. 此解决方法允许通过 /etc/resolv.conf 手动替代 DNS 解析。This workaround will allow you to manually override the DNS resolution through /etc/resolv.conf.

  1. 执行 ipconfig.exe /all,记下 VPN 的 DNS 服务器Take a note of the DNS server of the VPN from doing ipconfig.exe /all
  2. 执行 sudo cp /etc/resolv.conf /etc/resolv.conf.new,创建现有 resolv.conf 的副本Make a copy of the existing resolv.conf sudo cp /etc/resolv.conf /etc/resolv.conf.new
  3. 执行 sudo unlink /etc/resolv.conf 以便与当前 resolv.conf 取消链接Unlink the current resolv.conf sudo unlink /etc/resolv.conf
  4. sudo mv /etc/resolv.conf.new /etc/resolv.conf
  5. 打开 /etc/resolv.conf 并执行以下操作Open /etc/resolv.conf and
    a.a. 删除该文件中显示了“# This file was automatically generated by WSL.Delete the first line from the file, which says "# This file was automatically generated by WSL. To stop automatic generation of this file, remove this line”的第一行。To stop automatic generation of this file, remove this line.".
    b.b. 将上面步骤 (1) 中记下的 DNS 条目添加为 DNS 服务器列表中的第一个条目。Add the DNS entry from (1) above as the very first entry in the list of DNS servers.
    c.c. 关闭该文件。Close the file.

断开 VPN 连接后,必须将更改还原为 /etc/resolv.confOnce you have disconnected the VPN, you will have to revert the changes to /etc/resolv.conf. 为此,请执行以下命令:To do this, do:

  1. cd /etc
  2. sudo mv resolv.conf resolv.conf.new
  3. sudo ln -s ../run/resolvconf/resolv.conf resolv.conf

启动 WSL 或安装分发版会返回一个错误代码Starting WSL or installing a distribution returns an error code

按照这些说明收集详细日志并在 GitHub 上提出问题。Follow these instructions to collect detailed logs and file an issue on our GitHub.

更新 Windows 上的 Ubuntu BashUpdating Bash on Ubuntu on Windows

Windows 上的 Ubuntu Bash 有两个组件需要更新。There are two components of Bash on Ubuntu on Windows that can require updating.

  1. 适用于 Linux 的 Windows 子系统The Windows Subsystem for Linux

    升级 Windows 上的 Ubuntu Bash 的此部分会启用发行说明中所述的任何新修复程序。Upgrading this portion of Bash on Ubuntu on Windows will enable any new fixes outlines in the release notes. 确保已订阅 Windows 预览体验计划,并且内部版本是最新的。Ensure that you are subscribed to the Windows Insider Program and that your build is up to date. 若要获得更精细的控制,包括重置 Ubuntu 实例,请查看命令参考页For finer grain control including resetting your Ubuntu instance check out the command reference page.

  2. Ubuntu 用户二进制文件The Ubuntu user binaries

    升级 Windows 上的 Ubuntu Bash 的此部分会安装 Ubuntu 用户二进制文件(包括通过 apt-get 安装的应用程序)的任何更新。Upgrading this portion of Bash on Ubuntu on Windows will install any updates to the Ubuntu user binaries including applications that you have installed via apt-get. 若要更新,请在 Bash 中运行以下命令:To update run the following commands in Bash:

    1. apt-get update
    2. apt-get upgrade

Apt-get upgrade 错误Apt-get upgrade errors

某些包使用我们尚未实现的功能。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

Windows 更新后出现“错误:0x80040306”"Error: 0x80040306" on installation

此错误与我们不支持旧版控制台这一事实有关。This has to do with the fact that we do not support legacy console. 若要关闭旧版控制台:To turn off legacy console:

  1. 打开 cmd.exeOpen cmd.exe
  2. 右键单击标题栏 -> 选择“属性”-> 取消选中“使用旧版控制台”Right click title bar -> Properties -> Uncheck Use legacy console
  3. 单击“确定”Click OK

Windows 更新后出现“错误:0x80040154”"Error: 0x80040154" after Windows update

在 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 can be found in the Installation Guide.

更改显示语言Changing the display language

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

Windows 系统还原后出现的安装问题Installation issues after Windows system restore

  1. 删除 %windir%\System32\Tasks\Microsoft\Windows\Windows Subsystem for Linux 文件夹。Delete the %windir%\System32\Tasks\Microsoft\Windows\Windows Subsystem for Linux folder.
    注意:如果可选功能已完全安装且工作正常,请不要执行此操作。Note: Do not do this if your optional feature is fully installed and working.
  2. 启用 WSL 可选功能(如果尚未这样做)Enable the WSL optional feature (if not already)
  3. 重新启动Reboot
  4. lxrun /uninstall /fulllxrun /uninstall /full
  5. 安装 bashInstall bash

在 WSL 中无法进行 Internet 访问No internet access in 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.

使用 ping 时出现“权限被拒绝”错误Permission Denied error when using ping

对于 Windows 周年更新版本 1607,在 WSL 中运行 ping 命令需要拥有 Windows 中的管理员特权For Windows Anniversary Update, version 1607, administrator privileges in Windows are required to run ping in WSL. 若要运行 ping,请以管理员身份运行 Windows 上的 Ubuntu Bash,或使用管理员特权从 CMD/PowerShell 提示符运行 bash.exe。To run ping, run Bash on Ubuntu on Windows as an administrator, or run bash.exe from a CMD/PowerShell prompt with administrator privileges.

对于更高版本的 Windows(内部版本 14926 及更高版本),则不再需要管理员特权。For later versions of Windows, Build 14926+, administrator privileges are no longer required.

Bash 挂起Bash is hung

如果使用 bash 时发现 bash 挂起(或死锁)且不响应输入,请收集并报告内存转储来帮助我们诊断问题。If while working with bash, you find that bash is hung (or deadlocked) and not responding to inputs, help us diagnose the issue by collecting and reporting a memory dump. 请注意,这些步骤会导致系统崩溃。Note that these steps will crash your system. 如果你不熟悉此过程,请不要这样做,或者,请在执行此操作之前保存你的工作。Do not do this if you are not comfortable with that or save your work prior to doing this.

收集内存转储To collect a memory dump

  1. 将内存转储类型更改为“完整内存转储”。Change the memory dump type to "complete memory dump". 更改转储类型时,请记下当前类型。While changing the dump type, take a note of your current type.

  2. 遵循这些步骤使用键盘控制来配置崩溃。Use the steps to configure crash using keyboard control.

  3. 再现挂起或死锁场景。Repro the hang or deadlock.

  4. 使用步骤 (2) 中的按键顺序来使系统崩溃。Crash the system using the key sequence from (2).

  5. 系统将会崩溃并收集内存转储。The system will crash and collect the memory dump.

  6. 系统重新启动后,会将 memory.dmp 报告给 secure@microsoft.com。Once the system reboots, report the memory.dmp to secure@microsoft.com. 转储文件的默认位置是 %SystemRoot%\memory.dmp 或 C:\Windows\memory.dmp(如果 C: 是系统驱动器)。The default location of the dump file is %SystemRoot%\memory.dmp or C:\Windows\memory.dmp if C: is the system drive. 请注意,在电子邮件中,转储供 WSL 或 Windows 上的 Bash 团队参考。In the email, note that the dump is for the WSL or Bash on Windows team.

  7. 将内存转储类型还原为原始设置。Restore the memory dump type to the original setting.

检查内部版本号Check your build number

若要查找电脑的体系结构和 Windows 内部版本号,请打开To find your PC's architecture and Windows build number, open
“设置” > “系统” > “关于” Settings > System > About

查看“OS 内部版本”和“系统类型”字段。 Look for the OS Build and System Type fields.
“内部版本”和“系统类型”字段的屏幕截图Screenshot of Build and System Type fields

若要查找 Windows Server 内部版本号,请在 PowerShell 中运行以下命令:To find your Windows Server build number, run the following in PowerShell:

systeminfo | Select-String "^OS Name","^OS Version"

确认已启用 WSLConfirm WSL is enabled

可以通过在 PowerShell 中运行以下命令来确认是否已启用“适用于 Linux 的 Windows 子系统”:You can confirm that the Windows Subsystem for Linux is enabled by running the following in PowerShell:

Get-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

OpenSSH 服务器连接问题OpenSSH-Server connection issues

尝试连接 SSH 服务器失败并出现以下错误:“ 端口 22 已关闭连接”。Trying to connect your SSH server is failed with the following error: "Connection closed by port 22".

  1. 确保 OpenSSH 服务器正在运行:Make sure your OpenSSH Server is running:

    sudo service ssh status

    并已按照此教程进行操作: https://help.ubuntu.com/lts/serverguide/openssh-server.html.enand you've followed this tutorial: https://help.ubuntu.com/lts/serverguide/openssh-server.html.en

  2. 停止 sshd 服务,然后在调试模式下启动 sshd:Stop the sshd service and start sshd in debug mode:

    sudo service ssh stop
    sudo /usr/sbin/sshd -d
  3. 检查启动日志,确保已提供主机密钥,并且未看到如下所示的日志消息:Check the startup logs and make sure HostKeys are available and you don't see log messages such as:

    debug1: sshd version OpenSSH_7.2, OpenSSL 1.0.2g  1 Mar 2016
    debug1: key_load_private: incorrect passphrase supplied to decrypt private key
    debug1: key_load_public: No such file or directory
    Could not load host key: /etc/ssh/ssh_host_rsa_key
    debug1: key_load_private: No such file or directory
    debug1: key_load_public: No such file or directory
    Could not load host key: /etc/ssh/ssh_host_dsa_key
    debug1: key_load_private: No such file or directory
    debug1: key_load_public: No such file or directory
    Could not load host key: /etc/ssh/ssh_host_ecdsa_key
    debug1: key_load_private: No such file or directory
    debug1: key_load_public: No such file or directory
    Could not load host key: /etc/ssh/ssh_host_ed25519_key

如果确实看到了此类消息,并且 /etc/ssh/ 下缺少主机密钥,则必须重新生成这些密钥,或者只是清除并安装 OpenSSH 服务器:If you do see such messages and the keys are missing under /etc/ssh/, you will have to regenerate the keys or just purge&install openssh-server:

sudo apt-get purge openssh-server
sudo apt-get install openssh-server

“找不到引用的程序集。”"The referenced assembly could not be found." 在启用 WSL 可选功能后出现when enabling the WSL optional feature

此错误与处于错误的安装状态相关。This error is related to being in a bad install state. 请完成以下步骤来尝试解决此问题:Please complete the following steps to try and fix this issue:

  • 如果是从 PowerShell 运行了启用 WSL 功能的命令,请尝试改用 GUI,方法是打开“开始”菜单,搜索“启用或关闭 Windows 功能”,然后在列表中选择“适用于 Linux 的 Windows 子系统”,这将安装可选组件。If you are running the enable WSL feature command from PowerShell, try using the GUI instead by opening the start menu, searching for 'Turn Windows features on or off' and then in the list select 'Windows Subsystem for Linux' which will install the optional component.

  • 转到“设置”、“更新”,然后单击“检查更新”来更新 Windows 版本Update your version of Windows by going to Settings, Updates, and clicking 'Check for Updates'

  • 如果这两个操作均失败,并且你需要访问 WSL,请考虑使用安装介质重新安装 Windows 10 并选择“保留所有内容”以确保保留你的应用和文件,从而就地升级。If both of those fail and you need to access WSL please consider upgrading in place by reinstalling Windows 10 using installation media and selecting 'Keep Everything' to ensure your apps and files are preserved. 可以在“重新安装 Windows 10”页面中找到有关如何执行此操作的说明。You can find instructions on how to do so at the Reinstall Windows 10 page.

如果看到此错误:If you're seeing this error:

Permissions 0777 for '/home/artur/.ssh/private-key.pem' are too open.

若要解决此问题,请将以下内容追加到 /etc/wsl.conf 文件:To fix this, append the following to the the /etc/wsl.conf file:

enabled = true
options = metadata,uid=1000,gid=1000,umask=0022

请注意,添加此命令将包含元数据并修改有关从 WSL 中看到的 Windows 文件的文件权限。Please note that adding this command will include metadata and modify the file permissions on the Windows files seen from WSL. 有关详细信息,请参阅文件系统权限Please see the File System Permissions for more information.