修复 Arch Linux WSL2 中无法调用 Windows 程序的问题
目录
什么是 WSL Interop
WSL Interop 是 WSL2 的核心功能之一,允许你在 WSL 终端中直接调用 Windows 可执行文件。例如:
powershell.exe -NoProfile -Command "Get-Process | Select-Object -First 5"
winget.exe search nushell
explorer.exe .Windows 文件系统也通过 /mnt/c/、/mnt/d/ 等路径挂载,可以直接读写。
问题
在 Arch Linux WSL2 中,有时会发现以上命令全部失效,报错为"exec format error"或"未找到命令"。这是因为 WSL Interop 的注册依赖 systemd-binfmt.service,而该服务在 Arch Linux 上会因条件检查未满足而被跳过——Arch 不像 Ubuntu 那样预置 binfmt.d 配置文件。
修复
在 WSL 终端中执行:
sudo mkdir -p /etc/binfmt.d
sudo tee /etc/binfmt.d/wsl-interop.conf > /dev/null << 'EOF'
:WSLInterop:M::MZ::/init:P
EOF然后重启服务:
sudo systemctl restart systemd-binfmt验证
# 检查服务状态,应为 active (exited) 而非 skipped
systemctl status systemd-binfmt
# 检查 WSLInterop 已注册
cat /proc/sys/fs/binfmt_misc/WSLInterop
# 测试调用 Windows 程序
powershell.exe -NoProfile -Command "Write-Output 'interop OK'"该配置文件是持久化的,每次 WSL 启动时自动生效,无需重复操作。