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 设置为默认发行版:

bash 复制代码
[wsl]
default=Ubuntu

虚拟机内存限制

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

bash 复制代码
[wsl2]
memory=4GB

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

wsl 可用核心数

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

bash 复制代码
[wsl2]
processors=2

交换分区大小

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

bash 复制代码
[wsl2]
swap=1G

内存动态分配

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

bash 复制代码
[wsl2]
pageReporting=true

2. 详细介绍

  • wsl.conf
    wsl.conf 可用的配置选项 \wsl.localhost\Ubuntu-20.04\etc\wsl.conf。
bash 复制代码
# 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。
bash 复制代码
# 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

结语

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

相关推荐
xuanzdhc1 小时前
Linux 基础IO
linux·运维·服务器
愚润求学1 小时前
【Linux】网络基础
linux·运维·网络
bantinghy2 小时前
Linux进程单例模式运行
linux·服务器·单例模式
小和尚同志3 小时前
29.4k!使用 1Panel 来管理你的服务器吧
linux·运维
帽儿山的枪手3 小时前
为什么Linux需要3种NAT地址转换?一探究竟
linux·网络协议·安全
shadon1789 天前
回答 如何通过inode client的SSLVPN登录之后,访问需要通过域名才能打开的服务
linux
AWS官方合作商9 天前
AWS ACM 重磅上线:公有 SSL/TLS 证书现可导出,突破 AWS 边界! (突出新功能的重要性和突破性)
服务器·https·ssl·aws
小米里的大麦9 天前
014 Linux 2.6内核进程调度队列(了解)
linux·运维·驱动开发
算法练习生9 天前
Linux文件元信息完全指南:权限、链接与时间属性
linux·运维·服务器
忘了ʷºᵇₐ9 天前
Linux系统能ping通ip但无法ping通域名的解决方法
linux·服务器·tcp/ip