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
- Edit
/etc/wsl.conf, enable systemd support. (https://devblogs.microsoft.com/commandline/systemd-support-is-now-available-in-wsl/) - Run
wsl.exe --shutdownin wsl2 shell, and reopen the shell. - Run
wsl.exe --versionor 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)
Links to this issue
Commits related to this issue
- Add WSLInterop.conf to execute windows binary https://github.com/microsoft/WSL/issues/8843 — committed to sileshn/DebianSidWSL2 by sileshn 4 years ago
- added some tweaks from https://github.com/microsoft/WSL/issues/8843 — committed to tuteco/wsl-setup by evaschreck42 4 years ago
- Add notes for WSL + Systemd + VSCode fix (https://github.com/microsoft/WSL/issues/8843#issuecomment-1459120198) — committed to willwm/dotfiles by willwm 4 years ago
- Fix running Windows .exe files from a WSL/Ubuntu command prompt when mono-runtime is installed and systemd is enabled. Trying to run "explorer.exe" from a WSL/Ubuntu command prompt, for example, woul... — committed to shaneharper/dotfiles by shaneharper 3 years ago
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:
After
wsl --shutdownand logging back in, now next to systemd,explorer.exeand 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:
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/registerI 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
/etc/systemd/system/wslinterop-force.service
Then:
@freckhard Can confirm this worked for me as well.
@romaluca It is here, https://github.com/arkane-systems/bottle-imp/blob/v0.13.0/othersrc/usr-lib/binfmt.d/WSLInterop.conf
Thank you so much for posting this! ❤️ I am in the same situation. My executables are now working, though the
appendWindowsPathdirective still isn’t adding them to the systemPATH. But using a fully qualified path like/mnt/c/Windows/System32/clip.exeworks 🤔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
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:
Exit WSL on Powershell via
wsl --shutdownand 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:
_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 errorAs 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!
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-binfmtassumes it must be managing all of the registrations for the system, which seems like a problem even forsystemd-nspawn, for example. 😅I used
sudo systemctl mask systemd-binfmt.serviceto fix this (telling systemd explicitly to no longer assume it manages this). The consequence of this is that packages likeqemu-user-staticthat 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!
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”
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.confbe 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:
I then did
wsl --shutdownand restarted. Runningcmd.exefrom 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 cliworkaround didn’t work for me. That might be something that applies to a different root cause.The
WSLInrerop.conffile solution did not work for meIn my case what solved was disabling the cli interpreter using the command:
sudo update-binfmts --disable clifrom here https://github.com/microsoft/WSL/issues/4567#issuecomment-680034755@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 --shutdownnever got my exe’s working until I addedWSLInterop.confThanks 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
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
我是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_mischandler for WSL2 to run Windows binaries either is getting overwritten bysystemdor is never set:systemdenabledsystemddisabledI 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.
Worked for me on Win11 Pro Preview ( file now at commit 2742d2 )
worked for me
I found the same problem and receiving issues like:
The @freckhard soluction of creating WSLInterop.conf worked for me.
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.
@lhrbest 这里已经给了 workaround 了