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

About

This page shows you:

  • how to enable, install and configure WSL2 ( windows subsystem linux )
  • and introduce you to the basic of WSL.

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

  • one or more linux distribution (operating system)
  • with their file system

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

  • Enable
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
  • Check the minimal windows 10 build for WSL2 with winver
    • For X64, Build 18362
    • For ARM64, Build 19041
winver
  • Enable Virtual Machine feature
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
  • Download the kernel. Download and install the linux kernel
  • Set WSL2 as the default
wsl --set-default-version 2

Get a linux distribution

Get a distribution from the store.

  • Example: ubuntu
  • Launch it and create a user (not admin, if you need to run with root privilege, use sudo
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.
  • Update it
sudo apt update -Y && sudo apt upgrade -Y

You can also access the WSL terminal from the Windows Terminal

_

Firewall

wsl adds a network interface called vEthernet (WSL)

_

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.

  • Delete the rule for your application. Example for phpStorm
Get-NetFirewallRule  | where DisplayName -ILike "*PhpStorm*" | Remove-NetFirewallRule
  • Then start your service (debug in case of phpstorm) and select public

_

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$

_

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
  • same as
wsl --list --verbose
  NAME                   STATE           VERSION
* docker-desktop-data    Running         2
  Ubuntu-20.04           Running         2
  docker-desktop         Running         2

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 ...

Documentation / Reference


Powered by ComboStrap