WSL: [WSL2][systemd][interop] Unable to Execute Windows Binary when systemd enabled

Version

0.67.6.0

WSL Version

  • WSL 2
  • WSL 1

Kernel Version

Linux version 5.15.62.1-microsoft-standard-WSL2 (oe-user@oe-host) (x86_64-msft-linux-gcc (GCC) 9.3.0, GNU ld (GNU Binutils) 2.34.0.20200220) #1 SMP Wed Aug 24 22:24:20 UTC 2022

Distro Version

Ubuntu 22.04

Other Software

No response

Repro Steps

  1. Edit /etc/wsl.conf, enable systemd support. (https://devblogs.microsoft.com/commandline/systemd-support-is-now-available-in-wsl/)
  2. Run wsl.exe --shutdown in wsl2 shell, and reopen the shell.
  3. Run wsl.exe --version or some other PE binaries, and shell will warn you that the “exec format error” or “MZ: not found”

Expected Behavior

Able to execute any valid PE binaries.

Actual Behavior

“exec format error” or “MZ: not found”

Diagnostic Logs

$ execve("/mnt/c/WINDOWS/system32/calc.exe", ["calc.exe"], 0x7ffc12cdd038 /* 37 vars */) = -1 ENOEXEC (Exec format error)
strace: exec: Exec format error
+++ exited with 1 +++
$ echo $WSL_INTEROP
/run/WSL/410_interop

$ ll /run/WSL
total 0
drwxrwxrwx  2 root root 100  9月 22 12:29 ./
drwxr-xr-x 26 root root 760  9月 22 12:29 ../
srwxrwxrwx  1 root root   0  9月 22 12:29 2_interop=
srwxrwxrwx  1 root root   0  9月 22 12:29 410_interop=
srwxrwxrwx  1 root root   0  9月 22 12:29 549_interop=

$ ll /proc/sys/fs/binfmt_misc/
total 0
drwxr-xr-x 2 root root 0  9月 22 12:29 ./
dr-xr-xr-x 1 root root 0  9月 22 12:29 ../
-rw-r--r-- 1 root root 0  9月 22 12:29 llvm-12-runtime.binfmt
-rw-r--r-- 1 root root 0  9月 22 12:29 llvm-14-runtime.binfmt
-rw-r--r-- 1 root root 0  9月 22 12:29 python2.7
-rw-r--r-- 1 root root 0  9月 22 12:29 python3.10
--w------- 1 root root 0  9月 22 12:29 register
-rw-r--r-- 1 root root 0  9月 22 12:29 status
$cat /proc/sys/fs/binfmt_misc/WSLInterop
cat: /proc/sys/fs/binfmt_misc/WSLInterop: No such file or directory

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 57
  • Comments: 57 (2 by maintainers)

Commits related to this issue

Most upvoted comments

Hello everyone, thanks to your comments, I was able to solve the problem that my Windows executables were not working after the activation of systemd, just by adding the above mentioned entry to the WSLInterop.conf.

Here is a one-liner for you:

sudo sh -c 'echo :WSLInterop:M::MZ::/init:PF > /usr/lib/binfmt.d/WSLInterop.conf'

After wsl --shutdown and logging back in, now next to systemd, explorer.exe and the like are finally working again! ✓ 🥳

(tested on WSL2 @ Ubuntu22.04 and Debian bookworm/sid)

Hi,

I’m using https://github.com/yuk7/ArchWSL and enabling systemd reliably broke the ability to execute Windows applications from WSL. I resolved the issue by generating a file in /usr/lib/binfmt.d/WSLInterop.conf with the following content:

:WSLInterop:M::MZ::/init:PF

Also I removed mono.conf from the same directory as they might create a conflicting mapping. After running sudo systemctl restart systemd-binfmt, my executables are working now.

I’ve tried several binfmt entries but most of the entries I found online did not include the ‘PF’ flags and resulted in an error.

Systemd does that, as those of us who’ve futzed with alternative systemd solutions in the past have come to know. The solution-slash-workaround is to put this file:

https://github.com/arkane-systems/bottle-imp/blob/master/othersrc/usr-lib/binfmt.d/WSLInterop.conf

in your /usr/lib/binfmt.d .

有什么解决方案吗,我也碰到这个问题了

For me the binfmt went missing every docker run for some reason. I do not know the reason but the fix is obvious - either restart binfmt service or register the format using sudo echo ":WSLInterop:M::MZ::/init:PF" > /proc/sys/fs/binfmt_misc/register

I got tired of manually adding this pre-docker so I fight fire with fire and I made systemd service and path monitor that will do the same if WSLinterop file are missing. Seems to work so far, sharing so maybe some of you will benefit:

/etc/systemd/system/wslinterop-monitor.path

[Unit]
Description=Monitor for WSLInterop
After=systemd-binfmt.service

[Path]
PathChanged=/proc/sys/fs/binfmt_misc/WSLInterop
Unit=wslinterop-force.service
TriggerLimitIntervalSec=0

[Install]
WantedBy=multi-user.target

/etc/systemd/system/wslinterop-force.service

[Unit]
Description=Force WSLInterop
After=systemd-binfmt.service
ConditionPathExists=!/proc/sys/fs/binfmt_misc/WSLInterop

[Service]
Type=oneshot
ExecStart=echo ":WSLInterop:M::MZ::/init:PF"
StandardOutput=file:/proc/sys/fs/binfmt_misc/register

[Install]
WantedBy=multi-user.target

Then:

systemctl daemon-reload
systemctl enable wslinterop-force.service wslinterop-monitor.path
systemctl start wslinterop-force.service wslinterop-monitor.path

Hello everyone, thanks to your comments, I was able to solve the problem that my Windows executables were not working after the activation of systemd, just by adding the above mentioned entry to the WSLInterop.conf.

Here is a one-liner for you:

sudo sh -c 'echo :WSLInterop:M::MZ::/init:PF > /usr/lib/binfmt.d/WSLInterop.conf'

After wsl --shutdown and logging back in, now next to systemd, explorer.exe and the like are finally working again! ✓ 🥳

(tested on WSL2 @ Ubuntu22.04 and Debian bookworm/sid)

@freckhard Can confirm this worked for me as well.

Hi,

I’m using yuk7/ArchWSL and enabling systemd reliably broke the ability to execute Windows applications from WSL. I resolved the issue by generating a file in /usr/lib/binfmt.d/WSLInterop.conf with the following content:

:WSLInterop:M::MZ::/init:PF

Also I removed mono.conf from the same directory as they might create a conflicting mapping. After running sudo systemctl restart systemd-binfmt, my executables are working now.

I’ve tried several binfmt entries but most of the entries I found online did not include the ‘PF’ flags and resulted in an error.

Thank you so much for posting this! ❤️ I am in the same situation. My executables are now working, though the appendWindowsPath directive still isn’t adding them to the system PATH. But using a fully qualified path like /mnt/c/Windows/System32/clip.exe works 🤔

Hi, i have the same problem and the file: https://github.com/arkane-systems/bottle-imp/blob/master/othersrc/usr-lib/binfmt.d/WSLInterop.conf not exists anymore. Where i can find it? Thasnk

Hello everyone, thanks to your comments, I was able to solve the problem that my Windows executables were not working after the activation of systemd, just by adding the above mentioned entry to the WSLInterop.conf.

Here is a one-liner for you:

sudo sh -c 'echo :WSLInterop:M::MZ::/init:PF > /usr/lib/binfmt.d/WSLInterop.conf'

After wsl --shutdown and logging back in, now next to systemd, explorer.exe and the like are finally working again! ✓ 🥳

(tested on WSL2 @ Ubuntu22.04 and Debian bookworm/sid)

Fixed it for me thanks

I ran into the same issue on arch linux after install dotnet, notepad.exe cannot run, uninstall dotnet does not work. Then did google search, found page here, tried the one liner fix: sudo sh -c ‘echo :WSLInterop:M::MZ::/init:PF > /usr/lib/binfmt.d/WSLInterop.conf’

now works perfectly, including vscode. Just want to post and hope this would also help others.

None of the above worked for me. However, on WSL2 running Ubuntu 20.04.04 LTS this worked:

sudo su -
echo ":WSLInterop:M::MZ::/init:" > /proc/sys/fs/binfmt_misc/register

Exit WSL on Powershell via wsl --shutdown and then restart WSL.

The solution was sourced from here: https://github.com/microsoft/WSL/issues/10363#issuecomment-1665839431 credits to @martin-rueegg

This was reported on systemd as systemd/systemd#28126. @poettering says the following there:

wsl really should add their entry to /run/binfmt.d/ before invoking systemd, otherwise this will always conflict because we never know whether an entry we find in the kernel should be deleted because we can’t find any config for it, or if it should stay because it was inherited down.

I don’t think we should change anything in systemd here. On systems where systemd-binfmt is enabled the subsystem is kinda owned by it, and you cannot have multiple owners of it without stepping on each others toes.

Can you file a bug against WSL asking them to fix that? Fix should be simple: just provide whatever they want to provide in a drop-in /run/binfmt.d/ from their agent and things should be good.

_Originally posted by @poettering in https://github.com/systemd/systemd/issues/28126#issuecomment-1603028661_

Hi!

This also happen to me an VSCode, yesterday I was able to work fine with VS Code and WSL2 but today I got this error:

/mnt/c/Program Files/Microsoft VS Code/bin/code: 61: /mnt/c/Program Files/Microsoft VS Code/Code.exe: Exec format error

As other above I got the Exec format error

This happened to me now as well after installing another Ubuntu distribution. I had systemd enabled and WSLInterop running without issues so far. Now suddenly VSCode integration and git-credential-manger interop broke. Also able to fix it with @freckhard 's solution. Thanks a lot!

Hello everyone, thanks to your comments, I was able to solve the problem that my Windows executables were not working after the activation of systemd, just by adding the above mentioned entry to the WSLInterop.conf.

Here is a one-liner for you:

sudo sh -c 'echo :WSLInterop:M::MZ::/init:PF > /usr/lib/binfmt.d/WSLInterop.conf'

After wsl --shutdown and logging back in, now next to systemd, explorer.exe and the like are finally working again! ✓ 🥳

(tested on WSL2 @ Ubuntu22.04 and Debian bookworm/sid)

Huge thanks to @freckhard! It works on my debian 12(bookworm) WSL instance.

(copying my comment from https://github.com/microsoft/WSL/issues/7181#issuecomment-1624024171 since this appears to be the main open “dupe target” for this issue)

I think this is due to https://github.com/systemd/systemd/blob/b9b0f338be2f63af96d295e4eaf9e8a8889190bb/src/binfmt/binfmt.c#L246-L251 – effectively, systemd-binfmt assumes it must be managing all of the registrations for the system, which seems like a problem even for systemd-nspawn, for example. 😅

I used sudo systemctl mask systemd-binfmt.service to fix this (telling systemd explicitly to no longer assume it manages this). The consequence of this is that packages like qemu-user-static that also will try to register might not do so, but I’m also running Docker Desktop so I don’t need that (what helped me figure this out is that its QEMU rules also get wiped out by this).

Edit: also related: https://github.com/microsoft/WSL/issues/8203 👀

Also works for me, thank you so much!

Hello everyone, thanks to your comments, I was able to solve the problem that my Windows executables were not working after the activation of systemd, just by adding the above mentioned entry to the WSLInterop.conf.

Here is a one-liner for you:

sudo sh -c 'echo :WSLInterop:M::MZ::/init:PF > /usr/lib/binfmt.d/WSLInterop.conf'

After wsl --shutdown and logging back in, now next to systemd, explorer.exe and the like are finally working again! ✓ 🥳

(tested on WSL2 @ Ubuntu22.04 and Debian bookworm/sid)

Hi,

I’m using https://github.com/yuk7/ArchWSL and enabling systemd reliably broke the ability to execute Windows applications from WSL. I resolved the issue by generating a file in /usr/lib/binfmt.d/WSLInterop.conf with the following content:

:WSLInterop:M::MZ::/init:PF

Also I removed mono.conf from the same directory as they might create a conflicting mapping. After running sudo systemctl restart systemd-binfmt, my executables are working now.

I’ve tried several binfmt entries but most of the entries I found online did not include the ‘PF’ flags and resulted in an error.

I tried using this solution (for Kali) and while the window launches it is black. Is there anything else that needs to be done?

Good pointer @cerebrate, thanks heaps! That helped.

I compared the distro that worked out of the box and the distro that didn’t and the difference was existing config files in “/usr/lib/binfmt.d”

  • Stock Ubuntu out of the box with nothing in “/usr/lib/binfmt.d” works fine
  • Kali Linux with plenty of packages installed and thus several config files in “/usr/lib/binfmt.d” does not work

Ubuntu on its own works fine. Kali doesn’t work and also breaks Ubuntu. Deleting everything under “/usr/lib/binfmt.d” in the Kali distro fixes Kali (and stops breaking Ubuntu). Adding your file to “/usr/lib/binfmt.d” alongside the existing files fixes Kali (and stops breaking Ubuntu).

Thanks for that @cerebrate, much appreciate it.

Issue is still prevalent, hope this gets resolved soon. I had to disable systemd to link WSL ubuntu (22.04.02) to windows git credentials.

Will WSLInterop.conf be added by default in future releases? Would like to know if I need to add it to my setup playbook or not. Looks like the systemd guys won’t be changing anything on their end.

Following @tianon’s instructions worked for me:

sudo systemctl mask systemd-binfmt.service

I then did wsl --shutdown and restarted. Running cmd.exe from WSL or anything else seems to work fine now.

This seems like the most sensible workaround as it disables the service that is removing the WSLInterop capabilities.

The sudo update-binfmts --disable cli workaround didn’t work for me. That might be something that applies to a different root cause.

The WSLInrerop.conf file solution did not work for me

sanr@GEO-00099386:/~ code .
run-detectors: unable to find an interpreter for /mnt/c/Program Files/Microsoft VS Code/Code.exe

In my case what solved was disabling the cli interpreter using the command: sudo update-binfmts --disable cli from here https://github.com/microsoft/WSL/issues/4567#issuecomment-680034755

and after reboot windows executable work fine again. All that with (unchanged):

@g2flyer When you say “All that with (unchanged):” do you mean you didn’t have to run the workaround?

sudo sh -c 'echo :WSLInterop:M::MZ::/init:PF > /usr/lib/binfmt.d/WSLInterop.conf'

I suspected this had something to do with the Windows update, but restarting my machine and doing wsl --shutdown never got my exe’s working until I added WSLInterop.conf

Thanks guys. This worked. It is odd though, it was working fine last week, and then this week it just stopped. I turned on systemd a while ago to deal with this issue: https://github.com/microsoft/WSL/issues/8204

我是Ubuntu 20.04,会报如下的错误: ``root@lhrxxt:/mnt/f/pwsh# cmd.exe <3>WSL (996) ERROR: UtilConnectUnix:511: connect failed 2 root@lhrxxt:/mnt/f/pwsh# root@lhrxxt:/mnt/f/pwsh# ll /usr/lib/binfmt.d/ total 12 drwxr-xr-x 2 root root 4096 Sep 26 15:30 ./ drwxr-xr-x 122 root root 4096 Sep 14 10:57 …/ -rw-r–r-- 1 root root 27 Sep 26 15:30 WSLInterop.conf root@lhrxxt:/mnt/f/pwsh# cmd.exe <3>WSL (1000) ERROR: UtilConnectUnix:511: connect failed 2 root@lhrxxt:/mnt/f/pwsh# root@lhrxxt:/mnt/f/pwsh#

The binfmt_misc handler for WSL2 to run Windows binaries either is getting overwritten by systemd or is never set:

systemd enabled

adam@ADAM-HOMEDESK /p/s/f/binfmt_misc> ls -lah
total 0
drwxr-xr-x 2 root root 0 Sep 22 12:20 ./
dr-xr-xr-x 1 root root 0 Sep 22 12:20 ../
-rw-r--r-- 1 root root 0 Sep 22 12:20 llvm-13-runtime.binfmt
-rw-r--r-- 1 root root 0 Sep 22 12:20 llvm-14-runtime.binfmt
-rw-r--r-- 1 root root 0 Sep 22 12:20 python3.10
--w------- 1 root root 0 Sep 22 12:20 register
-rw-r--r-- 1 root root 0 Sep 22 12:20 status

systemd disabled

adam@ADAM-HOMEDESK /p/s/f/binfmt_misc> ls -lah
total 0
drwxr-xr-x 2 root root 0 Sep 22 12:21 ./
dr-xr-xr-x 1 root root 0 Sep 22 12:21 ../
--w------- 1 root root 0 Sep 22 12:21 register
-rw-r--r-- 1 root root 0 Sep 22 12:21 status
-rw-r--r-- 1 root root 0 Sep 22 12:21 WSLInterop

I don’t think it is by design @WitherZuo. I have both systemd and interop working temporarily in Ubuntu but it stops working when I start another WSL distro. Once I shutdown WSL and start Ubuntu on its own it works again.

Sorry, file moved in the 1.0 release. Try here:

https://github.com/arkane-systems/bottle-imp/blob/v0.13.0/othersrc/usr-lib/binfmt.d/WSLInterop.conf

Systemd does that, as those of us who’ve futzed with alternative systemd solutions in the past have come to know. The solution-slash-workaround is to put this file:

https://github.com/arkane-systems/bottle-imp/blob/master/othersrc/usr-lib/binfmt.d/WSLInterop.conf

in your /usr/lib/binfmt.d .

Worked for me on Win11 Pro Preview ( file now at commit 2742d2 )

Hello everyone, thanks to your comments, I was able to solve the problem that my Windows executables were not working after the activation of systemd, just by adding the above mentioned entry to the WSLInterop.conf.

Here is a one-liner for you:

sudo sh -c 'echo :WSLInterop:M::MZ::/init:PF > /usr/lib/binfmt.d/WSLInterop.conf'

After wsl --shutdown and logging back in, now next to systemd, explorer.exe and the like are finally working again! ✓ 🥳

(tested on WSL2 @ Ubuntu22.04 and Debian bookworm/sid)

worked for me

I found the same problem and receiving issues like:

$ powershell.exe
bash: /mnt/c/WINDOWS/System32/WindowsPowerShell/v1.0/powershell.exe: cannot execute binary file: Exec format error

The @freckhard soluction of creating WSLInterop.conf worked for me.

Hello everyone, thanks to your comments, I was able to solve the problem that my Windows executables were not working after the activation of systemd, just by adding the above mentioned entry to the WSLInterop.conf.

Here is a one-liner for you:

sudo sh -c 'echo :WSLInterop:M::MZ::/init:PF > /usr/lib/binfmt.d/WSLInterop.conf'

After wsl --shutdown and logging back in, now next to systemd, explorer.exe and the like are finally working again! ✓ 🥳

(tested on WSL2 @ Ubuntu22.04 and Debian bookworm/sid)

This one worked for kali-linux. It takes a while to start WSL (the black screen), but works.

Thanks @freckhard your solution solved it for me, as well! I was using a custom Fedora distro.

Systemd does that, as those of us who’ve futzed with alternative systemd solutions in the past have come to know. The solution-slash-workaround is to put this file:

https://github.com/arkane-systems/bottle-imp/blob/master/othersrc/usr-lib/binfmt.d/WSLInterop.conf

in your /usr/lib/binfmt.d .

@lhrbest 这里已经给了 workaround 了