WSL 中的進階設定組態

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

  • 儲存在 /etc 散發套件的目錄中,做為 unix 檔案。
  • 用來設定個別散發的設定。 此檔案中設定的設定只會套用至包含儲存此檔案之目錄的特定 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 發行版本的子系統完全停止執行,並重新啟動組態設定更新才會出現。 關閉散發殼層的所有實例之後,這通常需要大約 8 秒的時間。

如果您啟動散發套件 (。Ubuntu) 、修改組態檔、關閉散發套件,然後重新開機它。 您可能會假設您的設定變更已立即生效。 這不是目前的情況,因為子系統仍然可以執行。 您必須等到子系統停止再重新開機,才能提供足夠的時間來挑選您的變更。 您可以使用 PowerShell 搭配 命令,查看 Linux 發行版本 (殼層) 是否仍在執行中: wsl --list --running 。 如果沒有執行散發套件,您會收到回應:「沒有執行中的散發套件」。您現在可以重新開機散發套件,以查看已套用的組態更新。

此命令 wsl --shutdown 是重新開機 WSL 2 散發套件的快速路徑,但會關閉所有執行中的散發套件,因此請小心使用。

wsl.conf 的組態設定

wsl.conf 檔案會以個別散發為基礎來設定設定。 (如需 WSL 2 散發套件的全域設定,請參閱 .wslconfig) 。

wsl.conf 檔案支援四個區段: automountnetworkinteropuser (模型化.ini檔案慣例之後,金鑰會在 區段下宣告,例如 .gitconfig files.) 如需儲存 wsl.conf 檔案的位置資訊,請參閱 wsl.conf

systemd 支援

許多 Linux 發行版本預設會執行「systemd」 (,包括 Ubuntu) 和 WSL 最近新增了此系統/服務管理員的支援,讓 WSL 更類似于在裸機電腦上使用您最愛的 Linux 發行版本。 您需要 0.67.6+ 版的 WSL 才能啟用系統。 使用 命令 wsl --version 檢查您的 WSL 版本。 如果您需要更新,您可以在 Microsoft Store 中擷取最新版本的 WSL。 深入瞭解 部落格公告

若要啟用 systemd,請在文字編輯器中開啟您的 wsl.conf 檔案,以取得 sudo 系統管理員許可權,並將這幾行新增至 /etc/wsl.conf

[boot]
systemd=true

然後,您必須使用 wsl.exe --shutdown PowerShell 關閉 WSL 散發套件,以重新開機您的 WSL 實例。 一旦您的散發套件重新開機,系統應該正在執行。 您可以使用 命令來確認: systemctl list-unit-files --type=service ,這會顯示服務的狀態。

自動掛接設定

區段標籤:[automount]

索引鍵 value default 附註
已啟用 boolean true true 會導致固定磁片磁碟機 (,也就是 C:/) D:/ 在 底下 /mnt 自動掛接 DrvFs。 false 表示磁片磁碟機不會自動掛接,但您仍然可以手動或透過 fstab 掛接。
mountFsTab boolean true true 會將 /etc/fstab 設定為在 WSL 啟動時處理。 /etc/fstab 是一個檔案,您可以在其中宣告其他檔案系統,例如 SMB 共用。 因此,您可以在啟動時,在 WSL 中自動掛接這些檔案系統。
root 字串 /mnt/ 設定將自動掛接固定磁碟機的目錄。 根據預設,這會設定為 /mnt/ ,因此您的 Windows 檔案系統 C 磁片磁碟機會掛接至 /mnt/c/ 。 如果您變更 /mnt//windir/ ,您應該會看到固定的 C 磁片磁碟機掛接至 /windir/c
選項 以逗號分隔的值清單,例如 uid、gid 等,請參閱下方的自動掛接選項 空字串 自動掛接選項值如下所列,並附加至預設 DrvFs 掛接選項字串。 只能指定 DrvFs 特定選項。

自動掛接選項會套用為所有自動掛接磁片磁碟機的掛接選項。 若要只變更特定磁片磁碟機的選項,請改用 /etc/fstab 檔案。 不支援掛接二進位檔通常會剖析成旗標的選項。 如果您想要明確指定這些選項,您必須在 中包含 /etc/fstab 您想要執行此動作的每個磁片磁碟機。

自動掛接選項

為 Windows 磁碟機 (DrvFs) 設定不同的掛接選項,可以控制 Windows 檔案計算檔案許可權的方式。 可用選項如下:

Key 描述 預設
uid 用於所有檔案擁有者的使用者識別碼 第一次安裝時,WSL 散發版本的預設使用者識別碼 (預設為 1000)
gid 用於所有檔案擁有者的群組識別碼 第一次安裝時,WSL 散發版本的預設群組識別碼 (預設為 1000)
umask 要排除所有檔案和目錄的權限八進位遮罩 000
fmask 要排除所有檔案的權限八進位遮罩 000
dmask 要排除所有目錄的權限八進位遮罩 000
中繼資料 中繼資料是否新增至 Windows 檔案以支援 Linux 系統許可權 disabled
case 判斷將目錄視為區分大小寫,以及使用 WSL 建立的新目錄是否會設定旗標。 如需選項的詳細說明,請參閱 區分大小寫 。 選項包括 offdirforce off

根據預設,WSL 會將 uid 和 gid 設定為預設使用者的值。 例如,在 Ubuntu 中,預設使用者是 uid=1000,gid=1000。 如果使用這個值來指定不同的 gid 或 uid 選項,則會覆寫預設的使用者值。 否則,一律會附加預設值。

使用者檔案建立模式遮罩 (umask) 設定新建立檔案的許可權。 預設值為 022,只有您可以寫入資料,但任何人都可以讀取資料。 您可以變更值以反映不同的許可權設定。 例如, umask=077 變更完全私人的許可權,其他使用者無法讀取或寫入資料。 若要進一步指定許可權,也可以使用 fmask (檔案) 和 dmask (目錄) 。

注意

許可權遮罩會在套用至檔案或目錄之前,先執行邏輯 OR 作業。

什麼是 DrvFs?

DrvFs 是 WSL 的檔案系統外掛程式,其設計目的是支援 WSL 與 Windows 檔案系統之間的互通性。 DrvFs 可讓 WSL 在 /mnt 下掛接支援檔案系統的磁片磁碟機,例如 /mnt/c、/mnt/d 等。如需在掛接 Windows 或 Linux 磁片磁碟機或目錄時指定預設區分大小寫行為的詳細資訊,請參閱 區分大小寫 頁面。

網路設定

區段標籤:[network]

索引鍵 value default 附註
generateHosts boolean true true 會設定 WSL 以產生 /etc/hostshosts 檔案包含主機名稱對應 IP 位址的靜態對應。
generateResolvConf boolean true true 會設定 WSL 以產生 /etc/resolv.confresolv.conf 包含DNS 清單,可以將指定主機名稱解析為其 IP 位址。
hostname 字串 Windows 主機名稱 設定要用於 WSL 散發的主機名稱。

Interop 設定

區段標籤:[interop]

這些選項可在測試人員組建 17713 和更新版本中使用。

索引鍵 value default 附註
已啟用 boolean true 設定此機碼將決定 WSL 是否支援啟動 Windows 程序。
appendWindowsPath boolean true 設定此機碼將決定 WSL 是否會將 Windows 路徑元素新增至 $PATH 環境變數。

使用者設定

區段標籤:[user]

這些選項可在組建 18980 和更新版本中取得。

索引鍵 value default 附註
default 字串 第一次執行時建立的初始使用者名稱 設定此機碼會指定第一次啟動 WSL 會話時,要以身分執行的使用者。

開機設定

開機設定僅適用于 Windows 11 和 Server 2022。

區段標籤:[boot]

索引鍵 value default 附註
命令 字串 "" 您想要在 WSL 實例啟動時執行的命令字串。 此命令會以根使用者身分執行。 例如: service docker start

範例 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]

索引鍵 value default 附註
核心 (kernel) path Microsoft 建置的核心提供收件匣 自訂 Linux 核心的絕對 Windows 路徑。
記憶體 {1}size{2} Windows 或 8GB 上總記憶體的 50%,在 20175 之前的組建上:Windows 上總記憶體的 80% 要指派給 WSL 2 VM 的記憶體量。
處理器 number Windows 上的相同邏輯處理器數目 要指派給 WSL 2 VM 的邏輯處理器數目。
localhostForwarding boolean true 布林值,指定 WSL 2 VM 中系結至萬用字元或 localhost 的埠是否應該透過 從主機 localhost:port 連線。
kernelCommandLine 字串 Blank 其他核心命令列引數。
safeMode boolean false 在「安全模式」中執行 WSL,其會停用許多功能,並用來復原處於不良狀態的散發。 僅適用于 Windows 11 和 WSL 0.66.2+ 版。
swap {1}size{2} Windows 上 25% 的記憶體大小四捨五入到最接近的 GB 要新增至 WSL 2 VM 的交換空間,沒有交換檔案的 0。 當記憶體需求超過硬體裝置的限制時,交換儲存體是以磁片為基礎的 RAM。
swapFile path %USERPROFILE%\AppData\Local\Temp\swap.vhdx 交換虛擬硬碟的絕對 Windows 路徑。
pageReporting boolean true 預設 true 設定可讓 Windows 回收配置給 WSL 2 虛擬機器的未使用記憶體。
guiApplications 布林* true 布林值,以開啟或關閉 WSL 中 WSLg) GUI (應用程式的支援。 僅適用于Windows 11。
debugConsole 布林* false 布林值開啟輸出主控台視窗,顯示 WSL 2 散發版本實例開始時的內容 dmesg 。 僅適用于Windows 11。
nestedVirtualization 布林* true 布林值可開啟或關閉巢狀虛擬化,讓其他巢狀 VM 可在 WSL 2 內執行。 僅適用于Windows 11。
vmIdleTimeout 數量* 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

其他資源