Windows OS Hub
  • Windows
    • Windows 11
    • Windows Server 2022
    • Windows 10
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
  • PowerShell
  • Linux
  • Home
  • About

Windows OS Hub

  • Windows
    • Windows 11
    • Windows Server 2022
    • Windows 10
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
  • PowerShell
  • Linux

 Windows OS Hub / Windows 10 / Moving WSL to Another Drive in Windows

March 11, 2024

Moving WSL to Another Drive in Windows

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

list installed wsl images in windows

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}}

get wsl fyle system image - ext4.vhdx

You can try to reduce the size of the VHD file using the Optimize-VHD cmdlet (from the Hyper-V PowerShell module). Sometimes it is enough:

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.

wsl: get username whoami

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.

move wsl file system to another drive

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

start wsl

Tip. You can start your WSL distro from the Windows Terminal tab. run wsl from windows terminal

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

using lxrunoffline to move wsl

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

5 comments
5
Facebook Twitter Google + Pinterest
LinuxPowerShellWindows 10Windows 11
previous post
Adding Drivers into VMWare ESXi Installation Image
next post
Using iPerf to Test Network Speed and Bandwidth

Related Reading

How to Repair EFI/GPT Bootloader on Windows 10...

March 16, 2024

How to Restore Deleted EFI System Partition in...

March 11, 2024

How to Allow Multiple RDP Sessions on Windows...

March 15, 2024

How to Run Program without Admin Privileges and...

June 8, 2023

Wi-Fi (Internet) Disconnects After Sleep or Hibernation on...

March 15, 2024

How to Install Remote Server Administration Tools (RSAT)...

March 17, 2024

How to Repair Windows Boot Manager, BCD and...

March 11, 2024

Refresh AD Groups Membership without Reboot/Logoff

March 15, 2024

5 comments

go to hell October 20, 2023 - 5:43 pm

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.

Reply
Geo September 7, 2024 - 7:41 am

Thanks for the post, it’s just what i needed. I hadn’t any problems with it.

Reply
matt November 18, 2024 - 4:43 pm

Be carefull! With these instructions I was able to move the linux distribution to another drive and re-register it. However when launching it I was required to create a new user, and all of the old user files under HOME were gone.

Reply
matt November 18, 2024 - 5:18 pm

I found the missing home files under “%USERPROFILE%\wsl”

Reply
admin November 26, 2024 - 5:55 am

Thanks for clarifying!

Reply

Leave a Comment Cancel Reply

join us telegram channel https://t.me/woshub
Join WindowsHub Telegram channel to get the latest updates!

Recent Posts

  • Map a Network Drive over SSH (SSHFS) in Windows

    May 13, 2025
  • Configure NTP Time Source for Active Directory Domain

    May 6, 2025
  • Cannot Install Network Adapter Drivers on Windows Server

    April 29, 2025
  • Change BIOS from Legacy to UEFI without Reinstalling Windows

    April 21, 2025
  • How to Prefer IPv4 over IPv6 in Windows Networks

    April 9, 2025
  • Load Drivers from WinPE or Recovery CMD

    March 26, 2025
  • How to Block Common (Weak) Passwords in Active Directory

    March 25, 2025
  • Fix: The referenced assembly could not be found error (0x80073701) on Windows

    March 17, 2025
  • Exclude a Specific User or Computer from Group Policy

    March 12, 2025
  • AD Domain Join: Computer Account Re-use Blocked

    March 11, 2025

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • Fixing ‘The Network Path Was Not Found’ 0x80070035 Error Code on Windows
  • Recovering Files from BitLocker Encrypted Drive
  • Installing an Open Source KMS Server (Vlmcsd) on Linux
  • How to Access VMFS Datastore from Linux, Windows, or ESXi
  • Using iPerf to Test Network Speed and Bandwidth
  • Monitoring Domain Name Expiration Date with Zabbix
  • How to Install and Use ClamAV Antivirus on CentOS/RHEL
Footer Logo

@2014 - 2024 - Windows OS Hub. All about operating systems for sysadmins


Back To Top