wsl.conf
和
.wslconfig
文件用于根据每个分发配置高级设置选项,
wsl.conf
(在所有 WSL 2 分发
.wslconfig
() ) 全局。 本指南将介绍每个设置选项、何时使用每种文件类型、存储文件的位置、示例设置文件和提示。
wsl.conf 和 .wslconfig 之间的区别是什么?
可以通过两种方式配置已安装的 Linux 分发版的设置,这些设置将在每次启动 WSL 时自动应用:
.wslconfig
用于在 WSL 2 上运行的所有已安装分发中
全局
配置设置。
wsl.conf
用于为 WSL 1 或 WSL 2 上运行的 Linux 发行版配置
每个发行版
的设置。
这两种文件类型都用于配置 WSL 设置,但存储文件的位置、配置范围和运行分发版的 WSL 版本都会影响要选择的文件类型。
正在运行的 WSL 版本将影响配置设置。 WSL 2 作为轻型虚拟机 (VM) 运行,因此使用虚拟化设置,允许你控制 (使用 Hyper-V 或 VirtualBox) 时可能熟悉的内存或处理器量。
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 版本 17093 及更高版本。
8 秒规则
必须等待,直到运行 Linux 分发版的子系统完全停止运行并重启,配置设置更新才会显示。 关闭分发 shell 的所有实例后,这通常需要大约 8 秒。
如果启动分发 (即。Ubuntu) 、修改配置文件、关闭分发版,然后重新启动它。 可以假设配置更改已立即生效。 但当前情况并非如此,因为子系统可能仍在运行。 在重新启动之前,必须等待子系统停止,以便有足够的时间来选取更改。 可以通过使用 PowerShell 和以下命令来检查关闭 Linux 发行版 (shell) 后其是否仍在运行:
wsl --list --running
。 如果没有正在运行的分发版,则会收到响应:“没有正在运行的分发版。”现在,可以重启分发,以查看配置更新已应用。
命令
wsl --shutdown
是重启 WSL 2 分发版的快速路径,但它将关闭所有正在运行的分发版,因此请明智地使用 。
wsl.conf 的配置设置
wsl.conf 文件根据每个分发配置设置。
(有关 WSL 2 分发版的全局配置,请参阅
.wslconfig
) 。
wsl.conf 文件支持四个部分:
automount
、
network
、
interop
和
user
。
(按照.ini文件约定建模,密钥在 节下声明,如 .gitconfig files.)
有关
wsl.conf
文件的存储位置的信息,请参阅 wsl.conf。
systemd 支持
许多 Linux 发行版默认运行“systemd”, (包括 Ubuntu) 和 WSL 最近添加了对此系统/服务管理器的支持,因此 WSL 更类似于在裸机计算机上使用你喜欢的 Linux 发行版。 需要 0.67.6+ 版本的 WSL 才能启用 systemd。 使用命令
wsl --version
检查 WSL 版本。 如果需要更新,可以在
Microsoft Store 中获取最新版本的 WSL
。 有关详细信息,请参阅
博客公告
。
若要启用 systemd,请使用 在文本编辑器中打开
wsl.conf
文件,以获取管理员权限,并将以下行添加到
/etc/wsl.conf
:
sudo
[boot]
systemd=true
然后,需要使用 PowerShell 中的 关闭 WSL 分发 wsl.exe --shutdown 以重启 WSL 实例。 分发重启后,systemd 应正在运行。 可以使用 命令进行确认: systemctl list-unit-files --type=service,该命令将显示服务的状态。
自动装载设置
节标签:[automount]
default
boolean
true 导致固定驱动器(即 C:/ 或 D:/)自动装载到 DrvFs 中的 /mnt 下。 false 表示驱动器不会自动装载,但你仍可以手动或通过 fstab 装载驱动器。
mountFsTab
boolean
true 设置启动 WSL 时要处理的 /etc/fstab。 /etc/fstab 是可在其中声明其他文件系统的文件,类似于 SMB 共享。 因此,在启动时,可以在 WSL 中自动装载这些文件系统。
string
/mnt/
设置固定驱动器要自动装载到的目录。 默认情况下,此值设置为 /mnt/,因此 Windows 文件系统 C 驱动器将装载到 /mnt/c/。 如果更改为 /mnt//windir/,则应会看到固定的 C 驱动器已装载到 /windir/c。
以逗号分隔的值列表,例如 uid、gid 等,请参阅下面的自动装载选项
下面列出了自动装载选项值,并追加到默认的 DrvFs 装载选项字符串中。 只能指定特定于 DrvFs 的选项。
自动装载选项作为所有自动装载驱动器的装载选项应用。 若要仅更改特定驱动器的选项,请 /etc/fstab 改用 文件。 通常由装载二进制文件分析成标志的选项不受支持。 如果要显式指定这些选项,则必须在 中包含 /etc/fstab要为其执行此操作的每个驱动器。
自动装载选项
为 Windows 驱动器 (DrvFs) 设置不同的装载选项可以控制为 Windows 文件计算文件权限的方式。 提供了以下选项:
默认情况下,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 驱动器或目录时指定默认区分大小写行为的详细信息,请参阅 区分大小写 页。
节标签:[network]
default
示例 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]
default
Windows 上 25% 的内存大小四舍五入到最接近的 GB
要向 WSL 2 VM 添加的交换空间量,0 表示无交换文件。 交换存储是基于磁盘的 RAM,当内存需求超过硬件设备上的限制时使用。
swapFile
%USERPROFILE%\AppData\Local\Temp\swap.vhdx
交换虚拟硬盘的绝对 Windows 路径。
pageReporting
boolean
默认设置 true 使 Windows 能够回收分配给 WSL 2 虚拟机的未使用内存。
guiApplications
一个布尔值,用于在 WSL 中打开或关闭对 GUI 应用程序 (WSLg) 的支持。 仅适用于Windows 11。
debugConsole
false
一个布尔值,用于在 WSL 2 发行版实例启动时打开显示 dmesg 内容的输出控制台窗口。 仅适用于Windows 11。
nestedVirtualization
用于打开或关闭嵌套虚拟化的布尔值,使其他嵌套 VM 能够在 WSL 2 中运行。 仅适用于Windows 11。
vmIdleTimeout
60000
VM 在关闭之前处于空闲状态的毫秒数。 仅适用于Windows 11。
具有 path 值的条目必须是带有转义反斜杠的 Windows 路径,例如:C:\\Temp\\myCustomKernel
具有 size 值的条目必须是后跟单位的大小,例如 8GB 或 512MB。
值类型后为 * 的条目仅在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
Windows 命令行博客:自动配置 WSL
Windows 命令行博客:Chmod/Chown、DrvFs、文件元数据