大老赖不赖
大老赖不赖
发布于 2022-07-05 / 134 阅读 / 0 评论 / 0 点赞

wsl.conf 和 .wslconfig

做个记录吧,以免以后用到

wsl.conf

  • 作为 unix 文件存储在 /etc 分发目录中。

  • 用于根据每个分发配置进行设置。此文件中配置的设置将仅应用于包含存储此文件的目录的特定Linux发行版。

  • 可用于WSL 1或WSL 2版本运行的发行版。

  • 若要访问已安装的发行版的/etc 目录,请使用发行版的命令行和 cd / 访问根目录,然后使用 ls 列出文件或使用 explorer.exe . 在 Windows 文件资源管理器中查看。 目录路径应如下所示: /etc/wsl.conf

.wslconfig

  • 存储在您的%UserProfile%目录中。

  • 用于在WSL 2版本运行的所有已安装Linux发行版中全局配置设置。

  • 只能用于由WSL 2运行的分发。作为WSL 1运行的分发不会受到此配置的影响,因为它们不是作为虚拟机运行的。

  • 要访问您的%UserProfile%目录,请在PowerShell中使用cd ~访问您的主目录(通常是您的用户配置文件C:\Users\<UserName>)或者您可以打开Windows文件资源管理器并在地址栏中输入 %UserProfile% 。目录路径应该类似于: C:\Users\<UserName>\.wslconfig

WSL将检测这些文件的存在,读取内容,并在每次启动WSL时自动应用配置设置。如果文件丢失或格式不正确(标记格式不正确),WSL将继续正常启动,而不应用配置设置。

备注:
使用wsl调整每个分布的设置。wsl.conf文件仅在Windows Build 17093及更高版本中可用。

第 8 秒的规则

您必须等到运行Linux发行版的子系统完全停止运行并重新启动,才能显示配置设置更新。这通常在关闭分发shell的所有实例后需要8秒左右。

如果您启动了一个发行版(即Ubuntu),请修改配置文件,关闭该发行版,然后重新启动它。您可以假设您的配置更改已立即生效。目前情况并非如此,因为子系统可能仍在运行。在重新启动之前,您必须等待子系统停止,以便有足够的时间提取您的更改。通过使用PowerShell和命令wsl--list--running,可以检查Linux发行版(shell)在关闭后是否仍在运行。如果没有正在运行的分发,您将收到响应:“没有正在运行的分发。”现在,您可以重新启动发行版以查看应用的配置更新。

命令wsl--shutdown是重新启动wsl 2发行版的快速途径,但它将关闭所有正在运行的发行版,因此请明智地使用。

wsl.conf 的配置设置

wsl.conf 文件基于每个分发配置设置。 (有关WSL 2发行版的全局配置,请参阅 .wslconfig).

wsl.conf文件支持四个部分: automount, network, interop, and user. (Modeled after .ini file conventions, keys are declared under a section, like .gitconfig files.) 查看wsl.conf 以获得存储wsl.conf的信息。

自动装载设置

标签部分: [automount]

key

default

说明

enabled

boolean

true

true 导致固定驱动器(如C:/D:/)自动装载到 DrvFs 中的 /mnt 下。false表示驱动器不会自动挂载,但您仍然可以手动或通过fstab挂载。

mountFsTab

boolean

true

true 设置启动 WSL 时要处理的 /etc/fstab。 /etc/fstab 是可在其中声明其他文件系统的文件,类似于 SMB 共享。 因此,在启动时,可以在 WSL 中自动装载这些文件系统。

root

string

/mnt/

设置固定驱动器要自动装载到的目录。 默认情况下,此设置设置为/mnt/,因此Windows文件系统 C 驱动器装载到/mnt/c/。 如果更改/mnt//windir/,应会看到固定的 C 驱动器已装载到/windir/c

options

逗号分隔的值列表,例如 uid、gid 等,请参阅下面的自动装载选项

空字符串

下面列出了自动装载选项值,并追加到默认 DrvFs 装载选项字符串。只能指定特定于 DrvFs 的选项。

自动装载选项将作为所有自动装载驱动器的装载选项应用。若仅更改特定驱动器的选项,请改用/etc/fstab文件。不支持装载二进制文件通常解析为标志的选项。如果要明确指定这些选项,必须在/etc/fstab中包含要指定的每个驱动器。

自动装载选项

为 Windows 驱动器 (DrvFs) 设置不同的装载选项可以控制为 Windows 文件计算文件权限的方式。 提供了以下选项:

Key

说明

Default

uid

用于所有文件的所有者的用户ID

首次安装时 WSL 发行版的默认用户 ID (默认为 1000)

gid

用于所有文件的所有者的组 ID

首次安装时 WSL 发行版的默认组 ID (默认为 1000)

umask

要对所有文件和目录排除的权限的八进制掩码

000

fmask

要对所有文件排除的权限的八进制掩码

000

dmask

要对所有目录排除的权限的八进制掩码

000

metadata

是否将元数据添加到 Windows 文件以支持 Linux 系统权限

disabled

case

确定被视为区分大小写的目录以及使用 WSL 创建的新目录是否将设置标志。 有关选项的详细说明,请参阅Adjust case sensitivity。 选项包括 off, dir, 及 force

off

