这是本节的多页打印视图。 点击此处打印.

返回本页常规视图.

WSL

这是关注WSL的实战经验

    WSL安装

    https://learn.microsoft.com/zh-cn/windows/wsl/install

    WSL的配置

    • 详见: https://learn.microsoft.com/zh-cn/windows/wsl/wsl-config

    • 扫盲 .wslconfig: 位于 window系统 %UserProfile%.wslconfig 配置了WSL中的功能,为WSL2 提供支持的虚拟机设置(RAM、内核数、CPU数等)。它将设置所有的WSL分发版的通用设置 wsl.conf: 位于 具体分发版的 /etc/wsl.conf 。设置例如启动选项、DrvFs 自动装载、网络、与 Windows 系统的互作性、系统使用情况和默认用户

    wsl.conf的配置示例

    [boot]
    # 此选项用于开启systemd功能,开启后可以使用 sudo systemctl 命令 服务 来对服务进行管理
    # 默认为 false,如果开启, 需要在 PowerShell 中执行 wsl --shutdown,然后重新启动 WSL 才能生效
    systemd=true
    
    # 你希望在 WSL 实例启动时运行的命令字符串。 此命令以根用户身份运行。 例如service docker start。
    # 启动设置仅在 Windows 11 和 Server 2022 上可用。
    command=service docker start
    
    [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.
    #true 导致固定驱动器(即 C:/ 或 D:/)自动装载到 DrvFs 中的 /mnt 下。 false 表示驱动器不会自动装载,但你仍可以手动或通过 fstab 装载驱动器。 default true
    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. 
    #设置固定驱动器要自动装载到的目录。 默认情况下,这设置为 /mnt/ ,因此Windows文件系统 C 驱动器装载到 /mnt/c/ 。 如果更改为 /mnt//windir/ ,应会看到固定 C 驱动器装载到 /windir/c 。default /mnt/
    root = /mnt/
    
    # DrvFs-specific options can be specified. 
    #下面列出了 automount 选项值,并追加到默认的 DrvFs 装载选项字符串中。 只能指定特定于 DrvFs 的选项。default "" 
    # 以逗号分隔的值列表(如 uid、gid 等),请参阅下面的自动装载选项
    # key        desc                                                                default
    # uid        用于所有文件的所有者的用户 ID                                          首次安装时,WSL (的默认用户 ID 默认为 1000) 
    # gid        用于所有文件的所有者的组 ID                                            首次安装时,WSL (的默认组 ID 默认为 1000) 
    # umask      要对所有文件和目录排除的权限的八进制掩码                                 000
    # fmask      要对所有文件排除的权限的八进制掩码                                      000
    # dmask      要对所有目录排除的权限的八进制掩码                                      000
    # metadata   是否将元数据添加到 Windows 文件以支持 Linux 系统权限                    disabled
    # case       确定被视为区分大小写的目录以及使用 WSL 创建的新目录是否将设置标志。        off
    #            有关选项的详细说明,请参阅区分大小写。
    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.
    #true 设置启动 WSL 时要处理的 /etc/fstab。 /etc/fstab 是可在其中声明其他文件系统的文件,类似于 SMB 共享。 因此,在启动时,可以在 WSL 中自动装载这些文件系统。 default true
    mountFsTab = true
    
    
    # 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]
    #设置此键可确定 WSL 是否支持启动 Windows 进程。default true
    enabled = false
    #设置此键可确定 WSL 是否会将 Windows 路径元素添加到 $PATH 环境变量。default true
    appendWindowsPath = false
    
    # Set the user when launching a distribution with WSL.
    [user]
    #设置此键指定在首次启动 WSL 会话时以哪个用户身份运行。default 首次运行时创建的初始用户名
    default = root 
    
    • .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
    # 要分配给 WSL 2 VM 的内存量。 default:Windows 上总内存的 50% 或 8GB,以较小者为准;在 20175 之前的版本上:Windows 上总内存的 80%
    memory=4GB 
    
    # Sets the VM to use two virtual processors
    #要分配给 WSL 2 VM 的处理器数量。 default: Windows 上相同数量的处理器
    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
    #自定义 Linux 内核的绝对 Windows 路径。default:Microsoft 内置内核提供的收件箱
    kernel=C:\\temp\\myCustomKernel
    
    # Sets additional kernel parameters, in this case enabling older Linux base images such as Centos 6
    #其他内核命令行参数。default:空白
    kernelCommandLine = vsyscall=emulate
    
    # Sets amount of swap storage space to 8GB, default is 25% of available RAM
    # 要向 WSL 2 VM 添加的交换空间量,0 表示无交换文件。 交换存储是内存需求超过硬件设备限制时使用的基于磁盘的 RAM。 default:Windows 上 25% 的内存大小四舍五入到最接近的 GB
    swap=8GB
    
    # Sets swapfile path location, default is %USERPROFILE%\AppData\Local\Temp\swap.vhdx
    # 交换虚拟硬盘的绝对 Windows 路径。 default: %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
    #默认设置 true 允许Windows回收分配给 WSL 2 虚拟机的未使用内存。
    pageReporting=false
    
    # Turn off default connection to bind WSL 2 localhost to Windows localhost
    #一个布尔值,用于指定绑定到 WSL 2 VM 中的通配符或 localhost 的端口是否应可通过 localhost:port 从主机连接。 default:true
    localhostforwarding=true
    
    # Disables nested virtualization
    # 启用或关闭嵌套虚拟化的布尔值,使其他嵌套 VM 在 WSL 2 中运行。 仅适用于Windows 11。 default : true
    nestedVirtualization=false
    
    # Turns on output console showing contents of dmesg when opening a WSL 2 distro for debugging
    #一个布尔值,用于在 WSL 2 发行版实例启动时打开显示 dmesg 内容的输出控制台窗口。 仅适用于Windows 11。default: false
    debugConsole=true
    
    #一个布尔值,用于在 WSL 中打开或关闭对 GUI 应用程序 (WSLg) 的支持。 仅适用于Windows 11。 default : true
    guiApplications=true
    
    #VM 在关闭之前处于空闲状态的毫秒数。 仅适用于Windows 11。 default 60000
    vmIdleTimeout=60000