Skip to content

Instantly share code, notes, and snippets.

@gorshkov-leonid
Last active February 16, 2024 10:56
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gorshkov-leonid/b55072d6876acecf43dabaf7f1e72cf0 to your computer and use it in GitHub Desktop.
Save gorshkov-leonid/b55072d6876acecf43dabaf7f1e72cf0 to your computer and use it in GitHub Desktop.
Install and configure Ubuntu WSL

Install and configure Ubuntu WSL

Installation

  1. Enable WSL2 as Administrator via Power Shell (information is from link1, link2):
    dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
    dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
    then restart and call after wsl --set-default-version 2
  2. Update WSL from console opened as Administrator wsl --update and restart then using wsl --shutdown.

    If you have problem with self-update, try to use WSL kernel installer or check another tips from this article

  3. Install Ubuntu
    • from Microsoft Windows Store or
    • via cli
      • wsl -l --online
      • wsl --install -d Then check WSL version for installed Ubuntu distribution wsl --list --verbose. It must be 2. If distribution is not default it can be run so wsl -d <Distro>
    • NOTE: You can install not only Ubuntu, but other Linux family OS:
  4. Set up default username with a password
    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: gorshkov
    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.3 LTS (GNU/Linux 4.4.0-19041-Microsoft x86_64)
    
     * Documentation:  https://help.ubuntu.com
     * Management:     https://landscape.canonical.com
     * Support:        https://ubuntu.com/advantage
     
      System information as of Fri Nov  5 00:28:25 +04 2021
    
      System load:    0.52      Processes:              7
      Usage of /home: unknown   Users logged in:        0
      Memory usage:   53%       IPv4 address for eth3:  10.236.151.120
      Swap usage:     0%        IPv4 address for wifi0: 192.168.1.7
    
    1 update can be applied immediately.
    To see these additional updates run: apt list --upgradable
    
    
    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 a day. To disable it please create the
    /home/gorshkov/.hushlogin file.
    
  5. Update all packages
    sudo apt update
    sudo apt upgrade
  6. Optionally upgrade Ubuntu release. It takes usually 1-2 hours
    sudo LANG=C do-release-upgrade
    and check
    $ lsb_release -a
    No LSB modules are available.
    Distributor ID: Ubuntu
    Description:    Ubuntu 21.04
    Release:        21.04
    Codename:       hirsute

In you have a problem like Command terminated with exit status 1 check this tip

  1. In order to run in current folder use command ubuntu.exe run . It can be usefull to add run icons to Total Commander

Configuration

  1. Edit /etc/wsl.conf (see documents)
    [wsl2]
    localhostForwarding=true
    [interop]
    appendWindowsPath=false # To avoid collisions. Any necessary paths can be set manually
    [automount]
    options="metadata" # To avoid this error https://askubuntu.com/questions/1115564/wsl-ubuntu-distro-how-to-solve-operation-not-permitted-on-cloning-repository
    # [user]
    # default=user # can be important to change default user
    then call wsl --shutdown

X-Server

  • Implementation
    • WSLG it should be the fastest solution but only on windows 11 (check if you have win11)
    • GWSL: the simpliest solution for windows 10
      • Run GWSL and click on icon in the status bar
      • Choose GWSL Distro Tools in opened menu
      • Tick Display/Audio auto exporting to set environment variables DISPLAY/PULSE_SERVER in WSL (see ~/.bashrc)
      • To make GWSL start on Windows startup use receipt
    • VcXsrv Windows X Server: classic, that is used by other solutions under hood, as well as in GWSL
      • Run VcXsrv and follow the suggestions
      • Add to ~/.bashrc this line
        export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2; exit;}'):0.0
        
  • How to check
    • sudo apt install x11-apps
    • xeyes

Scripts

  • Mount flash drive
mountdrv() {
  sudo mkdir -p  /mnt/$1
  sudo mount -t drvfs $1: /mnt/$1 -o uid=$(id -u $USER),gid=$(id -g $USER),metadata
}

Convert Docker Container to WSL Distribution

