Windows WSL

Highlights

Setup after laptop reboot
# enable ssh
$ sudo /etc/init.d/ssh restart
# restart docker
sudo service docker start
# re-register ssh keys with ssh-agent
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa_gabepublic
Resynch wsl clock when out-of-synch
$ sudo hwclock -s
# if it does work, do the following
# From cmd prompt
wsl --shutdown
# then restart
wsl

Summary

Setup

Source: Install WSL command

  • Use winver to find the Windows version

    Windows 10 Version 20H2 (OS Build 19042.2006)

  • The original wsl status; Note: this will change after wsl install.

wsl --status
// PowerShell
PS C:\WINDOWS\system32> wsl --status
Copyright (c) Microsoft Corporation. All rights reserved.
Usage: wsl.exe [Argument]

Arguments:
    --install <Options>
        Install Windows Subsystem for Linux features. If no options are specified,
        the recommended features will be installed along with the default distribution.

        To view the default distribution as well as a list of other valid distributions,
        use 'wsl --list --online'.

        Options:
            --distribution, -d [Argument]
                Specifies the distribution to be downloaded and installed by name.

                Arguments:
                    A valid distribution name (not case sensitive).

                Examples:
                    wsl --install -d Ubuntu
                    wsl --install --distribution Debian

    --list, -l [Options]
        Lists distributions.

        Options:
            --online, -o
                Displays a list of available distributions for install with 'wsl --install'.

    --help
        Display usage information.
PS C:\WINDOWS\system32>

  • 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> 
Required features for WSL
  • Check system info

systeminfo
// PowerShell
PS C:\WINDOWS\system32> bcdedit

Windows Boot Manager
--------------------
identifier              {bootmgr}
device                  partition=\Device\HarddiskVolume3
path                    \EFI\Microsoft\Boot\bootmgfw.efi
description             Windows Boot Manager
locale                  en-US
inherit                 {globalsettings}
default                 {current}
resumeobject            {17e348be-3b64-11ed-a2af-f7622a3b200c}
displayorder            {current}
toolsdisplayorder       {memdiag}
timeout                 30

Windows Boot Loader
-------------------
identifier              {current}
device                  partition=C:
path                    \WINDOWS\system32\winload.efi
description             Windows 10
locale                  en-US
inherit                 {bootloadersettings}
recoverysequence        {a64a7c09-3b9d-11ed-837d-86b9cc1f9826}
displaymessageoverride  Recovery
recoveryenabled         Yes
isolatedcontext         Yes
allowedinmemorysettings 0x15000075
osdevice                partition=C:
systemroot              \WINDOWS
resumeobject            {17e348be-3b64-11ed-a2af-f7622a3b200c}
nx                      OptIn
bootmenupolicy          Standard
hypervisorlaunchtype    Auto
PS C:\WINDOWS\system32>
PS C:\WINDOWS\system32> systeminfo

Host Name:                 DESKTOP-U4G084O
OS Name:                   Microsoft Windows 10 Pro
OS Version:                10.0.19042 N/A Build 19042
OS Manufacturer:           Microsoft Corporation
OS Configuration:          Standalone Workstation
OS Build Type:             Multiprocessor Free
Registered Owner:          gabe
Registered Organization:
Product ID:                00342-50610-98658-AAOEM
Original Install Date:     9/23/2022, 5:20:51 PM
System Boot Time:          9/24/2022, 5:10:35 PM
System Manufacturer:       Dell Inc.
System Model:              Latitude E7470
System Type:               x64-based PC
Processor(s):              1 Processor(s) Installed.
                           [01]: Intel64 Family 6 Model 78 Stepping 3 GenuineIntel ~2200 Mhz
BIOS Version:              Dell Inc. 1.35.3, 7/12/2022
Windows Directory:         C:\WINDOWS
System Directory:          C:\WINDOWS\system32
Boot Device:               \Device\HarddiskVolume3
System Locale:             en-us;English (United States)
Input Locale:              en-us;English (United States)
Time Zone:                 (UTC-08:00) Pacific Time (US & Canada)
Total Physical Memory:     16,280 MB
Available Physical Memory: 9,079 MB
Virtual Memory: Max Size:  19,224 MB
Virtual Memory: Available: 11,492 MB
Virtual Memory: In Use:    7,732 MB
Page File Location(s):     C:\pagefile.sys
Domain:                    WORKGROUP
Logon Server:              \\DESKTOP-U4G084O
Hotfix(s):                 5 Hotfix(s) Installed.
                           [01]: KB5013624
                           [02]: KB4562830
                           [03]: KB5007401
                           [04]: KB5017308
                           [05]: KB5016705
Network Card(s):           5 NIC(s) Installed.
                           [01]: Intel(R) Ethernet Connection I219-LM
                                 Connection Name: Ethernet
                                 Status:          Media disconnected
                           [02]: Intel(R) Dual Band Wireless-AC 8260
                                 Connection Name: Wi-Fi
                                 DHCP Enabled:    Yes
                                 DHCP Server:     192.168.1.1
                                 IP address(es)
                                 [01]: 192.168.1.25
                                 [02]: fe80::3478:e08c:bba2:b73d
                           [03]: Bluetooth Device (Personal Area Network)
                                 Connection Name: Bluetooth Network Connection
                                 Status:          Media disconnected
                           [04]: Hyper-V Virtual Ethernet Adapter
                                 Connection Name: vEthernet (Default Switch)
                                 DHCP Enabled:    No
                                 IP address(es)
                                 [01]: 172.29.64.1
                                 [02]: fe80::19f6:1635:70a3:f94e
                           [05]: Hyper-V Virtual Ethernet Adapter
                                 Connection Name: vEthernet (WSL)
                                 DHCP Enabled:    No
                                 IP address(es)
                                 [01]: 172.27.96.1
                                 [02]: fe80::4d30:4712:bec7:d77a
Hyper-V Requirements:      A hypervisor has been detected. Features required for Hyper-V will not be displayed.
PS C:\WINDOWS\system32>

  • 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:~$

Ubuntu virtual hard disk

They are stored in %USERPROFILE%\AppData\Local\Packages<distribution package name>\LocalState\ext4.vhdx For Ubuntu, the distribution package name is CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc

  • 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
$ 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>

Troubleshooting

After running wsl --install -d Ubuntu command, even though it fails, the "Ubuntu" icon will show up on the start menu.

  • 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 Advanced

    • Ensure Compress contents to save disk space and Encrypt contents to secure data are both deselected

    • Click OK, then Apply, to Apply changes to this folder only

    • From 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

Windows File Explorer shows Ubuntu folders

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

After rebooting Windows, the File Explorer cannot connect the Linux folder (see Connect Windows to Linux) until WSL is started.

  • 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

Repair or Reset WSL Linux instance

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 and xrdp

#$ 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

wsl not found after windows update
// PowerShell - Adminstrator
PS C:\WINDOWS\system32> DISM /Online /Enable-Feature /All /FeatureName:Microsoft-Hyper-V
cannot ssh into wsl after windows update

windows update changes the wsl ssh config causing ssh from windows using (putty, termius, etc.) to fail. SSH should be possible with localhost:22, and the linux userid/password.

To fix the issue, do the following:

  • From the start menu, find Ubuntu and open

  • Go to /etc/ssh folder, and modify sshd_config to the following:

$ sudo nano /etc/ssh/sshd_config

PasswordAuthentication yes
ChallengeResponseAuthentication yes
  • Restart the ssh server

$ sudo /etc/init.d/ssh restart

Source: https://stackoverflow.com/questions/65196276/sshing-from-windows-10-into-wsl2-ubuntu

wsl2 clock out of sync (Temporary solution)
# From cmd prompt
wsl --shutdown
# then restart
wsl
Error due to WSL time out of sync or not updated

An error occurred (SignatureDoesNotMatch) when calling the GetCallerIdentity operation: Signature expired: 20230804T145316Z is now earlier than 20230805T044911Z (20230805T050411Z - 15 min.)

Run the following command; issue should have been fixed by wsl; see https://stackoverflow.com/questions/65086856/wsl2-clock-is-out-of-sync-with-windows

$ sudo hwclock -s
Setup second instance of WSL Linux

References

Last updated