Wsl

Installation

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
# OR
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
Enable-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform

Disable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
Disable-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform

Download a distribution

Build a distribution

Create rootfs from Docker image

sudo su -
mkdir rootfs && docker export $(docker create archlinux) | tar -C rootfs -xf -
cd rootfs && tar czf ../rootfs.tar.gz --hard-dereference * && cd ..

Create rootfs from ISO

sudo su -
unsquash -d rootfs /path/to/distro.sfs to extract file system
cd rootfs && tar czf ../rootfs.tar.gz --hard-dereference * && cd ..

Install distro

  1. Create distro folder
  2. Download Launcher.exe or icons.zip from here
  3. Extract rootfs.tar.gz into distro folder
  4. Run "distro.exe" to install
  5. Run "distro.exe --help" to configure distro

Import a distribution

  1. Find the distro of choice at https://jenkins.linuxcontainers.org/view/Images/
  2. Download the rootfs.tar.xz for that distro
  3. Unpack it using tar or 7zip to get rootfs.tar
  4. Import using wsl --import <distro name> <distro folder> <path to rootfs.tar>
    • import --import Debian C:\Users\jdoe\WSL\debian C:\Debian\rootfs.tar

Compacting WSL ext4.vhdx

wsl --shutdown
diskpart
select vdisk file="C:\path\to\ext4.vhdx"
compact vdisk

Build kernel

  1. Install required packages
    sudo apt install g++ make flex bison libssl-dev libelf-dev bc
    
  2. Download new kernel source from https://github.com/microsoft/WSL2-Linux-Kernel/releases
  3. Extract, change into folder, and run
    make KCONFIG_CONFIG=Microsoft/config-wsl
    
  4. Copy built kernel in arch/x86/boot/bzImage to Windows folder, i.e.: /mnt/c/Users/jdoe/kernel
  5. Add line below to C:\Users\jdoe\.wslconfig under [wsl2] section
    [wsl2]
    kernel=C:\\Users\\jdoe\\kernel
    
  6. Restart WSL using "wsl --shutdown" or restart LxssManager service
  7. Check kernel using "uname -r"

Systemd

Arch

# As root
pacman-key --init
pacman-key --populate archlinux
pacman-key --refresh-keys
pacman -S archlinux-keyring
pacman -Syu
pacman -S --noconfirm base-devel curl git go vi vim wget
user=ctran
useradd -d /home/$user $user
mkdir /home/$user
chown $user:$user /home/$user
passwd $user
su - $user

# As user with sudo
git clone https://aur.archlinux.org/yay.git
cd yay && makepkg -si
yay -S --noconfirm daemonize
wget https://github.com/arkane-systems/genie/releases/download/v1.42/genie-systemd-1.42-1-x86_64.pkg.tar.zst
sudo pacman -U --noconfirm ./genie-systemd-1.42-1-x86_64.pkg.tar.zst

# Shortcut
mintty.exe --WSL="Arch" --configdir="C:\Users\jdoe\AppData\Roaming\wsltty" -~ genie -s

CentOS 8

yum install -y curl git vi vim wget
wget https://github.com/arkane-systems/genie/releases/download/v1.42/genie-1.42-1.fc33.x86_64.rpm
yum -y install ./genie-1.42-1.fc33.x86_64.rpm

# Shortcut
mintty.exe --WSL="CentOS8" --configdir="C:\Users\jdoe\AppData\Roaming\wsltty" -~ genie -s

Ubuntu/Debian

apt install -y curl git vi vim wget
wget https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb
apt install -y ./packages-microsoft-prod.deb
apt update && apt install -y dotnet-runtime-5.0
wget https://github.com/arkane-systems/genie/releases/download/v1.42/systemd-genie_1.42_amd64.deb
apt install -y ./systemd-genie_1.42_amd64.deb

# wsltty shortcut
mintty.exe --WSL="Ubuntu-20.04" --configdir="C:\Users\jdoe\AppData\Roaming\wsltty" -~ genie -c zsh

“Waiting for systemd” hang fix

# Ubuntu
sudo mv /usr/lib/systemd/system/systemd-remount-fs.service /usr/lib/systemd/system/systemd-remount-fs.bak
sudo systemctl disable multipathd.socket

# CentOS 8
sudo mv /usr/lib/systemd/system/systemd-remount-fs.service /usr/lib/systemd/system/systemd-remount-fs.bak
sudo mv /usr/lib/systemd/system/systemd-modules-load.service /usr/lib/systemd/system/systemd-modules-load.bak
sudo systemctl disable auditd

# Use command below to list failed service and disable/remove them
sudo systemctl list-units --failed

Restart WSL with wsl --shutdown

DNS Resolution

Simple

  1. Create/edit /etc/wsl.conf with the following lines
    [network]
    generateResolvConf = false
    
  2. Remove /etc/resolv.conf if it’s a symlink
  3. Create/edit /etc/resolv.conf with lines below
    search domain1 domain1.com
    nameserver 10.10.10.50
    
  4. Restart WSL with wsl --shutdown in CMD

Better - Using dnsmasq

NOTES: Install systemd above first

  1. Create/edit /etc/wsl.conf with the following lines
    [network]
    generateResolvConf = false
    
  2. Install dnsmasq using apt, yum, or pacman
  3. Create/edit /etc/dnsmasq.conf
    listen-address=0.0.0.0,127.0.0.1
    server=/domain1/domain1.com/domain2/domain2.com/10.10.10.50
    server=/domain2/domain2.com/10.10.10.51
    server=8.8.8.8
    server=8.8.4.4
    
  4. Enable and start dnsmasq service
    sudo systemctl enable dnsmasq
    sudo systemctl start dnsmasq
    
  5. Remove /etc/resolv.conf if it’s a symlink
  6. Create/edit /etc/resolv.conf with lines below
    search domain1 domain1.com
    nameserver 127.0.0.1
    
  7. Restart WSL with wsl --shutdown in CMD

Docker Fix

sudo vim /etc/docker/daemon.json
  { "iptables": false }

VPN Fix

  1. Run command below in Powershell as administrator
    Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser
    
  2. Create a ps1 file, vpnfix.ps1 for example, with content below
    Get-NetAdapter | Where-Object {$_.InterfaceDescription -Match "AnyConnect"} | Set-NetIPInterface -InterfaceMetric 9000
    
  3. Create a shortcut for powershell.exe pointing to that ps1 file
    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -f "C:\path\to\vpnfix.ps1"
    
  4. Change that shortcut to run as Administrator
    • Right click -> Properties -> Shortcut tab -> Advanced -> Run as administrator
  5. Run this shortcut every time after connecting to VPN

NAT fix

# Required for Debian
sudo update-alternatives --set iptables /usr/sbin/iptables-legacy
sudo update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy

cgroup fix

# Required for Debian and Ubuntu
sudo mkdir /sys/fs/cgroup/systemd
sudo mount -t cgroup -o none,name=systemd cgroup /sys/fs/cgroup/systemd

Kali shell fix

sudo vim /usr/lib/win-kex/xstartup
  export SHELL=/bin/zsh

References