ankostis ankostis - 1 year ago 346
Linux Question

How can a run Windows executable from WSL (Ubuntu) Bash

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

, another
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
files from Windows Bash?[1]

[1] Question asked also in Microsoft's "official" GH support repo.

Answer Source

Native solution

On 19 Oct 2016, Microsfot announced the plans for the official solution, which is based on the almost forgotten binfmt_msc Linux facility for launching binaries. The registration command for the binfmt_misc would 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 through cmd.exe.
  • wrun: call a win-executable synchronously with CreateProcess, and wait to die (not using cmd.exe).
  • wstart: launch a detached (asynchronously) command (with the use of cmd.exe).

In order to use them, you must:

  1. 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.
  2. Use this outbash.exe (wherever you installed it) to start WSL, NOT C:\Windows\System32\bash.exe!
  3. 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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download