如何从 WSL (Ubuntu) Bash 运行 Windows 可执行文件 [英] How can I run a Windows executable from WSL (Ubuntu) Bash
问题描述
随着 2016 年夏季的 Windows 10 周年更新,出现了在新的 Windows Subsystem for Linux (WSL),一个轻量级"虚拟化子系统.
不幸的是,启动 C:WindowsSystem32ash.exe
,另一个 bash
ELF 二进制文件会在 WSL 内部启动一个进程,从那里你无法逃脱!您只能启动其他 ELF 二进制文件.
那么如何从 Windows Bash 执行 *.exe
文件?[1]
[1] 在 Microsoft 的官方"GH 支持存储库中也提出了问题.
Native solution
官方提供的解决方案 Windows 10 Insider Preview 更新 (14951) 基于几乎被遗忘的 binfmt_msc 用于启动二进制文件的 Linux 工具.binfmt_misc 的注册命令如下(其中 /init
是 win 可执行文件的临时 binfmt_misc解释器"):
sudo echo ":WSLInterop:M::MZ::/init:" >/proc/sys/fs/binfmt_misc/register
然后 win-executable 将像常规程序一样启动:
$ export PATH=$PATH:/mnt/c/Windows/System32$记事本.exe$ ipconfig.exe |grep IPv4 |切-d:-f2$ ls -la |findstr.exe foo.txt$ cmd.exe/c 目录
<块引用>
并不是说任何 win 可执行文件都必须驻留在 windows (DrvFs) 文件系统中 - 而不是 Linux 的文件系统 (VolFs) - 以便继承一个合适的 Windows 工作目录.
cbwin 替代方案
在您获得最新版本之前,项目 cbwin 提供了一种解决方法,通过在其中安装 3 个新的 linux 命令WSL:
wcmd
:通过cmd.exe
调用win-executable.wrun
:与CreateProcess
同步调用win-executable,并等待死亡(不使用cmd.exe
).wstart
:启动一个分离的(异步)命令(使用cmd.exe
).
为了使用它们,您必须:
- 安装cbwin:
- 一个新的
outbash.exe
将安装在您的常规 Windows 文件系统中(位于您的%PATH%
中),加上 - WSL 文件系统中的 3 个 linux 命令.
- 一个新的
- 使用这个
outbash.exe
(无论你在哪里安装)来启动 WSL,而不是C:WindowsSystem32ash.exe
! - 使用这些命令之一为任何 win 可执行文件添加前缀,例如
扭动记事本
.
提示:如果使用 wcmd
或 wrun
启动的可执行文件产生任何子项,则这些子项仅在该可执行文件保持活动状态时存活.
换句话说,尝试用 wcmd
启动 notepad.exe
是行不通的,因为 notepad启动 -- 在这种情况下使用 wrun
(同步)或 wstart
(异步).
Along with Windows 10 Anniversary update for summer 2016, came the possibility to run ubuntu binaries inside the new Windows Subsystem for Linux (WSL), a "lightweight" virtualized subsystem.
Unfortunately, launching C:WindowsSystem32ash.exe
, another bash
ELF binary starts a process inside the WSL, from where you cannot escape! You may launch only other ELF binaries.
So how can I execute *.exe
files from Windows Bash?[1]
[1] Question asked also in Microsoft's "official" GH support repo.
Native solution
The official solution provided with Windows 10 Insider Preview Update (14951) is based on the almost forgotten binfmt_msc Linux facility for launching binaries. The registration command for the binfmt_misc would be like this (where /init
is the provisional binfmt_misc "interpreter" for the win-executables):
sudo echo ":WSLInterop:M::MZ::/init:" > /proc/sys/fs/binfmt_misc/register
And then win-executable would be launched like regular programs:
$ export PATH=$PATH:/mnt/c/Windows/System32
$ notepad.exe
$ ipconfig.exe | grep IPv4 | cut -d: -f2
$ ls -la | findstr.exe foo.txt
$ cmd.exe /c dir
Not that any win-executable must reside in the windows (DrvFs) file-system - not on the Linux's file-system (VolFs) - in order to inherit a proper Windows working-directory.
The cbwin alternative
Untill you get the latest build, project cbwin offers a workaround, by installing 3 new linux commands inside WSL:
wcmd
: call a win-executable throughcmd.exe
.wrun
: call a win-executable synchronously withCreateProcess
, and wait to die (not usingcmd.exe
).wstart
: launch a detached (asynchronously) command (with the use ofcmd.exe
).
In order to use them, you must:
- Install cbwin:
- a new
outbash.exe
will be installed in your regular Windows filesystem (somewhere in your%PATH%
), plus - the 3 linux-commands in the WSL filesystem.
- a new
- Use this
outbash.exe
(wherever you installed it) to start WSL, NOTC:WindowsSystem32ash.exe
! - Prefix any win-executables with one of those commands, e.g.
wrun notepad
.
Tip: If the executable launched with wcmd
or wrun
spawns any children, these survive only for as long that executable remains alive.
In other words, trying to start notepad.exe
with wcmd
won't work, because notepad will be killed just after having been launched -- Use wrun
(synchronously) or wstart
(asynchronously) in this case.
这篇关于如何从 WSL (Ubuntu) Bash 运行 Windows 可执行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!