WSL 1 与 WSL 2 的用户体验差异User Experience Changes Between WSL 1 and WSL 2

此页主要描述 WSL 1 和 WSL 2 预览版之间的用户体验差异。This page goes over the user experience differences between WSL 1 and the WSL 2 preview. 关键差异包括:The key changes to be aware of are:

  • 将 Linux 应用需要访问的文件放在 Linux 根文件系统中可以提高文件访问速度Place files that your Linux apps will access in your Linux root file system for faster file performance speed
  • 在 WSL 2 预览版的初始版本中,你需要使用 IP 地址而不是 localhost 来访问网络应用程序In initial builds of the WSL 2 preview you will need to access network applications using an IP address and not using localhost

下面是可能会注意到的差异的完整列表:And below is the full list of other changes that you may notice:

  • WSL 2 使用虚拟硬件磁盘(VHD)来存储文件,如果达到其最大大小,则可能需要将其扩展WSL 2 uses a Virtual Hardware Disk (VHD) to store your files and if you reach its max size you may need to expand it
  • 启动时,WSL 2 现在将使用少量内存When starting, WSL 2 will now use a small proportion of memory
  • 初始预览版本中,跨 OS 文件访问速度会变慢Cross OS file access speed will be slower in initial preview builds

将 Linux 文件放在 Linux 根文件系统中Place your Linux files in your Linux root file system

请务必将使用 Linux 应用程序频繁访问的文件放在 Linux 根文件系统中,从而获取文件性能方面的优势。Make sure to put the files that you will be accessing frequently with Linux applications inside of your Linux root file system to enjoy the file performance benefits. 这些文件只有在 Linux 根文件系统中才能实现更快的文件系统访问。These files have to be inside of the Linux root file system to have faster file system access. 我们还使 Windows 应用能访问 Linux 根文件系统(如文件资源管理器!We have also made it possible for Windows apps to access the Linux root file system (like File Explorer! 尝试在 Linux 发行版的 home 目录中运行:explorer.exe .,看看会发生什么), 这会使文件转移更加便捷。Try running: explorer.exe . in the home directory of your Linux distro and see what happens) which will make this transition significantly easier.

访问网络应用程序Accessing network applications

在 WSL 2 预览版的初始版本中,需要使用主机的 IP 地址从 Linux 访问任何 Windows server。In the initial builds of the WSL 2 preview, you will need to access any Windows server from Linux using the IP address of your host machine.

从 Linux 访问 Windows 应用程序Accessing Windows applications from Linux

若要访问 Windows 网络应用程序,需要使用主机的 IP 地址。To access a Windows network application you'll need to use the IP address of your host machine. 为此,可以执行以下步骤:You can do so with these steps:

  • 通过运行命令cat /etc/resolv.conf并复制该字词nameserver后面的 ip 地址来获取主机的 ip 地址。Obtain the IP address of your host machine by running the command cat /etc/resolv.conf and copying the IP address following the term nameserver.
  • 使用复制的 IP 地址连接到任何 Windows server。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 网络应用程序

从 Windows 访问 Linux 应用程序(仅在版本低于18945的版本中)Accessing Linux applications from Windows (only in builds lower than 18945)

如果在 WSL 发行版中有服务器,需要查找运行 Linux 发行版的虚拟机的 IP 地址并使用该 IP 地址连接它。If you have a server in a WSL distro, you'll need to find the IP address of the virtual machine powering your distro and connect to it with that IP address. 可以通过以下步骤来实现此目的:You can do that by following these steps:

  • 通过在 WSL 发行版中运行命令ip addr ,并inet在该eth0接口的值下面查找,来获取发行版的 IP 地址。Obtain the IP address of your distro by running the command ip addr inside of your WSL distro and finding it under the inet value of the eth0 interface.
    • 可以通过使用 grep 筛选命令的输出来更轻松地找到此内容,如下所ip addr | grep eth0示:。You can find this more easily by filtering the output of the command using grep like so: ip addr | grep eth0.
  • 使用上面找到的 IP 连接到 Linux 服务器。Connect to your Linux server using the IP you found above.

下图显示了一个示例,该示例通过使用 Edge 浏览器连接到 node.js 服务器。The picture below shows an example of this by connecting to a Node.js server using the Edge browser.

从 Windows 访问 Linux 网络应用程序

如果生成为18945或更高版本,则可以像平时一样使用 localhost。If your build is 18945 or higher, you can use localhost just like normal.

其他网络注意事项Other networking considerations

当使用远程 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 连接,例如:你可能需要将应用程序绑定到0.0.0.0127.0.0.1而不是。This means that you will need to make sure your application can accept LAN connections, i.e: You might need to bind your application to 0.0.0.0 instead of 127.0.0.1. 例如,在 python 中使用 flask,可以使用以下命令完成此app.run(host='0.0.0.0')操作:。For example in python 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.

了解 WSL 2 使用 VHD,如果达到其最大大小,会发生什么情况Understanding WSL 2 uses a VHD, and what to do if you reach its max size

WSL 2 将所有 Linux 文件都存储在使用 ext4 文件系统的 VHD 中。WSL 2 stores all your Linux files inside of a VHD that uses the ext4 file system. 此 VHD 会自动调整大小以满足你的存储需求。This VHD automatically resizes to meet your storage needs. 此 VHD 的初始最大大小为256GB。This VHD also has an initial max size of 256GB. 如果发行版大小增长到大于256GB,则会看到错误,指出磁盘空间不足。If your distro 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 these by expanding the VHD size. 下面是有关如何执行此操作的说明:Instructions on how to do so are below:

  1. 使用wsl --shutdown命令终止所有 WSL 实例Terminate all WSL instances using the wsl --shutdown command
  2. 查找你的发行版安装包名称 "PackageFamilyName"Find your distro installation package name 'PackageFamilyName'
    • 在 powershell 提示符下(其中,"发行版" 是你的分发名称)键入:In a powershell prompt (where 'distro' is your distribution name) type:
      • Get-AppxPackage -Name "*<distro>*" | Select PackageFamilyName
  3. 找到 WSL 2 安装使用的 VHD 文件 fullpath,这将是 "pathToVHD":Locate 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
    • 使用管理员权限打开命令提示符窗口,并运行以下命令:Open a command prompt Window with admin privileges and run the following commands:
      • diskpart
      • Select vdisk file="<pathToVHD>"
      • expand vdisk maximum="<sizeInMegaBytes>"
  5. 启动 WSL 发行版Launch your WSL distro
  6. 请注意,WSL 可以扩展其文件系统的大小Make WSL aware that it can expand its file system's size
    • 在 WSL 发行版中运行以下命令:Run these commands in your WSL distro:
      • 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
        • 请确保使用前面复制的值,并且可能需要使用: apt install resize2fsMake sure to use the value you copied earlier, and you may need to use: apt install resize2fs.

请注意:通常,不要使用 Windows 工具或编辑器修改、移动或访问位于 AppData 文件夹内的 WSL 相关文件。Please note: 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 distro to become corrupted.

WSL 2 将在启动时使用一些内存WSL 2 will use some memory on startup

WSL 2 在实际 Linux 内核上使用轻型实用程序 VM,以提供高性能的文件系统性能和系统调用的完全兼容性,同时仍然像 WSL 1 一样快速、快速、集成且响应迅速。WSL 2 uses a lightweight utility VM on a real Linux kernel to provide great file system performance and full system call compatibility while still being just as light, fast, integrated and responsive as WSL 1. 此实用程序 VM 的内存占用较小,将在启动时分配虚拟地址支持的内存。This utility VM has a small memory footprint and will allocate Virtual Address backed memory on startup. 将其配置为从总体内存的小部分开始。It is configured to start with a small proportion of your total memory.

在初始预览版中,跨 OS 文件速度会变慢Cross OS file speed will be slower in initial preview builds

从 Linux 应用程序访问 Windows 文件或从 Windows 应用程序访问 Linux 文件时,与 WSL 1 相比,你会注意到文件速度更慢。You will notice slower file speeds compared to WSL 1 when accessing Windows files from a Linux application, or accessing Linux files from a Windows application. 这是 WSL 2 中体系结构更改的结果,WSL 团队正在积极地调查如何改进此体验。This is a result of the architectural changes in WSL 2, and is something that the WSL team is actively investigating on how we can improve this experience.