WSL2 - A getting started guide - How to enable and use Linux Subsystem WSL2 on Windows

Windows 95 Wallpaper Version

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

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.

  • 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

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
  • same as
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





Discover More
Card Puncher Data Processing
Application - Software Development Kit (SDK)

A Software Development Kit (SDK) makes possible to develop application (ie to compile code, to run it , etc...) A typical SDK example is the Java Development Kit (also known as Java SDK, or JDK) for...
Bash Liste Des Attaques Ovh
Bash on Windows

To get a bash environment on Windows, you can install: WSL (Windows Subsystem for Linux) Cygwin or MSYS+MinGW.
Docker Wsl Distro Folders File Explorer
Docker and WSL

Both your own WSL 2 distro and docker-desktop run on the same utility VM. They share the same Kernel, VFS cache etc. They just run in separate namespaces so that they have the illusion of running totally...
Card Puncher Data Processing
OS - Home environment Variable

The special home directory is the user's home directory. It is set in a environment variable System: C:\Windows\system32\config\systemprofile or C:\Windows\system32 Users: C:\Users\ User:...
Undraw File Manager Re Ms29
File System - Home Directory

The home directory is a special directory where all documents for a user or application are supposed to be stored. home The value is dependent of the operating system. Distribution Home Path Wsl...



Share this page:
Follow us:
Task Runner