45

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
  • 2,986
  • 10
  • 35
  • 38

7 Answers7

89

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>
Jayesh Vachhani
  • 991
  • 1
  • 4
  • 2
  • 3
    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. – Param Siddharth Mar 06 '21 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. – Mário Meyrelles May 17 '21 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. – Daniel Dewhurst Jun 30 '21 at 14:23
  • After importing, the wsl file system was blocked for some reason with message, some other process was still using it and I could not find out which one. So I just restarted Windows and it worked well after. – kolodi Jul 09 '21 at 12:39
  • 1
    You can configure the default user by editing the `/etc/wsl.conf` file within the guest VM, as [this guide](https://docs.microsoft.com/en-us/windows/wsl/wsl-config) describes. – Bass Jul 13 '22 at 09:12
  • Best answer! better than selected one – DivinesLight Oct 26 '22 at 23:39
  • 3
    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 Jan 10 '23 at 11:05
  • Has anyone tried it with Docker for Desktop? It seems to take a lot of space. I wonder, will I break it if I try the same trick with it. – Michael Kruglos Feb 03 '23 at 12:59
  • If you encounter the `Unspecified error Error code: Wsl/Service/E_FAIL` error when using this method, try using a relative path after navigating to the D drive as suggested in [this issue comment](https://github.com/microsoft/WSL/issues/4735#issuecomment-1437996023). – Jason Lee Mar 03 '23 at 07:49
18

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,170
  • 11
  • 17
  • 24
catonyx
  • 181
  • 1
  • 2
15

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

Daniel B
  • 60,360
  • 9
  • 122
  • 163
9

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.

Steve Lihn
  • 191
  • 1
  • 1
6

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>
NotTheDr01ds
  • 17,574
  • 4
  • 44
  • 81
  • 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. – Miron Tewfik Jul 26 '23 at 14:58
1

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
  • 111
  • 3
0

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.