WSL 互通 — Windows 與 Linux 整合

Windows 子系統 Linux 版(WSL)提供 Windows 與 Linux 之間的雙向互通。 你可以從 Windows 程序啟動 Linux 指令,在 Linux shell 內執行 Windows 執行檔,跨兩個檔案系統分享檔案,並轉發網路連線。 本頁總結了關鍵互操作模式及其取捨。

Important

WSL 互通性要求安裝 WSL 並至少註冊一個 Linux 發行版。 不要假設 WSL 存在——在呼叫wsl.exe\\wsl$\或存取路徑前,務必先確認。 在未安裝 WSL 的系統中, wsl.exe 會回傳非零的退出代碼並出現錯誤訊息,且 \\wsl$\ UNC 路徑無法解析。

命令列互通

從 Windows 執行 Linux 指令

使用 wsl.exe,即可從任何 Windows Shell(PowerShell、命令提示字元或 Windows 終端機)執行 Linux 命令:

# Run a single command in the default distribution
wsl ls -la /home

# Run a command in a specific distribution
wsl -d Ubuntu-22.04 -- cat /etc/os-release

# Pipe Windows output into a Linux command
ipconfig | wsl grep "IPv4"

從 Linux 執行 Windows 執行檔

在 WSL Shell 中,請以完整名稱呼叫任何 Windows 可執行檔(包括 .exe 副檔名):

# Open File Explorer in the current Linux directory
explorer.exe .

# Run a PowerShell command from Linux
powershell.exe -Command "Get-Date"

# Pipe Linux output into a Windows command
cat /etc/hosts | clip.exe

Note

從 WSL 呼叫的 Windows 執行檔使用 Windows PATH。 .exe 擴充功能是必要的——沒有它,Shell 會尋找 Linux 二進位檔。

偵測 WSL 是否可用

在您的應用程式或指令碼中使用 WSL 互通操作之前,請先確認其可用性:

# PowerShell: Check if wsl.exe exists and WSL is functional
try {
    $wslStatus = wsl --status 2>&1
    if ($LASTEXITCODE -eq 0) {
        Write-Host "WSL is available"
    } else {
        Write-Host "WSL is installed but not functional"
    }
} catch {
    Write-Host "WSL is not installed"
}
// C#: Check WSL availability before use
var wslPath = Path.Combine(
    Environment.GetFolderPath(Environment.SpecialFolder.System), "wsl.exe");

if (!File.Exists(wslPath))
{
    // WSL is not installed — handle gracefully
    return;
}

var process = Process.Start(new ProcessStartInfo
{
    FileName = wslPath,
    Arguments = "--status",
    RedirectStandardOutput = true,
    UseShellExecute = false,
    CreateNoWindow = true
}) ?? throw new InvalidOperationException("Failed to start wsl.exe process.");

process.WaitForExit();
if (process.ExitCode != 0)
{
    // WSL is installed but no distribution is registered
}

檔案系統互通

從 Windows 存取 Linux 檔案

使用 \\wsl$\ (或\\wsl.localhost\) UNC 路徑從 Windows 存取 Linux 檔案系統:

\\wsl$\Ubuntu-22.04\home\username\project
\\wsl.localhost\Ubuntu-22.04\home\username\project

Important

\\wsl$\該路徑僅在 WSL 實例執行時可用。 若分配器關閉,路徑不會被解析。 先使用 wsl -d <distro-name> 啟動發行版,或使用 wsl.localhost,即可在 Windows 11 上自動啟動該發行版。

從 Linux 存取 Windows 檔案

Windows 磁碟機預設會掛載在 /mnt/ 下:

# Access C: drive
ls /mnt/c/Users/username/Documents

# Access D: drive
ls /mnt/d/projects

效能考量

