76

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?

4
  • 1
    Please post, if and when, you find the answer/solution to this issue. Commented Jul 24, 2020 at 6:03
  • I haven't found anything better than @Daniel B's answer, but that has actually worked. I even got Docker Desktop working fine with the docker-desktop-data distro on D:\ :) Commented Aug 10, 2020 at 20:16
  • @Constantine: See above. Commented Aug 10, 2020 at 20:16
  • You can also install directly to the D drive if desired: superuser.com/questions/1572834/…
    – rogerdpack
    Commented Aug 13, 2023 at 5:25

8 Answers 8

23

(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.

131

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>
14
  • 5
    Just wanted to mention, I initially tried moving the LocalState folder to another drive and using a junction. Obviously it worked, but any incoming updates to the distribution from the Windows Store were a threat to it. So I ended up doing this. This is the long-term sustainable solution. Commented Mar 6, 2021 at 17:52
  • 5
    I confirm it still works in 2021. Maybe the ubuntu executable on your system is slightly different, like "ubuntu2004" in my case. Another small detail is that if you have docker installed, there might be some other images installed. Use command wsl --set-default Ubuntu-20.04 to set the default again. Commented May 17, 2021 at 11:46
  • I've done this to put WSL on an external SSD. Anyway I can "register" or import this onto other machines I plug the SSD into? I don't want to run an export everytime I unplug and import on another machine. Commented Jun 30, 2021 at 14:23
  • 1
    You can configure the default user by editing the /etc/wsl.conf file within the guest VM, as this guide describes.
    – Bass
    Commented Jul 13, 2022 at 9:12
  • 4
    I arrived here because my WSL2 guest system filled up my smallish C drive so I needed to move it to the much larger D. If this happened to you, make sure to switch your Powershell working directory to D: before attempting a wsl --export command. It will keep failing if it attempts to write temp files to full C drive. Change to the target drive first and you'll more likely succeed.
    – gb96
    Commented Jan 10, 2023 at 11:05
21

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
14

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.

13

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\<new_distro_name>

wsl --import <new_distro_name> d:\WSL\distros\<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>
1
  • 1
    you can also rename it first or afterwards if you want to keep the name with regedit under Computer\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Lxss\... and set DistrubutionName accordingly. Commented Jul 26, 2023 at 14:58
5

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 BashPath entry in Registry Editor to point to this new location.

  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.

1
  • It works good. Like my case, First file location has changed, then this will be the easy solution.
    – agfe2
    Commented Mar 4 at 9:00
2

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.

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .