I'm using the new WLS2 on Windows 10 Home. I noticed that, when I ran "npm", the "npm" from my Windows program files was executed instead of the Linux version.
When I tried to execute cmd.exe or notepad.exe from WSL2, I noticed those also work!
How is this possible? How is WSL2 able to run Windows executables?
Additionally, how can I force WSL2 to prioritize the Linux executables over the Windows ones in cases where they have the same names?
Best Answer
The Windows executable (PE binary) is added as a binfmt_misc entry in WSL2. In simple words, binfmt_misc is a Linux kernel feature which allows arbitrary executable file formats to be recognized and passed to certain programs.
In WSL2, the
init
binary (from which every process is forked) register the Windows PE binary as a executable and make it executable by itself (i.e. theinit
). Here is a output of the PE binfmt entry:WSLInterop
is just a name for the entry. The magic number4d5a
isMZ
which is first two bytes of Windows PE executable. Assume this is a fingerprint with whichinit
(the interpreter) recognizes PE binary.Users can disable the registry with this command:
Further readings: