Windows Subsystem for Linux (WSL2) is widely used by developers, administrators, and common users to run different Linux distros (Ubuntu, Debian, OpenSUSE, Kali Linux, Alpine), tools, and apps in Windows without using virtualization and dual boot. In this article, we will show how to move the files of an existing WSL installation to a different drive or computer.
When you install WSL in Windows, all environment files are saved to the system drive C:\. If your system drive is not large enough (for example, SSD), the size of the WSL file system may grow significantly and you may need to move WSL to another drive or computer. You can move your WSL installation in several ways.
First, get the current size of the WSL file system on your current drive. List the installed WSL distributions on your computer:
wsl --list --verbose
In this example, only Ubuntu-20.04 is installed. Run the following PowerShell command to get the name of the UWP app with your Linux distros:
Get-AppxPackage -Name "*Ubuntu20*" | Select PackageFamilyName
In my example, the name of the UWP package is CanonicalGroupLimited.Ubuntu20.04onWindows_79rhkp1fndgsc. The WSL file system of the Linux image is located in the ext4.vhdx VHD file in %USERPROFILE%\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu20.04onWindows_79rhkp1fndgsc\LocalState\ext4.vhdx
.
To get a file size using PowerShell, run the command below:
$path= $env:USERPROFILE+ "\AppData\Local\Packages\" + (Get-AppxPackage -Name "*Ubuntu20*").PackageFamilyName + "\LocalState\ext4.vhdx"
Get-ChildItem -Path $path | fl @{Label="SizeGb"; Expression={$_.Length / 1Gb}}
optimize-vhd -Path <PATH_TO_VHD> -Mode full
The path to the VHD file of the WSL distribution is stored in the BasePath registry parameter under the registry key HKCU\Software\Microsoft\Windows\CurrentVersion\Lxss\{UUID}
.
You may stop WSL, move ext4.vhdx to another drive, and change the path to the directory in the registry. But this scenario is not recommended!
In current Windows 10 and 11 builds, you can migrate your WSL environment correctly using the built-in export and import procedure.
Run the WSL environment and check the user name:
wsl
whoami
In our example, it is sysops.
Close all apps running in your Linux environment and WSL console:
wsl --shutdown
In order to backup (export) your WSL environment and save it to a drive E:, run the commands:
mkdir e:\backup
wsl --export Ubuntu-20.04 e:\backup\ubuntu.tar
Wait till the WSL export is over (it may take much time). A TAR archive with your WSL ext4 file system will appear in the target directory.
Then you may remove the WSL files on the source disk:
wsl --unregister Ubuntu-20.04
Create a directory for your Linux image on a new drive and import the TAR archive to WSL using this command:
mkdir E:\WSL
wsl --import Ubuntu-20.04 E:\WSL\ E:\backup\ubuntu.tar
The default login to Ubuntu is root. To change it to another user name (we got it earlier), run:
cd $env:USERPROFILE\AppData\Local\Microsoft\WindowsApps
.\ubuntu2004.exe config --default-user sysops
Start the WSL environment:
wsl -d Ubuntu-20.04
You may use the same scenario to move a configured WSL image to other computers.
You may use a third-party utility LxRunOffline (available on GitHub — https://github.com/DDoSolitary/LxRunOffline) to move WSL in Windows. You can install it using Chocolatey:
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
choco install lxrunoffline
List available WSL images:
lxrunoffline list
Terminate all WSL processes:
wsl --shutdown
To get the current directory, the WSL image is located in:
lxrunoffline get-dir -n Ubuntu-20.04
To move a WSL image to another disk:
lxrunoffline move -n Ubuntu-20.04 -d d:\wsl2\Ubuntu-20.04
After the migration is complete, start the WSL using this command:
wsl -d Ubuntu-20.04
or
lxrunoffline run -n Ubuntu-20.04 -w
1 comment
Thanks to this tutorial, which i followed line after line, I lost all the setup done on my WSL2 and now i can’t even uninstall because you broke something in my system.