set containerName=centos7
set tar="c:/WSLs/%containerName%.tar"
set DIR=c:/WSLs
set DIR_CENTOS=%DIR%/Centos
mkdir "%DIR_CENTOS%"
wsl --terminate centos7
wsl --unregister centos7
rmdir /S /Q c:\WSLs\Centos
docker run -d -t --rm --name %containerName% -it centos:7 /bin/bash
docker export %containerName% -o %tar%
wsl --import %containerName% "%DIR_CENTOS%" "%tar%"
@gorshkov-leonid
Copy link
Author

Promlem with focus

[wsl2]
guiapplications=false

microsoft/terminal#12134 (comment)

@gorshkov-leonid
Copy link
Author

gorshkov-leonid commented Aug 17, 2023

To fix error like

Logon failure: the user has not been granted the requested logon type at this computer.
Error code: Wsl/Service/CreateInstance/CreateVm/0x80070569

use
Restart-Service vmcompute
or
restart-service vmms
or
gpupdate /force

After this this error will happen from time to time.

But what abou permanent solution?
Try wsl --update

on Error code: Wsl/UpdatePackage/0x8024500c try to run:
wsl --update --web-download

If it "последняя версия уже установлена", то ???

microsoft/WSL#9016


#Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
$p = Start-Process wsl -PassThru -windowstyle Hidden
sleep 1
if($p.ExitCode){
   Restart-Service vmcompute
} else {
   echo justkill
   taskkill /F /PID $p.id
}

#echo $p.id
#echo $p.HasExited
#echo $p.ExitCode
#Wait-Process $app.Id

# -windowstyle Hidden
#Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
#powershell -noexit "& ""C:\SVN\support\geographical-view\map-components\mapbox-component\test.ps1"""

C:\WSLs\ping.ps1:

# Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
# powershell -noexit "C:\WSLs\ping.ps1 ## -noexit 
$p = Start-Process wsl -PassThru -windowstyle Hidden
# sleep 1
if($p.ExitCode){
   # echo "Restart WSL"
   Restart-Service vmcompute
   # sleep 50
} else {
   # echo "Kill"
   # Stop-Process -Id $p.id -Confirm -PassThru
   taskkill /F /PID $p.id
   # sleep 2
}

Then create task with options:
General:

  • Run only when user is logged on
  • Run with highest privelegies
  • Hidden
    Action:
  • Program/script: powershell
  • Add arguments: -noexit "C:\WSLs\ping.ps1"
    Triggers:
  • At startup
  • Delay task for: 5 minutes
  • Repeat task every: 10 minutes
  • For a duration of: Indefinitely
  • Enabled

Try it later

@gorshkov-leonid
Copy link
Author

To fix error like

add to /etc/wsl.conf following content:
[automount] options="metadata"

@gorshkov-leonid
Copy link
Author

To set default use add to /etc/wsl.conf following content:

[user]
default=gorshkov

@gorshkov-leonid
Copy link
Author

@gorshkov-leonid
Copy link
Author

gorshkov-leonid commented Sep 21, 2023

docker/for-win#10985
microsoft/WSL#5401
https://winitpro.ru/index.php/2014/08/15/gpresult-diagnostika-primeneniya-gruppovyx-politik/

The user has not been granted the requested logon type at this computer
Logon failure: the user has not been granted the requested logon type at this computer.
Error code: Wsl/Service/CreateInstance/CreateVm/0x80070569

how to restart
microsoft/WSL#5401 (comment)

галочка на сервисе
microsoft/WSL#5401 (comment)

@gorshkov-leonid
Copy link
Author

gorshkov-leonid commented Oct 4, 2023

How to update kernel

  1. Clone this repo https://github.com/microsoft/WSL2-Linux-Kernel
  2. Build it accordint instruction
  3. Copy build binary somewhere cp ./arch/x86/boot/bzImage /mnt/c/WSLs/kernel6/
  4. Edit %USERPROFILE%.wslconfig:
[wsl2]
kernel=/mnt/WSLs/kernel/bzImage
  1. Restart WSL: wsl --shutdown
  2. Open again

See

  1. https://github.com/microsoft/WSL2-Linux-Kernel
  2. How-to use the Microsoft Linux kernel v6 on Windows Subsystem for Linux version 2 (WSL2)
  3. Advanced settings configuration in WSL

@gorshkov-leonid
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment