# WSL2

# Installation

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
wsl --set-default-version 2
wsl --install -d kali-linux

# Move location

reg query HKCU\Software\Microsoft\Windows\CurrentVersion\Lxss /s
Get-ChildItem HKCU:\Software\Microsoft\Windows\CurrentVersion\Lxss | ForEach-Object {
 $distro = (Get-ItemProperty $_.PSPath)
    [PSCustomObject]@{
 Name = $distro.DistributionName
 Path = $distro.BasePath
    }
}
wsl --manage kali-linux --move D:\WSL\kali-linux\

# Setting hyper-v launch type

bcdedit /set hypervisorlaunchtype auto
bcdedit /set hypervisorlaunchtype off

# Systemd support

To enable systemd, open your  wsl.conf  file in a text editor using  sudo  for admin permissions and add these lines to the  /etc/wsl.conf :

echo -e "[boot]\nsystemd=true" | sudo tee -a /etc/wsl.conf

# Custom environments

Append follow for login shells, example ~/.profile

cat >>~/.profile <<EOF
# Only for sakura user
if [ -f ~/.bash_sakura ]; then
    . ~/.bash_sakura
fi
EOF

Append follow to ~/.bash_sakura

cat >~/.bash_sakura <<'EOF'
# Open file with explorer.exe
open ()
{
    wp=$(wslpath -w "$1")
    explorer.exe "${wp%*\\}"
}
LS_COLORS=$LS_COLORS:'ow=1;34'
export PATH=$PATH:/mnt/e/assets/shell
EOF

# cloudflare

cat >>~/.ssh/config <<EOF
Host ssh.lostyu.qzz.io
ProxyCommand /mnt/e/assets/software/tools/cloudflared-windows-amd64.exe access ssh --hostname %h
EOF

# Configuring for wsl.conf

cat >/etc/wsl.conf <<EOF
[boot]
systemd = true
[automount]
enabled = true
options = "metadata,umask=022,dmask=22,fmask=33"
EOF

# configuring for .wslconfig

cat >/mnt/c/Users/sakura/.wslconfig <<EOF
# Settings apply across all Linux distros running on WSL 2
[wsl2]
# Limits VM memory to use no more than 8 GB, this can be set as whole numbers using GB or MB
memory=8GB
# Sets the VM to use two virtual processors
processors=8
# Sets additional kernel parameters, in this case enabling older Linux base images such as Centos 6
kernelCommandLine = vsyscall=emulate
# Enable network mirror
networkingMode=mirrored
dnsTunneling=true
firewall=true
autoProxy=true
[experimental]
autoMemoryReclaim=gradual
sparseVhd=true
hostAddressLoopback=true
EOF

# Mount and Unmount USB drive

$ sudo mkdir /mnt/f
$ sudo mount -t drvfs F: /mnt/f
$ sudo umount /mnt/f
[sudo] password for sakura:
umount: /mnt/f: target is busy
$ lsof /mnt/f
COMMAND PID   USER  FD   TYPE DEVICE SIZE/OFF NODE NAME
bash    183 sakura cwd    DIR   0,78      512    2 /mnt/f
lsof    395 sakura cwd    DIR   0,78      512    2 /mnt/f
lsof    396 sakura cwd    DIR   0,78      512    2 /mnt/f
$ sudo kill -9 183

# Troubleshooting

PS C:\Users\sakura> wsl -l -o
无法启动服务,原因可能是已被禁用或与其相关联的设备没有启动。
错误代码: Wsl/0x80070422
PS C:\Users\sakura> Set-Service -Name WSLService -StartupType Automatic
PS C:\Users\sakura> Start-Service -Name WSLService
PS C:\Users\sakura> Get-Service WSLService | Select-Object -Property Name, StartType, Status
Name       StartType  Status
----       ---------  ------
WSLService Automatic Running

The ccleaner performance optimizer sleep microsoft onedrive will disable wslservice automatic.

# Reference

・ https://learn.microsoft.com/en-us/windows/wsl/install

Edited on