針對適用於 Linux 的 Windows 子系統進行疑難排解Troubleshooting Windows Subsystem for Linux

如需 WSL 相關問題的支援,請參閱我們的 GitHub 存放庫: https://github.com/Microsoft/wsl/issuesFor support with issues related to WSL, please see our GitHub repo: https://github.com/Microsoft/wsl/issues

對於技術問題,請使用產品存放庫: 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

提交錯誤報告Submit a bug report

對於與 WSL 功能相關的錯誤,請在產品存放庫中提出問題: 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 10 1903 版,但還是沒有看到 WSL 2 的選項。I'm on Windows 10 version 1903 and I still do not see options for WSL 2.

這很可能是因為您的電腦尚未取得 WSL 2 的反向修補。This is likely because your machine has not yet taken the backport for WSL 2. 若要解決此問題,最簡單的方式是前往 [Windows 設定],然後按一下 [檢查更新] 以在您的系統上安裝最新的更新。The simplest way to resolve this is by going to Windows Settings and clicking 'Check for Updates' to install the latest updates on your system. 您可以在這裡檢視如何取得反向修補的完整指示。You can view the full instructions on taking the backport here.

如果您按 [檢查更新] 但仍未收到更新,則可以遵循此連結來手動安裝 KB KB4566116。If you hit 'Check for Updates' and still do not receive the update you can install KB KB4566116 manually by following this link.

錯誤:0x1bc 發生於 wsl --set-default-version 2Error: 0x1bc when wsl --set-default-version 2

當 [顯示語言] 或 [系統地區設定] 設定不是英文時,就可能發生這種情況。This may happen when 'Display Language' or 'System Locale' setting is not English.

wsl --set-default-version 2
Error: 0x1bc
For information on key differences with WSL 2 please visit https://aka.ms/wsl2

0x1bc 的實際錯誤是:THe actual error for 0x1bc is:

WSL 2 requires an update to its kernel component. For information please visit https://aka.ms/wsl2kernel

如需詳細資訊,請參閱問題 5749For more information, please refer to issue 5749

無法從 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.

請啟用虛擬機器平台 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. 以系統管理員身分啟動 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. 記下 VPN 的 DNS 伺服器,使其無法執行 ipconfig.exe /allTake a note of the DNS server of the VPN from doing ipconfig.exe /all
  2. 建立現有 resolv.conf sudo cp /etc/resolv.conf /etc/resolv.conf.new 的複本Make a copy of the existing resolv.conf sudo cp /etc/resolv.conf /etc/resolv.conf.new
  3. 取消連結目前的 resolv.conf sudo unlink /etc/resolv.confUnlink the current resolv.conf sudo unlink /etc/resolv.conf
  4. sudo mv /etc/resolv.conf.new /etc/resolv.conf
  5. 開啟 /etc/resolv.confOpen /etc/resolv.conf and
    a.a. 刪除檔案中的第一行,這一行會顯示「#此檔案由 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.".
    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 升級錯誤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

"Error: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

"Error:0x80040154」,在 Windows 更新之後出現"Error: 0x80040154" after Windows update

「適用於 Linux 的 Windows 子系統」功能可能會在 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 中沒有網際網路存取No internet access in WSL

某些使用者回報了特定防火牆應用程式在 WSL 中封鎖網際網路存取的問題。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),需要 Windows 中的系統管理員權限,才能在 WSL 中執行 ping。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 or 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. 在電子郵件中,請注意,傾印適用於 Windows 小組的 WSL 或 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

尋找 [作業系統組建] 和 [系統類型] 欄位。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. 檢查啟動記錄,並確認 HostKeys 可供使用,而且您未看到記錄訊息,例如: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-server: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

啟用 WSL 選用功能時"The referenced assembly could not be found." 會顯示「找不到參考的組件」。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.