Operation Performance Recommendation
Linux 程序讀取 Linux 檔案 (/home/... Fast(原生 ext4) 這裡保留專案檔案以支援基於 Linux 的工具
Windows 讀取 Windows 檔案的過程 (C:\... 快速(原生 NTFS) 這裡保留專案檔案以支援 Windows 基礎工具
Linux 程序讀取 Windows 檔案 (/mnt/c/... 緩慢(跨檔案系統 9P 通訊協定) 避免用於建置系統、node_modulesGit 儲存庫
Windows 程序讀取 Linux 檔案 (\\wsl$\... 緩慢(跨檔案系統的 9P 通訊協定) 適用於偶爾存取檔案,不適合用於密集迴圈

小提示

最常見的效能錯誤是在執行 Linux 建置工具(npm、cargo、make)時,將專案檔案儲存在 Windows 檔案系統/mnt/c/...()上。 這會造成相當大的 I/O 開銷。 相反地,當你主要使用 Linux 工具時,應該將儲存庫複製到 Linux 檔案系統(~/projects/)。

路徑翻譯

在 WSL 中使用 wslpath 在 Windows 和 Linux 路徑之間進行轉換:

# Windows path → Linux path
wslpath "C:\Users\username\file.txt"
# Output: /mnt/c/Users/username/file.txt

# Linux path → Windows path
wslpath -w /home/username/file.txt
# Output: \\wsl.localhost\Ubuntu-22.04\home\username\file.txt

# Linux path → Windows path (with drive letter format)
wslpath -m /mnt/c/Users/username/file.txt
# Output: C:/Users/username/file.txt

網路互通性

本地主機轉發

預設情況下,WSL 2 會將 Linux 訪客的埠口轉發到 localhost Windows 上。 在 WSL 中於連接埠 3000 上執行的伺服器可從 Windows 透過 http://localhost:3000 存取。

Note

在大多數 Windows 版本中,Localhost 轉發可自動從 Windows 轉發到 WSL。 不過,從 WSL 連接到 Windows 主機伺服器需要 Windows 主機 IP(可以在舊的 WSL 設定中找到cat /etc/resolv.conf | grep nameserver,或如果你啟用鏡像網路,直接使用localhost)。

鏡像網路模式(Windows 11)

Windows 11(版本 22H2 及以上)支援鏡像網路,WSL 與 Windows 共享相同的網路堆疊。 在 .wslconfig 中啟用:

# %USERPROFILE%\.wslconfig
[wsl2]
networkingMode=mirrored

使用鏡像模式:

  • WSL 與 Windows 共用同一 IP 位址
  • 兩者皆可與 localhost 上的服務進行雙向通訊
  • Windows 上的 VPN 連線在 WSL 中自動提供
  • IPv6 在 WSL 內部運作

環境變數共享

使用 變WSLENV數將 Windows 環境變數傳入 WSL:

# Windows: Share GOPATH with WSL (translate the path)
$env:WSLENV = "GOPATH/p"
$env:GOPATH = "C:\Users\username\go"
wsl echo $GOPATH
# Output: /mnt/c/Users/username/go

/p旗標會將 Windows 路徑轉換為 Linux 格式。 其他旗幟: /l (冒號分隔列表)、 /u (僅 WSL→Windows)、 /w (僅 Windows→WSL)。

常見的錯誤

小提示

大型語言模型與程式碼產生器常犯的錯誤:

  1. 假設 \\wsl$\ 總是可存取 ——路徑僅在目標 WSL 分布運行時才會解析。 若程式碼在未先啟動發行版的情況下開啟 \\wsl$\ 路徑,會因網路路徑錯誤而失敗。
  2. 未處理未安裝 WSL 的情況——許多電腦(尤其是 Windows Server 或由企業管理的電腦)都沒有 WSL。 使用前務必確認 wsl.exe 是否存在。
  3. 專案檔案儲存在錯誤的檔案系統——/mnt/c/用於 Linux 專案或 \\wsl$\ Windows 專案,會因 9P 檔案系統轉換而嚴重降低 I/O 效能。
  4. 硬編碼 /mnt/c/ — 掛載點可在 /etc/wsl.conf 中設定(透過 [automount] root)。 使用 wslpath 進行可靠的路徑轉譯。
  5. 從 WSL 呼叫 Windows 程式時忘記 .exe——notepad 行不通;你需要 notepad.exe。 這是 Windows 專用文件中常見的複製貼上錯誤。
  6. 假設網路拓撲 ——WSL 2 預設(NAT)模式與鏡像模式的網路行為不同。 在一個開發者的機器上運作的程式碼,可能會在另一個 .wslconfig 開發者的設定下失敗。
  7. 將 WSL 1 的假設套用於 WSL 2 — WSL 1 共用 Windows 網路堆疊,並可直接存取檔案系統。 WSL 2 使用輕量虛擬機,搭配虛擬網路卡與 9P 檔案分享。 效能特性與網路配置不同。