Windows Subsystem for Linux


Windows Subsystem for Linux is a compatibility layer for running Linux binary executables natively on Windows 10 and Windows Server 2019.
In May 2019, WSL 2 was announced, introducing important changes such as a real Linux kernel, through a subset of Hyper-V features. Since June 2019, WSL 2 is available to the Windows 10 customers through the Windows Insider program, including the Home edition.

Overview

The first release of WSL provides a Linux-compatible kernel interface developed by Microsoft, containing no Linux kernel code, which can then run a GNU user space on top of it, such as that of Ubuntu, openSUSE, SUSE Linux Enterprise Server, Debian and Kali Linux. Such a user space might contain a Bash shell and command language, with native GNU command-line tools, programming-language interpreters, and even graphical applications.
The architecture was redesigned in WSL 2, with a Linux kernel running in a lightweight virtual machine environment.

Introduction and availability

When introduced with the Anniversary Update on August 2, 2016, only an Ubuntu image was available. The Fall Creators Update, released on October 17, 2017, moved the installation process for Linux distributions to the Windows Store, and introduced SUSE images.
WSL is available only in 64-bit versions of Windows 10 from version 1607. It is also available in Windows Server 2019.
Microsoft announced WSL 2 on May 6, 2019, which features a completely new VM-based backend in lieu of the prior system-call adaptation layer, planned for release through the Windows Insider program in June 2019.

Development

Microsoft's first foray into achieving Unix-like compatibility on Windows began with the Microsoft POSIX Subsystem, superseded by Windows Services for UNIX via MKS/Interix, which was eventually deprecated with the release of Windows 8.1. The technology behind Windows Subsystem for Linux originated in the unreleased Project Astoria, which enabled some Android applications to run on Windows 10 Mobile. It was first made available in Windows 10 Insider Preview build 14316.
Whereas Microsoft's previous projects and the third-party Cygwin had focused on creating their own unique Unix-like environments based on the POSIX standard, WSL aims for native Linux compatibility. Instead of wrapping non-native functionality into Win32 system calls as these prior systems utilized, WSL's initial design leveraged the NT kernel executive to serve Linux programs as special, isolated minimal processes attached to kernel mode "pico providers"'' as dedicated system call and exception handlers distinct from that of a vanilla NT process, opting to reutilize existing NT implementations wherever possible.
Though WSL was much faster and arguably much more popular than its brethren UNIX-on-Windows projects, Windows kernel engineers found difficulty in trying to increase WSL's performance and syscall compatibility by trying to reshape the existing NT kernel to recognize and operate correctly on Linux's API. At a Microsoft Ignite conference in 2018, Microsoft engineers gave a high-level overview of a new "lightweight" Hyper-V VM technology for containerization where a virtualized kernel could make direct use of NT primitives on the host. In 2019, Microsoft announced a completely redesigned WSL architecture using this lightweight VM technology hosting actual Linux kernel images, claiming full syscall compatibility.
Microsoft envisages WSL as "primarily a tool for developers – especially web developers and those who work on or with open source projects". In September 2018, Microsoft said that "WSL requires fewer resources than a full virtual machine", while also allowing users to use Windows apps and Linux tools on the same set of files.

Architecture

WSL 1

LXSS Manager Service

LXSS Manager Service is the service in charge of interacting with the subsystem, and the way that Bash.exe launches the Linux processes, as well as handling the Linux system calls and the binary locks during their execution.
All Linux processes invoked by a particular user go into a "Linux Instance". Once all the applications are closed, the instance is closed.

wsl.exe

The wsl.exe command is used to manage distributions in the Windows Subsystem for Linux on the command-line. It can list available distributions, set a default distribution, and uninstall distributions. The command can also be used to run Linux binaries from the Windows Command Prompt or Windows PowerShell. wsl.exe replaces lxrun.exe which is deprecated as of Windows 10 1803 and later.
Examples
Run uname command in WSL using PowerShell.

PS C:\temp> wsl uname --all
Linux WikiMachine 4.4.0-18362-Microsoft #1-Microsoft Mon Mar 18 12:02:00 PST 2019 x86_64 x86_64 x86_64 GNU/Linux

Example using sudo command from Command Prompt.

C:\temp> wsl sudo apt-get update
password for username:
Hit:1 https://archive.ubuntu.com/ubuntu xenial InRelease
Get:2 https://security.ubuntu.com/ubuntu xenial-security InRelease

Hardware and filesystem access

WSL 1's design featured no hardware emulation / virtualization and makes direct use of the host file system and some parts of the hardware, such as the network, which guarantees interoperability.
There are certain locations and configurations whose access/modification is restricted, even when running as root, with sudo from the shell. An instance with elevated privileges must be launched in order to get "sudo" to give real root privileges, and allow such access.

Limitations

WSL 1 is not capable of running all Linux software, such as 32-bit binaries, or those that require specific Linux kernel services not implemented in WSL. Due to a lack of any "real" Linux kernel in WSL 1, kernel modules, such as device drivers, can't be run. WSL 2, however, makes use of live virtualized Linux kernel instances.
It is possible to run some graphical applications by installing an X11 server within the Windows environment, although not without caveats, such as the lack of audio support or hardware acceleration. Support for OpenCL and CUDA is also not being implemented currently, although planned for future releases.
Microsoft stated WSL was designed for the development of applications, and not for desktop computers or production servers, recommending the use of virtual machines, Kubernetes and Azure for those purposes.

Benchmarks

In benchmarks WSL 1's performance is often near native Linux Ubuntu, Debian, Intel Clear Linux or other Linux distributions. I/O is in some tests a bottleneck for WSL. The redesigned WSL 2 backend is claimed by Microsoft to offer twenty-fold increases in speed on certain operations compared to that of WSL 1.

WSL 2

Version 2 introduces changes in the architecture. Microsoft has opted for virtualization through a highly optimized subset of Hyper-V features, in order to run its provided kernel and the underlying distributions, promising performance equivalent to WSL 1. For backward compatibility, developers don't need to change anything in their published distributions. WSL2 settings can be tweaked by the WSL global configuration, contained in an INI file named .wslconfig in the User Profile folder.
The distribution installation resides inside an ext4-formatted filesystem inside a virtual disk, and the host file system is transparently accessible through the 9P protocol, similarly to other virtual machine technologies like QEMU. For the users, Microsoft promised up to 20 times the read/write performance of WSL 1. From Windows an IFS network redirector is provided for Linux guest file access using the UNC path prefix of \\wsl$.
WSL 2 requires Windows build 19041 or higher.

Screenshots

Controversy

There are fears in the developer community that Windows Subsystem for Linux could be a way to embrace, extend, and extinguish Linux.
Richard Stallman has expressed fears that integrating Linux functionality into Windows will only hinder the development of open source software.