WSL2Linux 子系统(七)

WSL 配置

在 Windows Subsystem for Linux (WSL) 中,可以使用一个名为 .wslconfig 的配置文件来对整个 WSL 子系统进行全局配置。这个配置文件允许你指定一些高级选项,例如默认的 WSL 发行版、虚拟机内存限制等。wsl.conf 用于为在 WSL 1 或 WSL 2 上运行的每个 Linux 发行版按各个发行版配置本地设置。
上一篇文章中讲解《WSL 连接USB》,本篇则讲解WSL全局配置

作者:炭烤毛蛋 ,点击博主了解更多。


提示: .wslconfig 配置文件修改后需要重启WSL生效。


1 .wslconfig 配置文件

文件位置:.wslconfig 配置文件位于 Windows 用户目录下(例如 C:\Users\username.wslconfig)。如果该文件不存在,你可以创建一个新的文本文件并将其命名为 .wslconfig。

设定发行版本

默认发行版:通过 .wslconfig 文件,你可以指定默认的 WSL 发行版。例如,如果你同时安装了多个 WSL 发行版(如 Ubuntu、Debian 等),你可以设定一个默认的发行版。
其中将 Ubuntu 设置为默认发行版:

[wsl]
default=Ubuntu

虚拟机内存限制

通过 .wslconfig 文件,可以限制 WSL 子系统可用的内存量。要控制 WSL 子系统使用的物理内存时很有用。配置文件路径 C:\Users<UserName>.wslconfig,其中将 WSL 子系统的内存限制设置为 4 GB:

[wsl2]
memory=4GB

注意:在修改了 .wslconfig 文件后,你需要重启 WSL 子系统才能使更改生效。 .wslconfig 文件只适用于 WSL2 版本,不适用于 WSL1。如果你使用的是 WSL1会忽略.wslconfig 文件。

wsl 可用核心数

通过 .wslconfig 文件,可以限制 WSL 子系统可用的CPU数量。

[wsl2]
processors=2

交换分区大小

Windows 上 25% 的内存大小四舍五入到最接近的 GB。要向 WSL 2 VM 添加的交换空间量,0 表示无交换文件。 交换存储是当内存需求超过硬件设备上的限制时使用的基于磁盘的 RAM。

[wsl2]
swap=1G

内存动态分配

默认的 true 设置使 Windows 能够回收分配给 WSL 2 虚拟机的未使用内存。

[wsl2]
pageReporting=true

2. 详细介绍

  • wsl.conf
    wsl.conf 可用的配置选项 \wsl.localhost\Ubuntu-20.04\etc\wsl.conf。
# Automatically mount Windows drive when the distribution is launched
[automount]

# Set to true will automount fixed drives (C:/ or D:/) with DrvFs under the root directory set above. Set to false means drives won't be mounted automatically, but need to be mounted manually or with fstab.
enabled = true

# Sets the directory where fixed drives will be automatically mounted. This example changes the mount location, so your C-drive would be /c, rather than the default /mnt/c. 
root = /

# DrvFs-specific options can be specified.  
options = "metadata,uid=1003,gid=1003,umask=077,fmask=11,case=off"

# Sets the `/etc/fstab` file to be processed when a WSL distribution is launched.
mountFsTab = true

# Network host settings that enable the DNS server used by WSL 2. This example changes the hostname, sets generateHosts to false, preventing WSL from the default behavior of auto-generating /etc/hosts, and sets generateResolvConf to false, preventing WSL from auto-generating /etc/resolv.conf, so that you can create your own (ie. nameserver 1.1.1.1).
[network]
hostname = DemoHost
generateHosts = false
generateResolvConf = false

# Set whether WSL supports interop processes like launching Windows apps and adding path variables. Setting these to false will block the launch of Windows processes and block adding $PATH environment variables.
[interop]
enabled = false
appendWindowsPath = false

# Set the user when launching a distribution with WSL.
[user]
default = DemoUser

# Set a command to run when a new WSL instance launches. This example starts the Docker container service.
[boot]
command = service docker start
  • .wslconfig
    文件路径为 C:\Users<UserName>.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
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

结语

不枉博主详细讲解,欢迎订阅博主–炭烤毛蛋

  • 17
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值