Skip to content

[WSL2][systemd][interop] Unable to Execute Windows Binary when systemd enabled #8843

Closed

Description

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

Activity

  1. WitherZuo commented on Sep 22, 2022

    @WitherZuo

    The interoperability of WSL and Windows relies on WSL init. After systemd is turned on, WSL init will also run under systemd, which may be one of the reasons for breaking the interoperability function.

  2. Re4son commented on Sep 22, 2022

    @Re4son

    I don't think it is by design WitherZuo (@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.

  3. reynoldskr commented on Sep 22, 2022

    @reynoldskr

    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
    
  4. cerebrate commented on Sep 22, 2022

    @cerebrate

    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 .

  5. Re4son commented on Sep 22, 2022

    @Re4son

    Good pointer Alistair Young (@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 Alistair Young (@cerebrate), much appreciate it.

  6. benhillis commented on Sep 23, 2022

    @benhillis
    Member

    Re4son Kernel (@Re4son) - thanks for the additional detail. This boils down to Linux not supporting namespacing of binfmt_misc interpreters. Any fix we make is going to be problematic until that is resolved.

    It looks like we need to handle the case where the distro has binfmt interpreters installed by adding our own .conf file though.

  7. dbbao commented on Sep 26, 2022

    @dbbao

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

  8. PhyX-Meow commented on Sep 26, 2022

    @PhyX-Meow

    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 了

  9. dbbao commented on Sep 26, 2022

    @dbbao

    我是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#

  10. PhyX-Meow commented on Sep 26, 2022

    @PhyX-Meow

    @lhrbest Try restart wsl. Normally there should be a file named WSLInterop.

  11. kpko commented on Dec 5, 2022

    @kpko

    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.

  12. romaluca commented on Dec 21, 2022

    @romaluca
  13. PhyX-Meow commented on Dec 21, 2022

    @PhyX-Meow
  14. cvernooy23 commented on Jan 12, 2023

    @cvernooy23

    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?

  15. ststefa commented on Feb 17, 2023

    @ststefa

    Thanks a lot, this solved the problem also for me on Debian bullseye. Learned something new today ;)

  16. 34 remaining items

  17. alkuzad commented on Nov 3, 2023

    @alkuzad

    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
    
  18. puetzk commented on Dec 2, 2023

    @puetzk

    It seems that Microsoft has now implemented a fix for this? at least on my machine (Windows 11 22H2, WSL version: 2.0.9.0) they now made a unit /run/systemd/generator.early/wsl-binfmt.service

    # Note: This file is generated by WSL to prevent binfmt.d from overriding WSL's binfmt interpretor.
    # To disable this unit, add the following to /etc/wsl.conf:
    # [boot]
    # protectBinfmt=false
    
    [Unit]
    Description=WSL binfmt restore
    After=systemd-binfmt.service
    
    [Service]
    Type=oneshot
    ExecStart=/bin/sh -c '(echo -1 > /proc/sys/fs/binfmt_misc/WSLInterop-late) ; (echo :WSLInterop-late:M::MZ::/init:P > /proc/sys/fs/binfmt_misc/register)'
    

    This is still not the "correct" solution Lennart Poettering (@poettering) suggested in systemd/systemd#28126 (using /run/binfmt.d, so that the normal systemd-binfmt.service would know about their registration), but it should at least add their service back after both run (and it schedules itself to run immediately afterward).

    https://docs.kernel.org/admin-guide/binfmt-misc.html
    However, it has another shortcoming - compared to all the settings proposed here, it's missing the Flags: F (fix binary): https://docs.kernel.org/admin-guide/binfmt-misc.html, which means that this registration doesn't work in a chroot (or other mount namespace that doesn't have wsl's /init mounted).

  19. earlofhemsley commented on Apr 5, 2024

    @earlofhemsley

    #8843 (comment)

    Thanks for this. It fixed my problem. I will come back later and read the rest of the solutions since this one is quite old and there may be a newer way to handle this issue now. Cheers!

  20. syigzaw commented on Apr 12, 2024

    @syigzaw

    I'm getting a very similar error, but for Linux commands instead. At the very beginning of a new terminal I get:

    -bash: /usr/bin/grep: cannot execute binary file: Exec format error
    

    And I also get this:

    $ groups
    -bash: /usr/bin/groups: cannot execute binary file: Exec format error
    

    However, some other commands that can be found under /usr/bin/, like find and which, work fine for me. I've tried all of the suggestions above and they didn't help. And that isn't a surprise since I didn't actually have any issues running Windows executables, but this error message is the exact same, so I'm wondering if my issue is related.

    For background info, this is a fresh WSL2 installation on Windows 10, and after installing WSL2 I installed Docker for Windows, while also setting autoMemoryReclaim to dropcache as suggested here.

  21. pfeileon commented on Apr 18, 2024

    @pfeileon

    Since the other thread ( #8952 ) was closed: I've just had this error with the git credential manager and simply restarting WSL (wsl --shutdown) solved the issue, so I'm not sure if systemd is at fault.

  22. smartexpert commented on May 15, 2024

    @smartexpert

    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: #10363 (comment) credits to Martin Rüegg (@martin-rueegg)

  23. trallnag commented on Aug 8, 2024

    @trallnag

    Just ran into this randomly for the first time in a long time. Simple restart of the distributions seems to have fixed it.

    WSL version: 2.2.4.0
    Kernel version: 5.15.153.1-2
    WSLg version: 1.0.61
    MSRDC version: 1.2.5326
    Direct3D version: 1.611.1-81528511
    DXCore version: 10.0.26091.1-240325-1447.ge-release
    Windows version: 10.0.22631.3880
    
  24. oxivanisher commented on Oct 16, 2024

    @oxivanisher

    This happens to me sometimes with Ubuntu 24.04, but not always. I currently do not know what the difference between the WSL2 installations of the Ubuntu VMs where. I did the exact same thing, sometimes wslvar worked, sometimes not. 🤷‍♂️

  25. michaelmhoffman commented on Oct 23, 2024

    @michaelmhoffman

    systemd/systemd#28126 is now closed. The systemd team regard this as an incorrect configuration by WSL, and suggest putting configuration in /run/binfmt.d before calling systemd as a fix.

    My current setup (WSL 2.3.24.0, Windows 11 23H2 22631.4391) has the fix in /run/systemd/generator.earlywsl-binfmt.service and it seems to work for me, despite being less elegant than fixing it in /run/binfmt.d.

    I suggest that the issue be closed as fixed.

  26. klasfahlberg commented on Mar 3, 2026

    @klasfahlberg

    I'm till affected by this issue. Is it the same bug as this one or a new one? For info see
    https://youtrack.jetbrains.com/issue/IJPL-236538

    wsl --version
    WSL version: 2.6.3.0
    Kernel version: 6.6.87.2-1
    WSLg version: 1.0.71
    MSRDC version: 1.2.6353
    Direct3D version: 1.611.1-81528511
    DXCore version: 10.0.26100.1-240331-1435.ge-release
    Windows version: 10.0.26200.7781

    $ lsb_release -a
    No LSB modules are available.
    Distributor ID: Ubuntu
    Description: Ubuntu 22.04.5 LTS
    Release: 22.04
    Codename: jammy

  27. xenobytezero commented on Mar 26, 2026

    @xenobytezero

    I'm also still affected by this issue, and it seems like some tools I have (namely ForkGit) seem to work for a while then fail again after an amount of time.

  28. hoerup commented on Jun 30, 2026

    @hoerup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions