93

As opposed to in WSL1, official docs for WSL2 state clearly that I should place files I access frequently from Linux under the Linux file system. However, I have not found any docs on settings for where inside the Windows file system the Linux file system is placed.

Of course, the default location (somewhere under %LOCALAPPDATA%) is suboptimal if you - like me - have your machine configured to have a minimal C:\ drive and a much larger D:\ - I would much rather place the Linux file system there, to make use of all that space.

Can I somehow control where my Linux file system is placed?

Tomas Aschan
  • 3,556

9 Answers9

158

If you want to move WSL2 Linux distro(Ubuntu) from C: drive D: below are the steps.

Export Ubuntu

  • mkdir D:\backup
  • wsl --export Ubuntu D:\backup\ubuntu.tar

Unregister the same distribution to remove it from the C: drive:

  • wsl --unregister Ubuntu

Import Ubuntu

  • mkdir D:\wsl
  • wsl --import Ubuntu D:\wsl\ D:\backup\ubuntu.tar

By default Ubuntu will use root as the default user, to switch back to previous user

Go to the Ubuntu App Folder run command to set default user

  • cd %userprofile%\AppData\Local\Microsoft\WindowsApps
  • ubuntu config --default-user <username>
45

As of WSL 2.3.11+ versions, there is a built-in command-line --move option.

Assuming you are on the "App" version of WSL (most installations in the last 2 years), you can update with:

wsl --update

You can then move any WSL2 (not WSL1) distribution using:

wsl --manage <distro_name> --move <new_location>

This should essentially have the same end-result as @TiborTakács' answer.


Original answer for older WSL releases

The latest versions of WSL2 include a new wsl --import --vhd option to directly copy the existing VHD (virtual SDD drive for your WSL2 distribution) to another location. Note that this will only work with WSL2 distributions, not WSL1.

Also note that it is important to pick a new, different name for the copied distribution. For instance, if your distribution is "Ubuntu", the new name could be "my_Ubuntu" (I call mine "ntd_Ubuntu").

Exit WSL (and, if you use it, shut down Docker Desktop), then from PowerShell (a regular, non-admin terminal):

wsl --shutdown

Find the location of the existing distribution:

Get-ChildItem HKCU:\Software\Microsoft\Windows\CurrentVersion\Lxss\ | ForEach-Object { (Get-ItemProperty $_.PSPATH) | Select-Object DistributionName,BasePath }

Copy the <BasePath> of the distribution you want to copy

to the clipboard so that you can paste it into the --import command

below.

Create a directory on the other drive for the distro

mkdir d:\WSL\distros&lt;new_distro_name>

wsl --import <new_distro_name> d:\WSL\distros&lt;new_distro_name> <BasePath>\ext4.vhdx --vhd

WSL will create a new distribution named <new_distro_name> in the directory on D:.

You can start this distribution with:

wsl ~ -d <new_distro_name>

Because it is a new distribution, WSL needs to be told what the default username is. In the meantime, it will start as the root user. Use this answer to create a /etc/wsl.conf file to specify the default user.

When you are satisfied that the new distribution is working the same as the old, you can remove the old with:

# Warning: This is a destructive and unrecoverable operation
# Please ensure the correct distribution name
wsl --unregister <old_distro_name>

You can set the new distribution as default with:

wsl --set-default <new_distro_name>
rklec
  • 103
NotTheDr01ds
  • 28,025
29

(I don’t have WSL 2 to try.)

WSL supports exporting and importing distributions, like this:

wsl --export Debian C:\temp\Debian.tar
wsl --import MovedDebian C:\WSL\Debian C:\temp\Debian.tar --version 2

Note how I can specify a parameter called InstallLocation (C:\WSL\Debian). Keep in mind that in my example, the “Debian” distribution still exists, so I select a different name when importing. You can use the wsl utility to manage your WSL environments.

user219095
  • 65,551
25

This is an example with ubuntu18.04.
There was couple places the above wasn't detailed enough. Hopefully, this helps.

wsl --list --verbose
mkdir d:\backuplinux
wsl --export Ubuntu-18.04 d:\backuplinux\ubuntu.tar
wsl --unregister Ubuntu-18.04
mkdir d:\wsl
wsl --import Ubuntu-18.04 d:\wsl\ d:\backuplinux\ubuntu.tar
cd %userprofile%\AppData\Local\Microsoft\WindowsApps            **(this is not needed if in path)**
ubuntu1804.exe config --default-user yourloginname
zx485
  • 2,337
catonyx
  • 351
  • 3
  • 2
17

In the same spirit, this is an example with ubuntu20.04 on Windows 10.
It was tested on my desktop. It saved 30GB on my C drive.

The Ubuntu disk is located at %userprofile%\AppData\Local\Packages
CanonicalGroupLimited.Ubuntu20.04onWindows_...\LocalState\ext4.

First, shutdown wsl and confirm the name.

wsl --shutdown
wsl --list --verbose

NAME STATE VERSION

  • Ubuntu-20.04 Stopped 2

Then you can export the tar file and import it to D drive:

mkdir d:\backuplinux
wsl --export Ubuntu-20.04 d:\backuplinux\ubuntu.tar
wsl --unregister Ubuntu-20.04
mkdir d:\wsl
wsl --import Ubuntu-20.04 d:\wsl\ d:\backuplinux\ubuntu.tar
ubuntu2004.exe config --default-user yourloginname

Check d:\wsl, the ext4 file is moved here. Also check the free space on your C drive. You should see a big difference.

12

I have found an approach that does not require deleting (unregistering) and importing an existing distibution.

  1. Stop the distribution you want to relocate: wsl --terminate Ubuntu or wsl --shutdown.

  2. Create a backup, just to be sure: wsl --export Ubuntu D:\Ubuntu-backup.tar.

  3. Start the Registry Editor (regedit.exe - it requires elevated permissions).

  4. Navigate to Computer\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Lxss\. Here, you can find all the existing distributions. Find the one that you want to relocate based on the DistributionName entry (in my case, it is Ubuntu).

  5. The BasePath entry shows the current location of the disk. In my case: C:\Users\myuser\AppData\Local\Packages\CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc\LocalState. If you open this folder, you will see that it has one ext4.vhdx file. This is the disk file.

  6. Copy this file to the location where you want to have it. For example, cp C:\Users\myuser\AppData\Local\Packages\CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc\LocalState\ext4.vhdx D:\wsl\Ubuntu\.

  7. Change the BasePath entry in Registry Editor to point to the new location of the ext4.vhdx.

  8. Just start the WSL distribution, it will use the disk in the new location. At this point, you can delete the disk file from the original location.

3

Cross post from another answer

  1. wsl --terminate distro_name
  2. move the ext4.vhdx file to new_location
  3. wsl --unregister distro_name
  4. wsl --import-in-place distro_name ext4.vhdx_file_in_new_location

Note you need to be on the latest version, but I can verify that this works.

TombMedia
  • 131
0

Note that after completing the accepted answer, you need to set Ubuntu as default distro and start it up again. Otherwise, the system will default to Docker.

Here is how to do it:

$ wsl --set-default Ubuntu
$ wsl -d Ubuntu
0

There is a new method to move your WSL filesystem to wherever you want. Let's say you want to move Ubuntu (stored in C: by default) to D: drive. You just do:

wsl --manage Ubuntu --move D:\WSL\Ubuntu

This takes less time & disk space than exporting & importing!