適用於 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 桌面和新式 Microsoft 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?

這主要是適用於開發人員的工具,特別是網頁程式開發人員,以及使用開放原始碼專案的使用者。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.exe 的應用程式,啟動時,會開啟執行 Bash 命令介面的 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 命令介面存取本機電腦的檔案系統,您會發現您的本機磁碟機掛接在 /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. 假設 Sam 正在建立 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. Sam 可以啟用 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. 在本機驗證開發管線之後,Sam 就可以將該 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 是常用的文字型命令介面和命令語言。Bash is a popular text-based shell and command-language. 這是包含在 Ubuntu 和其他 Linux 散發版本以及 macOS 中的預設命令介面。It is the default shell included within Ubuntu and other Linux distros, and in macOS. 使用者在命令介面中輸入命令來執行指令碼及/或執行命令和工具,以完成許多工作。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.

為什麼我會在 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 也可讓您同時執行 Linux 命令列工具和應用程式,以及 Windows 命令列、桌面和 Microsoft Store 應用程式,以及從 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 上的 Ruby,而不是 Windows 上的?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 和節點) 通常會在 Windows 上移植並執行良好。Many languages like Ruby and node are often ported to, and run great, on Windows. 不過,並非所有 Ruby Gem 或節點/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 小組部落格深入了解:適用於 Windows 的 Bash:為什麼它非常棒,而對 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?

適用於 Linux 的 windows 子系統可在 windows 10 年度版和 Creators Update 或更新版本的 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?)

即使您具有複雜的驗證模式 (例如 Azure Active Directory 或雙因素驗證) ,Git 認證管理員也可讓您對遠端 Git 伺服器進行驗證。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.

此支援會依賴 Windows 子系統 Linux 版和 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/

使用您掛接的磁碟機,您可以在其中編輯程式碼,例如 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 的其中一個主要限制是,不支援使用 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 升級時,為什麼會發生這麼多錯誤?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

從 Microsoft Store 安裝的 WSL 散發套件可以像任何其他 Windows 應用程式一樣,透過以滑鼠右鍵按一下應用程式磚,然後按一下 [解除安裝],或透過 PowerShell 使用 Remove-AppxPackage Cmdlet,來解除安裝。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.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?

「適用於 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" 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 存取網際網路?Why do I not have internet access from WSL?

某些使用者回報了特定防火牆應用程式在 WSL 中封鎖網際網路存取的問題。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 內容,您可以透過 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, or move them from one drive to another?

若要備份或移動發行版本,最佳方式是透過 Windows 1809 版和更新版本中提供的匯出/匯入命令。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 發行版本的狀態。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: