Skip to content

Windows Subsystem for Linux

Stuart Hicks edited this page Apr 18, 2024 · 4 revisions

WSL integration

GUI Environments

These snippets only work in WSL2.

X410

https://x410.dev/ (This should also work for any other third-party X server)

export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2; exit;}'):0.0

WSLg (Wayland)

https://github.com/microsoft/wslg

export DISPLAY=:0

Default browser

This makes the default browser in Windows, the default browser in WSL.

export BROWSER="powershell.exe /C start"

(If your WSL distro has wslutilities installed, then you can also register wslview to update the default browser, eg wslview -r).

wsl-sudo

https://github.com/Chronial/wsl-sudo

Maintenance

Shrinking Disk Space

This is only really useful if you free up a load of space within WSL, and then want to shrink the volume to make more space available to Windows. Make a copy of this script, and update the path to the WSL virtual disk depending on your system:

select vdisk file="C:\Users\stuar\AppData\Local\Packages\WhitewaterFoundryLtd.Co.16571368D6CFF_kd1vv0z0vy70w\LocalState\ext4.vhdx"  
compact vdisk
  1. Shut down WSL using wsl --shutdown in Windows.
  2. Open a PowerShell prompt with elevated permissions.
  3. Launch diskpart with the path to the script, eg: diskpart /s C:\Users\stuar\Documents\shrink_wsl.txt
  4. If it fails, reboot and try again (likely something keeping a file handle open on the vhdx file).

Move WSL disk image to another drive

  1. Shut down WSL: wsl --shutdown
  2. Navigate to [HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Lxss\{f063e071-a1b6-48b3-a125-366eebf80011}] in the windows registry (this is for fedoraremix. The UUID will be different for different WSL distros. Find the right one for your system)
  3. Set BasePath to the new directory that will contain the ext4.vhdx file.
  4. Move the file to the new target dir.
  5. Done!