默认情况下,WSL 会将 uid 和 gid 设置为默认用户的值。 例如,在 Ubuntu 中,默认用户为 uid=1000,gid=1000。 如果此值用于指定不同的 gid 或 uid 选项,将覆盖默认用户值。 否则,将始终追加默认值。

用户文件创建模式掩码 (umask) 设置新创建文件的权限。 默认值为 022,只能写入数据,但任何人都可以读取数据。 可以更改值以反映不同的权限设置。 例如, umask=077 更改完全私有的权限,其他用户无法读取或写入数据。 若要进一步指定权限,还可以使用 fmask) 和 dmask (目录 (文件) 。

备注:
权限掩码在应用于文件或目录之前,先执行逻辑“或”操作。

什么是 DrvFs?

DrvFs 是 WSL 的文件系统插件,旨在支持 WSL 与 Windows 文件系统之间的互操作。 DrvFs 使 WSL 能够在 /mnt 下装载支持文件系统的驱动器,例如 /mnt/c、/mnt/d 等。有关在装载Windows或 Linux 驱动器或目录时指定默认大小写敏感度行为的详细信息,请参阅Adjust case sensitivity

网络设置

标签部分: [network]

key

default

说明

generateHosts

boolean

true

true 将 WSL 设置为生成 /etc/hostshosts 文件包含主机名对应的 IP 地址的静态映射。

generateResolvConf

boolean

true

true 将 WSL 设置为生成 /etc/resolv.confresolv.conf 包含一个DNS列表,能够将给定的主机名解析为其IP地址。

hostname

string

Windows 主机名

设置要用于 WSL 分发的主机名。

互操作设置

标签部分: [interop]

这些选项在Insider Build 17713及更高版本中提供。

key

default

说明

enabled

boolean

true

设置此键可确定 WSL 是否支持启动 Windows 进程。

appendWindowsPath

boolean

true

设置此键可确定 WSL 是否会将 Windows 路径元素添加到 $PATH 环境变量。

用户设置

标签部分: [user]

这些选项在Build 18980和更高版本中可用。

key

default

说明

default

string

首次运行时创建的初始用户名

设置此键指定在首次启动 WSL 会话时以哪个用户身份运行。

启动设置

启动设置仅在Windows 11上可用。

标签部分: [boot]

key

default

说明

command

string

“”

你希望在 WSL 实例启动时运行的命令字符串。 此命令以root用户身份运行。 例如:service docker start仅适用于Windows 11。

systemd

boolean

false

使用systemd

示例 wsl.conf 文件

wsl.conf下面的示例文件演示了一些可用的配置选项。 在此示例中,发行版为 Ubuntu-20.04,文件路径为 \\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 process 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 的配置设置

.wslconfig 文件全局配置使用 WSL 2 运行的所有 Linux 分发版的设置。 (有关按分发配置,请参阅 wsl.conf)。

有关存储 .wslconfig 文件的位置的信息,请参阅 .wslconfig

[!备注]
全局.wslconfig配置选项仅适用于Windows内部版本 19041 及更高版本中作为 WSL 2 运行的分发版。 请记住,您可能需要运行 wsl --shutdown 来关闭 WSL 2 VM,然后重启 WSL 实例以使这些更改生效。

此文件可以包含以下选项,这些选项会影响为任何 WSL 2 分发提供支持的 VM:

标签部分: [wsl2]

key

value

default

notes

kernel

string

在此提供了Microsoft构建的内核

自定义Linux内核的绝对Windows路径。

memory

size

Windows总内存的50%或8GB,以较小者为准;在20175之前的版本上:Windows上总内存的80%

分配给WSL 2虚拟机的内存量。

processors

number

与Windows上的处理器数量相同

要分配给WSL 2虚拟机的处理器数量。

localhostForwarding

boolean

true

一个布尔值,用于指定绑定到 WSL 2 VM 中的通配符或 localhost 的端口是否应可通过 localhost:port 从主机连接。

kernelCommandLine

string

其他内核命令行参数。

swap

size

Windows 上 25% 的内存大小四舍五入到最接近的 GB

要向WSL 2虚拟机添加多少交换空间,0表示无交换文件。交换存储是在内存需求超过硬件设备限制时使用的基于磁盘的RAM。

swapFile

string

%USERPROFILE%\AppData\Local\Temp\swap.vhdx

交换虚拟硬盘的绝对Windows路径。

pageReporting

boolean

true

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

guiApplications

boolean*

true

一个布尔值,用于在 WSL 中打开或关闭对 GUI 应用程序 (WSLg) 的支持。 仅适用于Windows 11。

debugConsole

boolean*

false

一个布尔值,用于在 WSL 2 发行版实例启动时打开显示 dmesg内容的输出控制台窗口。 仅适用于Windows 11。

nestedVirtualization

boolean*

true

用于打开或关闭嵌套虚拟化的布尔值,使其他嵌套 VM 能够在 WSL 2 中运行。 仅适用于Windows 11。

vmIdleTimeout

number*

60000

VM 在关闭之前处于空闲状态的毫秒数。 仅适用于Windows 11。

具有 path 值的条目必须是带有转义反斜杠的 Windows 路径,例如:C:\\Temp\\myCustomKernel

具有 size 值的条目必须是后跟单位的大小,例如 8GB512MB

值类型后具有 * 的条目仅在Windows 11可用。

示例 .wslconfig 文件

下面的.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 off 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

参考
Advanced settings configuration in WSL
最后更新时间:2022-09-26
新增systemd的启用方式


评论