比較 WSL 1 和 WSL 2Comparing WSL 1 and WSL 2

將 Windows 子系統 Linux 版更新為新版本的主要目標是提高檔案系統效能,以及支援完整的系統呼叫相容性The primary goals of updating the Windows Subsystem for Linux to a new version are to increase file system performance and support full system call compatibility.

WSL 2 使用最新最強大的虛擬化技術,在輕量型公用程式虛擬機器 (VM) 內部執行 Linux 核心。WSL 2 uses the latest and greatest in virtualization technology to run a Linux kernel inside of a lightweight utility virtual machine (VM). 不過,WSL 2 並不是傳統的 VM 體驗。However, WSL 2 is not a traditional VM experience. 深入了解 WSL 2 架構Learn more about the WSL 2 architecture.

比較功能Comparing features

功能Feature WSL 1WSL 1 WSL 2WSL 2
Windows 和 Linux 之間的整合Integration between Windows and Linux
快速開機時間Fast boot times
低資源使用量Small resource foot print
以最新版的 VMWare 和 VirtualBox 執行Runs with current versions of VMWare and VirtualBox
受控 VMManaged VM
完整的 Linux 核心Full Linux Kernel
完整的系統呼叫相容性Full system call compatibility
OS 檔案系統之間的效能Performance across OS file systems

已使用 WSL 1 且想要升級至 WSL 2 嗎?Already using WSL 1 and want to upgrade to WSL 2? 請依照指示來更新至 WSL 2Follow the instructions to update to WSL 2!

WSL 2 僅適用於 Windows 10 2004 版 (組建 19041 或更高版本)。WSL 2 is only available in Windows 10, Version 2004, Build 19041 or higher. 若要檢查您的 Windows 版本,請選取 [Windows 標誌鍵 + R]並輸入 winver,然後選取 [確定]。Check your Windows version by selecting the Windows logo key + R, type winver, select OK. (或在 Windows 命令提示字元中輸入 ver 命令)。(Or enter the ver command in Windows Command Prompt). 您可能需要 更新至最新的 Windows 版本You may need to update to the latest Windows version. 若組建低於 19041,則完全不支援 WSL。For builds lower than 19041, WSL is not supported at all.

注意

WSL 2 將搭配使用 VMWare 15.5.5+VirtualBox 6+WSL 2 will work with VMWare 15.5.5+ and VirtualBox 6+.

使用 Linux 檔案系統以獲得更快的效能Use the Linux file system for faster performance

若要最佳化以獲得最快速的效能,請務必將您的專案檔儲存在 Linux 檔案系統中 (而不是 Windows 檔案系統)。In order to optimize for the fastest performance speed, be sure to store your project files in the Linux file system (not the Windows file system).

例如,儲存 WSL 專案檔時:For example, when storing your WSL project files:

  • 使用 Linux 檔案系統根目錄:\\wsl$\Ubuntu-18.04\home\<user name>\ProjectUse the Linux file system root directory: \\wsl$\Ubuntu-18.04\home\<user name>\Project
  • 不是 Windows 檔案系統根目錄:C:\Users\<user name>\ProjectNot the Windows file system root directory: C:\Users\<user name>\Project

搭配 WSL 發行版本 (例如 Ubuntu) 使用的專案檔必須位於 Linux 根檔案系統中,才能更快速地存取檔案系統。Project files that you are working with using a WSL distribution (like Ubuntu) must be in the Linux root file system to take advantage of faster file system access.

