Your whole WSL distro is one file: ext4.vhdx. Copy that file, and you have everything. Home dir, installed packages, configs, all of it.
Store-installed distros:
C:\Users\<user>\AppData\Local\Packages\<DistroFolder>\LocalState\ext4.vhdx
<DistroFolder> looks like CanonicalGroupLimited.Ubuntu_79rhkp1fndgsc.
Find it (cmd):
dir /s /b C:\Users\<user>\AppData\Local\Packages\ext4.vhdx
Find it (PowerShell):
Get-ChildItem C:\Users\<user>\AppData\Local -Recurse -Filter ext4.vhdx
Imported distros live wherever you imported them (for example C:\WSL\<name>). wsl -l -v lists all distros.
Note: AppData is hidden. Use dir /a to see it.
Best method. Shut down WSL first, then export:
wsl --shutdown
wsl --export Ubuntu D:\Backup\ubuntu.tar
Or copy the vhdx directly after wsl --shutdown. Never copy it while the distro runs. You get a corrupt disk.
Boot the Windows installer USB. Press Shift+F10 at the install screen for cmd.
- Drive letters get reshuffled in this environment. Find your real Windows drive:
for %d in (C D E F G H I) do @dir %d:\Windows /b >nul 2>&1 && echo %d: has Windows
- Find the vhdx (use the letter from step 1, here
D:):
dir /s /b D:\Users\<user>\AppData\Local\Packages\ext4.vhdx
- Copy its folder to your backup drive:
robocopy "D:\Users\<user>\AppData\Local\Packages\<DistroFolder>\LocalState" X:\Backup\WSL /E /R:1 /W:1
- Verify. Compare file sizes on both sides with
dirbefore you wipe anything.
Bonus tricks for that environment:
- No GUI? Run
D:\Windows\notepad.exe, then File > Open, file type "All Files". The Open dialog is a mini file explorer. Right-click to copy and paste files. Do not double-click files. - PowerShell (has
ls):D:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe - List drives:
wmic logicaldisk get name,volumename,size
On the fresh Windows, install the WSL engine (admin PowerShell):
wsl --install --no-distribution
Reboot. The import fails until you do.
From a tar export:
wsl --import Ubuntu C:\WSL\Ubuntu D:\Backup\ubuntu.tar
From a vhdx (copies it to the new location):
mkdir C:\WSL\Ubuntu
wsl --import Ubuntu C:\WSL\Ubuntu D:\Backup\WSL\ext4.vhdx --vhd
Or use the vhdx in place, no copy. Warning: the backup file becomes the live disk.
wsl --import-in-place Ubuntu D:\Backup\WSL\ext4.vhdx
Gotcha: if --import says path not found and your source path is correct, create the destination folder first with mkdir.
Imported distros log you in as root. Fix it inside the distro:
printf '\n[user]\ndefault=<linuxuser>\n' >> /etc/wsl.conf
Then restart it:
wsl --terminate Ubuntu
wsl -d Ubuntu
whoami should show your user. Check ls /home if you forgot the name.
wsl -l -v # list all, * marks default
wsl -d <name> # run a specific one
wsl --set-default <name> # change what plain `wsl` opens
wsl --install Ubuntu # fresh distro alongside the old one
wsl --unregister <name> # DELETE a distro and its disk, no undo
Old and fresh distros coexist fine. Name imports something like Ubuntu-old to keep them apart.
- Never copy a vhdx while WSL runs.
wsl --shutdownfirst. - Verify the backup size before you wipe.
- Keep the backup until the restored distro proves itself for a week or two.
wsl --exportbeats raw vhdx copies for planned backups. Do one now and then.