Windows WSL
Highlights
Summary
Setup
Source: Install WSL command
Use
winver
to find the Windows versionWindows 10 Version 20H2 (OS Build 19042.2006)
The original
wsl status
; Note: this will change afterwsl install
.
Turn-on Hyper-V, then Reboot Source: [REF-4]
// PowerShell
PS C:\WINDOWS\system32> DISM /Online /Enable-Feature /All /FeatureName:Microsoft-Hyper-V
PS C:\WINDOWS\system32>

Check system info
Install Ubuntu and Reboot
// PowerShell
PS C:\WINDOWS\system32> wsl --install -d Ubuntu
[...]
After reboot, a linux ubuntu terminal open to complete the ubuntu OS installation
// ubuntu shell terminal
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
Enter new UNIX username: gabe
New password:
Retype new password:
passwd: password updated successfully
Installation successful!
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.
Welcome to Ubuntu 20.04 LTS (GNU/Linux 5.10.16.3-microsoft-standard-WSL2 x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
System information as of Sat Sep 24 17:13:08 PDT 2022
System load: 0.06 Processes: 8
Usage of /: 0.4% of 250.98GB Users logged in: 0
Memory usage: 0% IPv4 address for eth0: 172.27.97.97
Swap usage: 0%
0 updates can be installed immediately.
0 of these updates are security updates.
The list of available updates is more than a week old.
To check for new updates run: sudo apt update
This message is shown once once a day. To disable it please create the
/home/gabe/.hushlogin file.
gabe@DESKTOP-U4G084O:~$
List all available distributions from Powershell:
PS C:\WINDOWS\system32> wsl -l -v
NAME STATE VERSION
* Ubuntu Running 2
PS C:\WINDOWS\system32> wsl --version
WSL version: 1.2.5.0
Kernel version: 5.15.90.1
WSLg version: 1.0.51
MSRDC version: 1.2.3770
Direct3D version: 1.608.2-61064218
DXCore version: 10.0.25131.1002-220531-1700.rs-onecore-base2-hyp
Windows version: 10.0.22621.1992
PS C:\WINDOWS\system32>
Install ssh
$ sudo apt-get install openssh-server
Enable ssh - from the linux terminal
Go to /etc/ssh folder, and modify sshd_config to the following:
$ sudo nano /etc/ssh/sshd_config
PasswordAuthentication yes
ChallengeResponseAuthentication yes
Generate host keys for ssh; otherwise, the following error may occur during
ssh restart
:sshd: no hostkeys available -- exiting
.
$ sudo ssh-keygen -A
ssh-keygen: generating new host keys: RSA DSA ECDSA ED25519
$
Restart the ssh server
$ sudo /etc/init.d/ssh restart
* Restarting OpenBSD Secure Shell server sshd [ OK ]
In addition to linux installation, the
wsl
was also updated; now we can issue the following:
// PowerShell
PS C:\WINDOWS\system32> wsl -l -v
NAME STATE VERSION
* Ubuntu Running 2
PS C:\WINDOWS\system32>
PS C:\WINDOWS\system32> wsl --status
Default Distribution: Ubuntu
Default Version: 2
Windows Subsystem for Linux was last updated on 9/24/2022
The Windows Subsystem for Linux kernel can be manually updated with 'wsl --update', but automatic updates cannot occur due to your system settings.
To receive automatic kernel updates, please enable the Windows Update setting: 'Receive updates for other Microsoft products when you update Windows'.
For more information please visit https://aka.ms/wsl2kernel.
Kernel version: 5.10.16
PS C:\WINDOWS\system32>
Check how to Connect Windows File Explorer to the Ubuntu drive; see Connect Windows to Linux;
Troubleshooting
After running the wsl --install -d Ubuntu
command above, a terminal window will open that is going to install the ubuntu, BUT an error occur:
Virtual hard disk files must be uncompressed and unencrypted and must not be sparse
To solve the "virtual hard disk uncompressed and unencrypted" issue, do the following:
This is recommended by Ref-3, and it worked
Open up File Explorer and navigate to
C:\Users\YOUR_USER\AppData\Local\Packages\CanonicalGroupLimited...
Right click on
LocalState
, then select Properties, then AdvancedEnsure
Compress contents to save disk space
andEncrypt contents to secure data
are both deselectedClick
OK
, thenApply
, to Apply changes to this folder onlyFrom the start menu, find and click "Ubuntu". A terminal window will open and start installing Ubuntu, as follow:
Update
// // PowerShell
PS C:\WINDOWS\system32> wsl --update
PS C:\WINDOWS\system32> wsl --shutdown
PS C:\WINDOWS\system32>
Connect Windows to Linux
Open File Explorer, and type
\\wsl$
File explorer will show the Linux instance and the folder structures
Connect Linux to Windows folders
From the Linux terminal
// shell
uname@compname:~$ cd /mnt
uname@compname:~$ ls -al
total 8
drwxr-xr-x 4 root root 4096 Jun 3 16:13 .
drwxr-xr-x 19 root root 4096 Jun 3 16:13 ..
drwxrwxrwx 1 root root 512 Jun 3 16:05 c
drwxrwxrwt 2 root root 40 Jun 3 16:13 wsl
uname@compname:~$ cd c/
uname@compname:~$ ls -al
ls: DumpStack.log: Permission denied
[...]
total 3644
drwxrwxrwx 1 root root 512 May 1 2018 '$Recycle.Bin'
dr-xr-xr-x 1 root root 512 Jun 3 13:50 '$WinREAgent'
drwxrwxrwx 1 root root 512 Jun 3 16:05 .
drwxr-xr-x 4 root root 4096 Jun 3 16:13 ..
lrwxrwxrwx 1 root root 12 May 1 2018 'Documents and Settings' -> /mnt/c/Users
[...]
uname@compname:~$
Use Termius or putty to connect to linux
For the host address, enter localhost
.
VS Code Connect Remotely to WSL
Install extension "Remote WSL" if not already. Not sure if need to restart VS code after installation.
Once installed, a button should appear on the bottom left hand corner (><) that can be used to "Open a Remote Window". This will open a new VS code
To open a Linux terminal within the already running VS code, click the (+) dropdown located on the top right hand side of the terminal tab, and select the distro "Ubuntu (WSL)"
Also see this article
From VS code connect to WSL
Then open folder and choose WSL folder
Start WSL after Windows reboot
To start WSL, for example after rebooting Windows, open PowerShell in the administrator mode, and issue the following command:
// PowerShell
PS C:\WINDOWS\system32> wsl
<userName>@<computerName>: $
The PowerShell terminal now becomes the WSL Ubuntu terminal.
Note: to open another Ubuntu terminal, from the Windows "Start" menu, find & click "Ubuntu for Windows", that will open the Ubuntu console
[Alternative] PowerShell is limited in functionality and lacks features such as: multiple tabs, panes, etc. Try install the "Windows Terminal". Using the windows terminal, to open "Panes" within the same tab, [alt-click-dropdown-selectType] to open a new pane of the selected terminal type; [ctrl-shift-w] to close the focused pane; [alt-arrowKey] to move between pane; [alt-shift-arrowKey] to resize pane.
From the Ubuntu terminal, start the SSH service so that we can connect to wsl Ubuntu from the SSH terminal (such as: putty or termius)
$ sudo /etc/init.d/ssh restart
* Restarting OpenBSD Secure Shell server sshd
Now, use the File Explorer should connect to Linux folder.
Repair or Reset WSL Linux instance
Click the Start button, then click Settings.
Click on Apps button, and then search for Ubuntu (or your distribution name) under Apps & features.
Under search results, click on Ubuntu (or your distribution name) and then click on Advance options.
Choose "Repair" or "Reset"
Best Practice - Set up a WSL development environment
[REF-5] Microsoft: Set up a WSL development environment]
Install GUI desktop in WSL2 Ubuntu
Source: - [Referenced here] WSL2 Ubuntu GUI - Install GUI Desktop in WSL2 Ubuntu 20.04 LTS in Windows 10
From the ubuntu terminal, setup Xfce. Note: after completion of the installation, we will be able to RDP (Windows remote desktop) into the Ubuntu GUI desktop
Update the Distro installation and remove previous install of xrdp
$ sudo apt-get update
$ sudo apt-get -y upgrade
$ sudo apt-get purge xrdp
Install Xrdp
$ sudo apt-get install xrdp
Install Xfce , select lightdm or gdm3 as per your preference when asked; optional to add
xfce4-goodies
.
$ sudo apt-get install -y xfce4
Open the start script,
/etc/xrdp/startwm.sh
, and comment the last 2 lines, and add one new entry, as shown below
$ sudo nano /etc/xrdp/startwm.sh
[...]
# test -x /etc/X11/Xsession && exec /etc/X11/Xsession
# exec /bin/sh /etc/X11/Xsession
# xce4
startxfce4
Modify the port to 3390 in the xrdp config
# first make a backup
$ sudo cp /etc/xrdp/xrdp.ini /etc/xrdp/xrdp.ini.bak
# change the port; optional
$ sudo sed -i 's/3389/3390/g' /etc/xrdp/xrdp.ini
# change the resolution by increasing the bits per pixel
$ sudo sed -i 's/max_bpp=32/#max_bpp=32\nmax_bpp=128/g' /etc/xrdp/xrdp.ini
$ sudo sed -i 's/xserverbpp=24/#xserverbpp=24\nxserverbpp=128/g' /etc/xrdp/xrdp.ini
$ echo xfce4-session > ~/.xsession
$
Start the
dbus
andxrdp
#$ sudo systemctl enable dbus
#$ sudo /etc/init.d/dbus start
# start rdp server
$ sudo /etc/init.d/xrdp start
Open RDP and connect to localhost:3390
How-to
References
[REF-1] Youtube - How To Run Linux Code on Windows with WSL 2 & VS Code
[REF-2] Microsoft - Windows Subsystem for Linux Documentation
[REF-5] Microsoft - Set up a WSL development environment
Last updated