Table of Contents

Windows - How to enable and use Linux Subsystem (WSL2)

About

This page shows you:

wsl stands for windows subsystem linux is a system that permits to add:

on top and accessible from Windows 10.

The linux distribution are in container (ie lightweight vm) on top of Windows 10.

Therefore docker is also seen as wsl linux distribution when the WSL2 option of docker is checked.

Steps to enable, install and configure WSL2

This summary steps comes from:

Enable and install

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
winver
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
wsl --set-default-version 2

Get a linux distribution

Get a distribution from the store.

Installing, this may take a few minutes...
Please create a default UNIX user account. The username does not need to match your Windows username.
For more information visit: https://aka.ms/wslusers
adduser: The group `admin' already exists.
Enter new UNIX username: nico
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Installation successful!
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

sudo apt update -Y && sudo apt upgrade -Y

You can also access the WSL terminal from the Windows Terminal

Wsl2 Windows Terminal

Firewall

wsl adds a network interface called vEthernet (WSL)

Wsl Network Interface

Inbound

To enable the inbound communication from the WSL distro to Windows.

New-NetFirewallRule -DisplayName "WSL" -Direction Inbound  -InterfaceAlias "vEthernet (WSL)"  -Action Allow
Name                  : {793ad6f6-53c8-4a79-a14a-af572ff91226}
DisplayName           : WSL
Description           :
DisplayGroup          :
Group                 :
Enabled               : True
Profile               : Any
Platform              : {}
Direction             : Inbound
Action                : Allow
EdgeTraversalPolicy   : Block
LooseSourceMapping    : False
LocalOnlyMapping      : False
Owner                 :
PrimaryStatus         : OK
Status                : The rule was parsed successfully from the store. (65536)
EnforcementStatus     : NotApplicable
PolicyStoreSource     : PersistentStore
PolicyStoreSourceType : Local

Outbound

To enable the communication from a Windows 10 application to WSL, the process should have public access.

Get-NetFirewallRule  | where DisplayName -ILike "*PhpStorm*" | Remove-NetFirewallRule

Process To Wsl Public Windows Defender

Discovering WSL

From File Explorer

When you enable WSL and install a Linux distribution, you are installing a new file system, separated from the Windows NTFS C:\ drive on your machine. In Linux, drives are not given letters. They are given mount points. The root of your file system / is the mount point of your root partition. Not everything under / is the same drive.

From Windows

From Windows 10 explorer:

\\wsl$

Wsl File Explorer

Distribution Home Path
Ubuntu 20.04 \\wsl$\Ubuntu-20.04\home\username
Debian \\wsl$\Debian\home\username
Windows C:\Users\username

From Linux

The Linux distribution views your Windows file system as a mounted drive: /mnt/c

Example with the users

ls -l /mnt/c/Users/
d--x--x--x 1 nic nic 4096 Mar 16 08:33  Administrator
lrwxrwxrwx 1 nic nic   18 Dec  7  2019 'All Users' -> /mnt/c/ProgramData
dr-xr-xr-x 1 nic nic 4096 Mar 16 08:40  Default
lrwxrwxrwx 1 nic nic   20 Dec  7  2019 'Default User' -> /mnt/c/Users/Default
drwxrwxrwx 1 nic nic 4096 Mar 16 08:33  Public
-r-xr-xr-x 1 nic nic  174 Dec  7  2019  desktop.ini

Wsl

wsl has also an utility called wsl

List

List all distribution

wsl.exe -l -v
wsl --list --verbose
NAME                   STATE           VERSION
* docker-desktop-data    Running         2
  Ubuntu-20.04           Running         2
  docker-desktop         Running         2

From Stopped State to Running

wsl -d Ubuntu-20.04

where the option: –distribution or -d <Distro> - run the specified distribution.

Set a distro as default

To set Ubuntu as your default WSL distro, run

wsl --set-default ubuntu

Start a command

wsl command

example docker

wsl docker run ...

Command line

Run bash with wsl

C:\WINDOWS\system32\wsl.exe --distribution Ubuntu-20.04 --exec /bin/bash

Documentation / Reference