问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

WSL2 高级设置配置选项参数说明

创作时间:
作者:
@小白创作中心

WSL2 高级设置配置选项参数说明

引用
1
来源
1.
https://dancying.cn/windows/windows-004.html

WSL2(Windows Subsystem for Linux 2)是微软推出的一项技术,允许用户在Windows系统上运行Linux环境。默认情况下,WSL2中的Linux子系统资源分配可能无法满足开发人员的需求。本文将详细介绍如何通过.wslconfig文件对WSL2进行高级设置,包括内存、CPU、网络等关键资源的配置。

前言

WSL2可以在Windows系统上提供无缝高效的Linux环境体验。但是默认情况下,WSL2中的Linux子系统资源分配(CPU 、内存、网络)可能不满足开发人员的需求。微软提供了两种WSL设置的配置文件类型,其简单区别如表格所示:

配置文件
文件位置
适用范围
.wslconfig
Windows用户目录下
作用于WSL2中的全部Linux发行版(仅WSL2可用)
wsl.conf
实际为Linux的/etc/wsl.conf文件
作用于WSL中的单独Linux发行版(WSL1和WSL2均可用)

根据实际使用场景,本文采用的是.wslconfig全局配置的方式。Windows版本:Windows 11家庭版,22H2。

可用配置

WSL2的全局配置需要使用.wslconfig文件。目前.wslconfig文件内容可分为主要配置和实验性配置,这两者分别以[wsl2]和[experimental]作为区块名。

若想.wslconfig配置生效,需要把.wslconfig文件存放至Windows的用户目录下(该文件默认不存在)。

建立配置

  1. 在Windows终端执行以下命令打开用户目录:
start %UserProfile%
  1. 在Windows的用户目录下创建名为.wslconfig的文件,然后编辑.wslconfig文件的内容即可。

注意:

使用.wslconfig全局配置需要Windows版本大于等于19041以及使用WSL2运行Linux发行版。执行命令wsl --shutdown关闭WSL2中的发行版后才能使.wslconfig配置生效(完全关闭WSL2中的Linux大约需要8秒)。

主要配置

以下为.wslconfig文件中可用的主要配置(该部分以[wsl2]作为区块名):

注意:

带有*号的配置仅能在Windows 11系统上使用
带有**号的配置仅能在Windows 11 22H2及以上版本使用

实验性配置

以下为.wslconfig文件中可用的实验性配置(该部分以[experimental]作为区块名):

注意:

带有*号的配置仅能在Windows 11系统上使用
带有**号的配置仅能在Windows 11 22H2及以上版本使用

配置示例

本部分展示了.wslconfig文件的官方示例和我在使用的个人示例,可以根据示例配置自行修改并应用。

官方示例

以下为微软官方提供的.wslconfig文件配置内容示例:

# Settings apply across all Linux distros running on WSL 2
[wsl2]
# Limits VM memory to use no more than 4 GB, this can be set as whole numbers using GB or MB
memory=4GB 
# Sets the VM to use two virtual processors
processors=2
# Specify a custom Linux kernel to use with your installed distros. The default kernel used can be found at https://github.com/microsoft/WSL2-Linux-Kernel
kernel=C:\\temp\\myCustomKernel
# Sets additional kernel parameters, in this case enabling older Linux base images such as Centos 6
kernelCommandLine = vsyscall=emulate
# Sets amount of swap storage space to 8GB, default is 25% of available RAM
swap=8GB
# Sets swapfile path location, default is %USERPROFILE%\AppData\Local\Temp\swap.vhdx
swapfile=C:\\temp\\wsl-swap.vhdx
# Disable page reporting so WSL retains all allocated memory claimed from Windows and releases none back when free
pageReporting=false
# Turn on default connection to bind WSL 2 localhost to Windows localhost. Setting is ignored when networkingMode=mirrored
localhostforwarding=true
# Disables nested virtualization
nestedVirtualization=false
# Turns on output console showing contents of dmesg when opening a WSL 2 distro for debugging
debugConsole=true
# Enable experimental features
[experimental]
sparseVhd=true

个人示例

以下为我个人使用的.wslconfig文件配置内容示例:

# document : https://learn.microsoft.com/en-us/windows/wsl/wsl-config#example-wslconfig-file
# Settings apply across all Linux distros running on WSL 2
[wsl2]
# Limits VM memory to use no more than 2 GB, this can be set as whole numbers using GB or MB
memory=2GB
# Sets the VM to use two virtual processors
processors=2
# Sets amount of swap storage space to 4GB, default is 25% of available RAM
swap=4GB
# Disables nested virtualization
nestedVirtualization=false
# If the value is mirrored then this turns on mirrored networking mode
networkingMode=mirrored
# Changes how DNS requests are proxied from WSL to Windows
dnsTunneling=true
# Enforces WSL to use Windows’ HTTP proxy information
autoProxy=true
# -----------------------------------------------------------------------------------------------------
# These settings are opt-in previews of experimental features that we aim to make default in the future
# Enable experimental features
[experimental]
# Automatically releases cached memory after detecting idle CPU usage
autoMemoryReclaim=dropcache
# When this option is set to false, 
# DNS requests tunneled from Linux will bypass cached names within Windows to always put the requests on the wire
useWindowsDnsCache=true
# When set to true, 
# Windows will extract the question from the DNS request and attempt to resolve it, ignoring the unknown records
bestEffortDnsParsing=true

总结

文中的示例仅用于.wslconfig配置文件,.wslconfig配置文件作用于WSL2中的所有Linux发行版。若在.wslconfig文件中设置memory=2GB,则WSL2中的所有Linux发行版将共用2GB内存。.wslconfig配置文件需要遵循INI语法。

参考链接

  • .wslconfig – 微软文档
© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号