您可以使用 Windows 應用程式和工具 (例如檔案總管) 來存取 Linux 根檔案系統。You can access your Linux root file system with Windows apps and tools like File Explorer. 請嘗試開啟 Linux 發行版本 (例如 Ubuntu),並輸入下列命令以確定您位於 Linux 主目錄:cd ~Try opening a Linux distribution (like Ubuntu), be sure that you are in the Linux home directory by entering this command: cd ~. 然後藉由輸入:explorer.exe . (不要忘了結尾的句點),在 [檔案總管] 中開啟您的 Linux 檔案系統Then open your Linux file system in File Explorer by entering (don't forget the period at the end): explorer.exe .

使用 WSL 1 而非 WSL 2 的例外狀況Exceptions for using WSL 1 rather than WSL 2

我們建議您使用 WSL 2,因為其提供更快的效能和 100% 的系統呼叫相容性。We recommend that you use WSL 2 as it offers faster performance and 100% system call compatibility. 不過,在某些特定案例中,使用 WSL 1 是較好的選擇。However, there are a few specific scenarios where you might prefer using WSL 1. 如果是下列情況,請考慮使用 WSL 1:Consider using WSL 1 if:

  • 您的專案檔案必須儲存在 Windows 檔案系統中。Your project files must be stored in the Windows file system.
    • 如果您將使用 WSL Linux 發行版本來存取 Windows 檔案系統上的專案檔,而且這些檔案無法儲存在 Linux 檔案系統上,那麼使用 WSL 1,將可以在 OS 檔案系統中達到更快的效能。If you will be using your WSL Linux distribution to access project files on the Windows file system, and these files cannot be stored on the Linux file system, you will achieve faster performance across the OS files systems by using WSL 1.
  • 需要在相同檔案上同時使用 Windows 和 Linux 工具進行跨平台編譯的專案。A project which requires cross-compilation using both Windows and Linux tools on the same files.
    • 在 WSL 1 中,Windows 和 Linux 作業系統上的檔案效能會比在 WSL 2 中還要快,因此,如果您使用 Windows 應用程式來存取 Linux 檔案,您目前可透過 WSL 1 來達到更快的效能。File performance across the Windows and Linux operating systems is faster in WSL 1 than WSL 2, so if you are using Windows applications to access Linux files, you will currently achieve faster performance with WSL 1.

注意

請考慮嘗試 VS Code 遠端 WSL 擴充功能,讓您將專案檔儲存在 Linux 檔案系統上並使用 Linux 命令列工具,以及在 Windows 上使用 VS Code 來撰寫、編輯、偵錯及執行網際網路瀏覽器中的專案,讓您的效能不會因為在 Linux 和 Windows 檔案系統上作業而變慢。Consider trying the VS Code Remote WSL Extension to enable you to store your project files on the Linux file system, using Linux command line tools, but also using VS Code on Windows to author, edit, debug, or run your project in an internet browser without any of the performance slow-downs associated with working across the Linux and Windows file systems. 深入了解Learn more.

WSL 2 架構WSL 2 architecture

傳統的 VM 體驗可能會很慢而無法開機、隔離的、耗用大量資源,而且需要您花時間管理。A traditional VM experience can be slow to boot up, is isolated, consumes lots of resources, and requires your time to manage it. WSL 2 沒有這些屬性。WSL 2 does not have these attributes.

WSL 2 可提供 WSL 1 的優點,包括 Windows 和 Linux 之間的無縫整合、快速開機時間、低資源使用量,以及不需要進行 VM 設定或管理。WSL 2 provides the benefits of WSL 1, including seamless integration between Windows and Linux, fast boot times, a small resource footprint, and requires no VM configuration or management. 雖然 WSL 2 會使用 VM,但會在幕後進行管理及執行,讓您擁有與 WSL 1 相同的使用者體驗。While WSL 2 does use a VM, it is managed and run behind the scenes, leaving you with the same user experience as WSL 1.

完整的 Linux 核心Full Linux kernel

WSL 2 中的 Linux 核心是 Microsoft 根據可在 kernel.org 取得的來源,從最新的穩定分支所建立的。此核心已特別針對 WSL 2 進行微調,並針對大小和效能進行最佳化,可在 Windows 上提供絕佳的 Linux 體驗。The Linux kernel in WSL 2 is built by Microsoft from the latest stable branch, based on the source available at kernel.org. This kernel has been specially tuned for WSL 2, optimizing for size and performance to provide an amazing Linux experience on Windows. 該核心將由 Windows 更新提供服務,這表示您將會獲得最新的安全性修正程式和核心改良功能,而且不需要自行管理。The kernel will be serviced by Windows updates, which means you will get the latest security fixes and kernel improvements without needing to manage it yourself.

WSL 2 Linux 核心是開放原始碼The WSL 2 Linux kernel is open source. 如果您想要深入了解,請參閱部落格文章:以 Windows 搭載 Linux 核心,此文章是由其開發小組所撰寫。If you'd like to learn more, check out the blog post Shipping a Linux Kernel with Windows written by the team that built it.

增加檔案 IO 效能Increased file IO performance

處理大量檔案的作業 (例如 git 複製、npm 安裝、apt 更新、apt 升級等等) 都可使用 WSL 2 來明顯地加快速度。File intensive operations like git clone, npm install, apt update, apt upgrade, and more are all noticeably faster with WSL 2.

實際增加的速度將取決於您正在執行的應用程式,以及其與檔案系統的互動方式。The actual speed increase will depend on which app you're running and how it is interacting with the file system. 相較於 WSL 1,WSL 2 的初始版本在解壓縮 zip 壓縮的 tarball 時,執行速度最多可快 20 倍,而在各種專案上使用 git 複製、npm 安裝和 cmake 時,大約可快 2 至 5 倍。Initial versions of WSL 2 run up to 20x faster compared to WSL 1 when unpacking a zipped tarball, and around 2-5x faster when using git clone, npm install and cmake on various projects.

完整的系統呼叫相容性Full system call compatibility

Linux 二進位檔會使用系統呼叫來執行功能,例如存取檔案、要求記憶體、建立處理程序等等。Linux binaries use system calls to perform functions such as accessing files, requesting memory, creating processes, and more. WSL 1 使用 WSL 小組所建置的轉譯層,而 WSL 2 則包含其本身的 Linux 核心,並具有完整系統呼叫相容性。Whereas WSL 1 used a translation layer that was built by the WSL team, WSL 2 includes its own Linux kernel with full system call compatibility. 好處包括:Benefits include:

  • 您可以在 WSL 內執行一套全新的應用程式,例如 Docker 等。A whole new set of apps that you can run inside of WSL, such as Docker and more.

  • Linux 核心的任何更新都會立即準備好供您使用。Any updates to the Linux kernel are immediately ready for use. (您不需要等候 WSL 小組執行更新和新增變更)。(You don't have to wait for the WSL team to implement updates and add the changes).

WSL 2 會在啟動時使用較低的記憶體數量WSL 2 uses a smaller amount of memory on startup

WSL 2 在實際的 Linux 核心上使用輕量公用程式 VM,其記憶體使用量較小。WSL 2 uses a lightweight utility VM on a real Linux kernel with a small memory footprint. 公用程式會在啟動時配置虛擬位址支援的記憶體。The utility will allocate Virtual Address backed memory on startup. 其已設定為從您總記憶體 (WSL 1 所需用量) 中的一小部分啟動。It is configured to start with a smaller proportion of your total memory that what was required for WSL 1.

存取網路應用程式Accessing network applications

從 Windows (localhost) 存取 Linux 網路應用程式Accessing Linux networking apps from Windows (localhost)

如果您要在 Linux 發行版本中建立網路應用程式 (例如,在 NodeJS 或 SQL 伺服器上執行的應用程式),您可以使用 localhost 從 Windows 應用程式 (例如您的 Edge 或 Chrome 網際網路瀏覽器) 進行存取 (如同您平時執行的動作)。If you are building a networking app (for example an app running on a NodeJS or SQL server) in your Linux distribution, you can access it from a Windows app (like your Edge or Chrome internet browser) using localhost (just like you normally would).

不過,如果您執行的是舊版 Windows (組建 18945 或更低版本),您將需要取得 Linux 主機 VM 的 IP 位址 (或是更新為最新的 Windows 版本)。However, if you are running an older version of Windows (Build 18945 or less), you will need to get the IP address of the Linux host VM (or update to the latest Windows version).

若要尋找支援您 Linux 發行版本的虛擬機器 IP 位址:To find the IP address of the virtual machine powering your Linux distribution:

  • 從您的 WSL 發行版本 (也就是 Ubuntu) 執行命令:ip addrFrom your WSL distribution (ie Ubuntu), run the command: ip addr
  • 尋找並複製 eth0 介面上 inet 值底下的位址。Find and copy the address under the inet value of the eth0 interface.
  • 如果您已安裝 grep 工具,可使用 ip addr | grep eth0 命令來篩選輸出,以便您找出該項目If you have the grep tool installed, find this more easily by filtering the output with the command: ip addr | grep eth0
  • 使用此 IP 位址連線到您的 Linux 伺服器。Connect to your Linux server using this IP address.

下圖顯示這種情況的範例,使用 Edge 瀏覽器連線到 Node.js 伺服器。The picture below shows an example of this by connecting to a Node.js server using the Edge browser.

從 Windows 存取 Linux 網路應用程式

從 Linux (主機 IP) 存取 Windows 網路應用程式Accessing Windows networking apps from Linux (host IP)

如果您想要從 Linux 發行版本 (也就是 Ubuntu) 存取在 Windows 上執行的網路應用程式 (例如,在 NodeJS 或 SQL 伺服器上執行的應用程式),則需要使用主機電腦的 IP 位址。If you want to access a networking app running on Windows (for example an app running on a NodeJS or SQL server) from your Linux distribution (ie Ubuntu), then you need to use the IP address of your host machine. 雖然這不是常見的案例,但您可以遵循下列步驟來讓其執行。While this is not a common scenario, you can follow these steps to make it work. - 從 Linux 發行版本執行下列命令,以取得主機電腦的 IP 位址:cat /etc/resolv.confObtain the IP address of your host machine by running this command from your Linux distribution: cat /etc/resolv.conf - 複製下列詞彙後面的 IP 位址:nameserverCopy the IP address following the term: nameserver. - 使用複製的 IP 位址連線到任何 Windows 伺服器。Connect to any Windows server using the copied IP address.

下圖顯示這種情況的範例,透過 curl 連線到在 Windows 中執行的 Node.js 伺服器。The picture below shows an example of this by connecting to a Node.js server running in Windows via curl.

從 Windows 存取 Linux 網路應用程式

其他網路功能考量Additional networking considerations

透過遠端 IP 位址連線Connecting via remote IP addresses

使用遠端 IP 位址連線到您的應用程式時,系統會將其視為來自區域網路 (LAN) 的連線。When using remote IP addresses to connect to your applications, they will be treated as connections from the Local Area Network (LAN). 這表示您必須確定您的應用程式可以接受 LAN 連線。This means that you will need to make sure your application can accept LAN connections.

例如,您可能需要將應用程式繫結至 0.0.0.0,而不是 127.0.0.1For example, you may need to bind your application to 0.0.0.0 instead of 127.0.0.1. 在使用 Flask 的 Python 應用程式範例中,您可以使用下列命令來完成這項作業:app.run(host='0.0.0.0')In the example of a Python app using Flask, this can be done with the command: app.run(host='0.0.0.0'). 在進行這些變更時,請記住安全性,因為這會允許來自您 LAN 的連線。Please keep security in mind when making these changes as this will allow connections from your LAN.

從區域網路 (LAN) 存取 WSL 2 發行版本Accessing a WSL 2 distribution from your local area network (LAN)

使用 WSL 1 發行版本時,如果您的電腦設定為在您的 LAN 中存取,則在 WSL 中執行的應用程式也可以在您的 LAN 上存取。When using a WSL 1 distribution, if your computer was set up to be accessed by your LAN, then applications run in WSL could be accessed on your LAN as well.

這不是 WSL 2 中的預設案例。This isn't the default case in WSL 2. WSL 2 具有虛擬化的乙太網路介面卡,而且具有自己的唯一 IP 位址。WSL 2 has a virtualized ethernet adapter with its own unique IP address. 目前若要啟用這個工作流程,您需要進行與一般虛擬機器相同的步驟。Currently, to enable this workflow you will need to go through the same steps as you would for a regular virtual machine. (我們正在尋找改善這種體驗的方式。)(We are looking into ways to improve this experience.)

IPv6 存取IPv6 access

WSL 2 發行版本目前無法連線至僅限 IPv6 的位址。WSL 2 distributions currently cannot reach IPv6-only addresses. 我們正致力於新增這項功能。We are working on adding this feature.

擴充 WSL 2 虛擬硬體磁碟的大小Expanding the size of your WSL 2 Virtual Hardware Disk

WSL 2 使用虛擬硬體磁碟 (VHD) 來儲存您的 Linux 檔案。WSL 2 uses a Virtual Hardware Disk (VHD) to store your Linux files. 如果您達到其大小上限,您可能需要進行擴充。If you reach its max size you may need to expand it.

WSL 2 VHD 使用 ext4 檔案系統。The WSL 2 VHD uses the ext4 file system. 此 VHD 會自動調整規模以符合您的儲存體需求,而其初始大小上限為 256GB。This VHD automatically resizes to meet your storage needs and has an initial maximum size of 256GB. 如果您的發行版本大小成長到大於 256GB,您將會看到錯誤,指出您已用盡磁碟空間。If your distribution grows in size to be greater than 256GB, you will see errors stating that you've run out of disk space. 您可以藉由擴充 VHD 大小來修正此問題。You can fix this error by expanding the VHD size.

若要擴充超過 256GB 的 VHD 大小上限:To expand your maximum VHD size beyond 256GB:

  1. 使用此命令終止所有 WSL 執行個體:wsl --shutdownTerminate all WSL instances using the command: wsl --shutdown

  2. 尋找發行版本安裝套件名稱 ('PackageFamilyName')Find your distribution installation package name ('PackageFamilyName')

    • 使用 PowerShell (其中 'distro' 是您的發行版本名稱) 輸入下列命令:Using PowerShell (where 'distro' is your distribution name) enter the command:
    • Get-AppxPackage -Name "*<distro>*" | Select PackageFamilyName
  3. 找出 WSL 2 安裝所使用的 VHD 檔案 fullpath,這會是您的 pathToVHDLocate the VHD file fullpath used by your WSL 2 installation, this will be your pathToVHD:

    • %LOCALAPPDATA%\Packages\<PackageFamilyName>\LocalState\<disk>.vhdx
  4. 完成下列命令以調整您的 WSL 2 VHD 大小:Resize your WSL 2 VHD by completing the following commands:

    • 以系統管理員權限開啟 Windows 命令提示字元,並輸入:Open Windows Command Prompt with admin privileges and enter:
      • diskpart
      • Select vdisk file="<pathToVHD>"
      • expand vdisk maximum="<sizeInMegaBytes>"
  5. 啟動您的 WSL 發行版本 (例如 Ubuntu)。Launch your WSL distribution (Ubuntu, for example).

  6. 讓 WSL 知道,其可以從 Linux 發行版本命令列中執行下列命令,以擴充其檔案系統的大小:Make WSL aware that it can expand its file system's size by running these commands from your Linux distribution command line:

    • sudo mount -t devtmpfs none /dev
    • mount | grep ext4
    • 複製此項目的名稱,如下所示:/dev/sdXX (X 代表任何其他字元)Copy the name of this entry, which will look like: /dev/sdXX (with the X representing any other character)
    • sudo resize2fs /dev/sdXX
    • 使用您稍早複製的值。Use the value you copied earlier. 您可能也需要安裝 resize2fs:apt install resize2fsYou may also need to install resize2fs: apt install resize2fs

注意

一般來說,請不要使用 Windows 工具或編輯器修改、移動或存取位於 AppData 資料夾內的 WSL 相關檔案。In general do not modify, move, or access the WSL related files located inside of your AppData folder using Windows tools or editors. 這麼做可能會導致您的 Linux 發行版本損毀。Doing so could cause your Linux distribution to become corrupted.