Windows OS Hub
  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu
  • Home
  • About

Windows OS Hub

  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu

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

September 29, 2022 LinuxPowerShellWindows 10Windows 11

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

In Windows 11, you may also run Windows Subsystem for Andriod (WSA) together with WSL.

1 comment
3
Facebook Twitter Google + Pinterest
previous post
Adding Drivers into VMWare ESXi Installation Image
next post
Using iPerf to Test Network Speed and Bandwidth

Related Reading

Zabbix: How to Get Data from PowerShell Scripts

October 27, 2023

Tracking Printer Usage with Windows Event Viewer Logs

October 19, 2023

PowerShell: Configure Certificate-Based Authentication for Exchange Online (Azure)

October 15, 2023

How to Query and Change Teams User Presence...

October 8, 2023

How to Increase Size of Disk Partition in...

October 5, 2023

1 comment

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

Leave a Comment Cancel Reply

Categories

  • Active Directory
  • Group Policies
  • Exchange Server
  • Microsoft 365
  • Azure
  • Windows 11
  • Windows 10
  • Windows Server 2022
  • Windows Server 2019
  • Windows Server 2016
  • PowerShell
  • VMWare
  • Hyper-V
  • Linux
  • MS Office

Recent Posts

  • Zabbix: How to Get Data from PowerShell Scripts

    October 27, 2023
  • Tracking Printer Usage with Windows Event Viewer Logs

    October 19, 2023
  • PowerShell: Configure Certificate-Based Authentication for Exchange Online (Azure)

    October 15, 2023
  • Reset Root Password in VMware ESXi

    October 12, 2023
  • How to Query and Change Teams User Presence Status with PowerShell

    October 8, 2023
  • How to Increase Size of Disk Partition in Ubuntu

    October 5, 2023
  • How to Use Ansible to Manage Windows Machines

    September 25, 2023
  • Installing Language Pack in Windows 10/11 with PowerShell

    September 15, 2023
  • Configure Email Forwarding for Mailbox on Exchange Server/Microsoft 365

    September 14, 2023
  • How to View and Change BIOS (UEFI) Settings with PowerShell

    September 13, 2023

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
  • How to Access VMFS Datastore from Linux, Windows, or ESXi
  • Using iPerf to Test Network Speed and Bandwidth
  • Installing an Open Source KMS Server (Vlmcsd) on Linux
  • How to Enable Two-Factor Authentication (2FA) for SSH on Linux
  • Install and Configure SNMP on RHEL/CentOS/Fedor
Footer Logo

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


Back To Top