WSL2 or Windows Subsystem for Linux, version 2 is a magnificent piece of software made by Microsoft to incorporate a real Linux system (yes, indeed!) into Windows.
Gone are the days that Steve Ballmer told the world Linux was a cancer and contaminated all other software with "hippie GPL rubbish" (June 2001). The idiot...
Satya Nadella took over the control of Microsoft in 2014 and ever since a whole new wind has blown over the "Microsoft attitude" towards the world. This guy saw that cooperation between the two giant OS-es was a necessity and look to what this has lead: Linux in Windows....
It started some years ago with WSL, version 1. That was a start, but was not something to continue with. Yes, you could run Linux commands on your Windows PC but there was still a translation layer in between the two OS-es that slowed down IO dramatically. Linux calls had to be routed to the "intermediate" layer and translated to Windows calls. And vice versa...
This is gone now with WSL2: it has its own contained Linux kernel, built by Microsoft, inside a lightweight virtual machine so no conversion techniques are needed anymore do to IO calls. Tests have proven this has increased IO speeds with a factor up to 6x faster!
A maybe even bigger thing is that now you can run Docker images inside WSL2!
All that would mean you don't need separate virtual machine applications anymore like VirtualBox or VmWare to run a Linux distro on your Windows PC. And yes, it works on Windows Home too! No need for expensive Pro or Enterprise licenses, none of that!
Whether you love or you hate Microsoft, you have to admit they've come a long way and did a superb job!!!
The easiest way to turn on the WSL feature is to open the Control Panel, select Programs and then choose Turn Windows Features on or off (or type "Turn Windows Features on or off" in the Start menu).
You will get the below given dialogue box in which you have to make sure the following two selections are activated (if not activated yet):
Virtual Machine Platform
Windows Subsystem for Linux
Once this is done, some update will start and you might have to reboot your system to get things in place.
See image on the right for the options to activate.
To make sure you have the latest and greatest WSL installed on your machine, run the following command in a Powershell window:
wsl --update
This will check the current version and if an update is available, it will install that one for you.
Once WSL2 is up and running, it's time to install a Linux distribution.
To see what Linux distributions are currently available, run the following command in PS command box:
wsl --list --online or the abbreviated version of it: wsl -l -o
As of this writing, these are the available Linux distros:
PS C:\Users\Geert> wsl --list --online
The following is a list of valid distributions that can be installed.
Install using 'wsl.exe --install <Distro>'.
NAME FRIENDLY NAME
Ubuntu Ubuntu
Debian Debian GNU/Linux
kali-linux Kali Linux Rolling
Ubuntu-18.04 Ubuntu 18.04 LTS
Ubuntu-20.04 Ubuntu 20.04 LTS
Ubuntu-22.04 Ubuntu 22.04 LTS
OracleLinux_8_5 Oracle Linux 8.5
OracleLinux_7_9 Oracle Linux 7.9
SUSE-Linux-Enterprise-Server-15-SP4 SUSE Linux Enterprise Server 15 SP4
openSUSE-Leap-15.4 openSUSE Leap 15.4
openSUSE-Tumbleweed openSUSE Tumbleweed
To install one of the above distros, go to the Microsoft Store and search for one, example Ubuntu.
You will be presented by 4 possible Ubuntu versions (again, as of this writing):
Ubuntu
Ubuntu 18.04.5 LTS
Ubuntu 20.04.5 LTS
Ubuntu 22.04.2 LTS
The Ubuntu option will always install the latest officially LTS supported distro, which is currently 22.04.
In 2024, for instance, that will change to 24.04 LTS and so on. The other ones are "fixed" releases that will never change version automatically.
Once you made your choice, press the "Get" button. The download will start and once the download is finished, the button will change into "Open". This will start the installation by opening a command box and after installation, will allow you to start working with that Ubuntu version.
To see the newly installed Linux distro as one of the drop-down options while opening Windows Terminal, you first have to close the Windows Terminal application, should it be open.
Once restarted, press the down arrow at the most-right position of the tab rule and you will see the new Linux distro. Selecting it will open the distro in a new tab.
Note that the download location of the distro is C:\Users\<user_name>\AppData\Local\Packages and the directory itself is named in a cryptically way (at least the end of the directory name). Example: CanonicalGroupLimited.Ubuntu22.04LTS_79rhkp1fndgsc.
As said in the above section, the installation of wsl2 is in C:\Users\<user_name>\AppData\Local\Packages, subdirectory .\CanonicalGroupLimited.Ubuntu22.04LTS_79rhkp1fndgsc.
Inside that folder you will see a few other sub folders. However, one of them is special: LocalState.
This folder contains the ext4.vhdx file that contains the complete RFS of the Linux distribution.
There's a few ways to see the content of that large ext4.vhdx file:
By starting up the Ubuntu distro in PowerShell (obvious)
By opening a file explorer and passing \\wsl$ as network address in the address bar
The latter one will show - among other possible distro's and applications like Docker desktop - the Ubuntu (or other) distribution.
Clicking on that distribution in the file explorer will also show you the whole RFS topology, just like you would see it if you were to view the RFS content in a "regular" Linux terminal.
If you want to get rid of a Linux distribution in WSL2 the following actions have to be taken:
Unregister the Linux distribution in WSL: wsl --unregister <name_of_linux_distro>
Uninstall the Linux distribution:
In the Start menu, type (for instance) Ubuntu: you will see all the installed Ubuntu distros (or other ones, depending on what you've installed)
Select the Uninstall option: the complete Linux distro will be removed from your PC.
The Linux distro can become quite big in size, sometimes up to 100GB. Knowing that the default location is the C-drive on Windows it can happen that you become short of space on that drive.
There is, however, a clever trick to move the virtual harddisk file with extension .vhdx (Virtual Hard Disk Extended, or Virtual Hard Disk v2) to another drive where there's more space available.
Since wsl version 2.3.11 and later it's now possible to move the Linux distro to another location using the following command:
wsl --manage <distro_name> --move <new_location>
Example:
First run wsl --list --all to see what Linux distro's are installed. This will result something like this:
C:\Users\f16873c
> wsl --list --all
Windows Subsystem for Linux Distributions:
Ubuntu-22.04 (Default)
Ubuntu-24.04
Then choose which Linux distro you want to move: e.g. Ubuntu-24.04
So, run the following command
wsl --manage Ubuntu-24.04 --move D:\Data\wsl\ubuntu-24.04
___________________________________________________________________________________________________________________________________________
When you press the Windows key + R and type in the parameter %LocalAppData% you will be directed to the location where Windows stores all Linux files you activated with WSL2. The result will be C:\Users\<your_name>\AppData\Local.
The Linux distros are stored in a subdirectory called Packages and in there you can look for the subdirectory containing the files for your chosen Linux distribution.
Example: when you installed Ubuntu 22.04 as Linux distro into your WSL2 environment, it will be stored in a directory called CanonicalGroupLimited.Ubuntu22.04LTS_<randome_character_sequence> located in the directory C:\Users\<your_name>\AppData\Local\Packages.
If you enter that directory you will see again a few other subdirectories, but the one of interest is called LocalState. That directory contains, among a few other things, a file called ext4.vhdx. In the long run, this file can - as said before - become quite large. The trick is now to move that directory to another location and to make sure it still works from that other location.
The trick is to use an external application called Link Shell Extension which can be downloaded here. This page contains a download button to select the installer. Make sure you select the All Windows 64 installation.
This application allows you to make soft and hard links as well as variations on these. Have a look on the website where you can find a very detailed in-depth explanation of the application.
Once you installed the application do the following steps:
Find the new location for the .vhdx file and create a directory wsl2 (or whatever you want to name it)
Move the complete directory LocalState to that new location. Make sure the directory LocalState is also removed from the original location.
Once moved, right-click on the new directory name LocalState and select the item Pick Link Source from the context menu that will open
Go to the location in the C:\Users\<your_name>\AppData\Local\Packages directory where your Linux distro directory is; in the example given that is CanonicalGroupLimited.Ubuntu22.04LTS_<randome_character_sequence>.
Right-click on the directory and select the option Drop As... from the context menu: it will open another drop down box
Select the option Junction since we want to make a "link" to that directory (Junctions are to directories what Symbolic Links are to files)
A "directory" with the same name, LocalState, will be re-created in the original directory. However, it's a link to the real location of the LocalState directory
From here onwards the large .vhdx file is moved away from your C-drive and you should be able to work with the relocated Linux distro just the way you worked with it before the move.
A YouTube video from TroubleChute is available here. In another section on this page there's an overview of the YouTube video's TroubleChute has made regarding WSL.
PowerShell is already at version 7 but also there updates are available frequently. Whenever a new version is available it will be given if you open a PowerShell 7 command box. It's still up to you to decide to go for the newest version or not.
If you want to update, run the following command inside a PS command box:
iex "& { $(irm https://aka.ms/install-powershell.ps1) } -UseMSI"
This will check what PS version you have and start the update when needed. Make sure all other PS command box windows are closed after starting the update procedure.
To start with, see this link from the makers of WSL itself. Below is an overview if you don't want to know the inner details.
Note that all the below commands must be run from a PowerShell command window!
Install WSL: wsl --install
List available Linux distros: wsl --list --online or wsl -l -o
List of installed Linux distros: wsl --list --verbose or wsl -l -v
Set WSL version to 1 or 2: wsl --set-version <distro name> <versionnumber>
Note that WSL version 2 is only available in Windows 10/11 from a certain version onwards (version 1903, build 18362 or later)
Set default WSL version: wsl --set-default-version <version>
Set default Linux distro: wsl --set-default <distro name>
Change to home directory: wsl ~
Run specific Linux distro from PowerShell or CMD: wsl --distribution <distro name> --user <user name>
Update WSL: wsl --update
Check WSL status: wsl --status
Check WSL version: wsl --version
Help command: wsl --help
Change default user for a Linux distro: <distro name> config --default-user <user name>
Shutdown WSL: wsl --shutdown
Terminate a specific Linux distro: wsl --terminate <distro name>
Check IP address of the Linux distro installed via WSL2: wsl hostname -i
Check IP address of the Windows machine as seen from WSL2: cat /etc/resolv.conf
Doesn't seem to work from a PowerShell command line???
Import a distribution: wsl --import <distro name> <install location> <filename>
Export a distribution: wsl --export <distribution> <filename>
This will generate a .tar file that contains the whole distribution. Useful if you want to create a snapshot, for instance.
Unregister or uninstall a Linux distro: wsl --unregister <distro name>
Mount a disk or device: wsl --mount <disk path>
Unmount a disk: wsl --unmount <disk path>
Check the Linux version currently used:
In a Linux terminal, run the command lsb_release -a. This will give the following output:
geertvc@Win10:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 24.04.1 LTS
Release: 24.04
Codename: noble
You can also run the command cat /etc/*-release. This is the output:
geertvc@TL0355418:~/mystuff/bashfiles$ cat /etc/*-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=24.04
DISTRIB_CODENAME=noble
DISTRIB_DESCRIPTION="Ubuntu 24.04.1 LTS"
PRETTY_NAME="Ubuntu 24.04.1 LTS"
NAME="Ubuntu"
VERSION_ID="24.04"
VERSION="24.04.1 LTS (Noble Numbat)"
VERSION_CODENAME=noble
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=noble
LOGO=ubuntu-logo
Splitting pane vertically, default profile shown on the right: Alt-Shift-+ (use the regular keyboard + sign, not the numerical keyboard + sign)
When the default profile is for instance Command Prompt then the right pane will show you the Command Prompt window, whatever the content of the left window is.
Splitting pane horizontally, default profile shown at the bottom: Alt-Shift--
When the default profile is for instance Command Prompt then the bottom pane will show you the Command Prompt window, whatever the content of the top window is.
Splitting pane horizontally, duplicate profile shown on the right: Alt-Shift-D
When the current profile is for instance Ubuntu then the right part will be a duplicate of the Ubuntu instance on the left. This is different compared to the shortcuts Alt-Shift-+ and/or Alt-Shift--!!!
Remove the active window: Ctrl-Shift-W
Make sure the cursor is in the window you want to remove!!!
If you want to resize a screen make sure the cursor is in the window of the screen you want to resize.
Once done, use the Shift + Alt + arrow_key to resize the screen. Following happens when using the arrow key:
up: reduce the height of the screen (doesn't work if the screen itself isn't split vertically)
down: increase the height of the screen (doesn't work if the screen itself isn't split vertically)
left: reduce the width of the screen (doesn't work if the screen itself isn't split horizontally)
right: increase the width of the screen (doesn't work if the screen itself isn't split horizontally)
Here's the playlist of TroubleChute's WSL video's: https://www.youtube.com/playlist?list=PLsSFD61FiOVmzTB-3JhsgPByozLA14Uyn.
Well presented, very useful!
Some important video's:
You can now install a GUI when WSL2 is used. I've taken XFCE since that has been explained by TroubleChute in one of his video's. See this section above.
Another interesting link I found: How To Enable Remote Desktop Protocol Using xrdp on Ubuntu 22.04
The following web page explains what to do to recompile the Linux kernel so that EXT4 partitions can be detected by WSL2. The website also explains what to do if you want to mount SD cards in WSL, but this section here only touches the mechanism to recompile the WSL2 kernel, if needed.
In short:
Install the following packages:
sudo apt install build-essential flex bison libssl-dev libelf-dev libncurses5-dev git bc pahole
Clone the Microsoft-based Linux kernel:
Create a directory for the Linux kernel in your home directory in WSL2 (not on a Windows NTFS partition, that won't work!)
git clone https://github.com/microsoft/WSL2-Linux-Kernel.git
cd WSL2-Linux-Kernel
Select relevant features in the Linux menu configuration:
export KCONFIG_CONFIG=Microsoft/config-wsl
make menuconfig
Select the following items (some might and will be already selected by default):
[*] Enable loadable module support
General setup --->
[*] Initial RAM filesystem and RAM disk (initramfs/initrd) support
Device Drivers --->
[*] Multiple devices driver support (RAID and LVM) --->
<*> Device mapper support
[*] Block Devices --->
<*> Loopback device support
SCSI device support --->
<*> SCSI device support --->
<*> SCSI disk support
<*> SCSI generic support
SCSI Transports --->
<M> iSCSI Transport Attributes
[*] SCSI low-level drivers --->
<M> iSCSI Initiator over TCP/IP
File systems --->
<*> FUSE (Filesystem in Userspace) support
[*] Networking support --->
Networking options --->
[*] TCP/IP networking
Compile the kernel:
sudo make KCONFIG_CONFIG=Microsoft/config-wsl
Install the newly build iSCSI modules
sudo make modules_install
After compiling the kernel, the physical copy is located in ./arch/boot/x86, called bzImage. But there's also a link to that location in ./arch/x86_64/boot/bzImage, which links to ../../x86/boot/bzImage.
Copy the new kernel image to the Windows host
cp ./arch/x86_64/boot/bzImage /mnt/c/Users/your-user-name/
Create a file on your Windows host (c:\Users\<user_name>) called .wslconfig
Add the following content to it:
[wsl2]
kernel=C:\\Users\\your-user-name\\bzImage
swap=0
localhostForwarding=true
Exit WSL2
wsl --shutdown
Add more information to the .wslconfig file (before using iSCSI, those modules must be loaded first!)
sudo modprobe -v libiscsi
sudo modprobe -v scsi_transport_iscsi
sudo modprobe -v iscsi_tcp
sudo modprobe -v libiscsi_tcp
To get iSCSI itself operational, follow the rest of the above mentioned web page. This section only explained what to do if you want to recompile the kernel with additional modules compiled.
This is explained here.
In short:
.wslconf is used for global configuration settings and is located in c:\Users\<user_name>
wsl.conf is used for distro-restricted configuration settings and is located in /etc
WSL2 now also supports systemd. More info here.
To change the hostname in WSL2, follow the steps below:
Run sudo vi /etc/wsl.conf
If the file didn't exist before, make sure you add the following lines to it as a starter:
[network]
generateHosts = false
The last line will make sure the file /etc/hosts will not be regenerated the next restart of WSL2.
Add the line hostname = <new_hostname>
Close the file
Edit the file /etc/hosts using sudo: sudo /etc/hosts
You will see at the beginning of the file that this file is automatically generated. Well, it was until now. With the generateHosts = false we added to the file /etc/wsl.conf that will not happen anymore in the future.
Replace the old hostname with the new hostname: search for the line starting with 127.0.1.1 and change the old hostname twice.
Edit the file /etc/hostname as sudo and replace the old hostname with the new hostname
Restart WSL2 and you should see the new hostname in the command prompt
Info found here.
On the command line, type \\wsl$ and you should end up in the distro directory from where you can explore the root file system (RFS).