71

I've installed wsl2 on my windows machine and I was not able to figure out where the files are actually stored.
Note, that I don't mean that I wanna browse them inside the file explorer - I know it can be done by typing in the explorer \\wsl$\.
If I would have to guess I would say the files are stored in the same hard-drive that the os is stored.

So actually I have two related questions.

  1. Where the files are stored?
  2. If they are stored in the hard drive of my os, can I somehow relocate my wsl to another hard drive?

EDIT:
I was able to locate the installation path, in my machine the path is:
C:\Users\Eliran\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu20.04onWindows_79rhkp1fndgsc\LocalState

Is there a way to mount this to another location?

Dweeberly
  • 4,668
  • 2
  • 22
  • 41
Eliran Turgeman
  • 1,526
  • 2
  • 16
  • 34
  • 17
    @Braiam I would argue that WSL is a software development tool just as much as any IDE. Certainly as much as containers. At least that is its intended purpose. MS have even made it one of the 3 extensions that make up their new remote development suite (Forgive me, I can't remember the name.) According to the [help center](https://stackoverflow.com/help/on-topic) bullet-points, it is ok. Would you agree. Not trying to be disagreeable; I am genuinely curious. I am still learning the rules myself. – Nate T Jul 30 '21 at 04:35
  • @NateT and so it's windows, linux, cpus and coffee machines, which doesn't make any question about those on topic. It has to be unique to software development. This is not even software development, it's asking for details about the implementation of a user feature. – Braiam Jul 30 '21 at 12:08
  • @Braiam coffee machines... If only. We need a site for that :D Good point – Nate T Jul 31 '21 at 03:21
  • 5
    @Braiam maybe this question should be migrated to [ServerFault](https://serverfault.com/) or another S.E. forum? I see Braiam's point, but the question is still useful. – John Polo Sep 09 '21 at 13:18
  • 1
    @JohnPolo there are several questions questions about that on SU already. No need. Those questions also rank higher than this one. – Braiam Sep 09 '21 at 14:43
  • My immediate gut feeling was that this question is ON-topic. But, apparently, it is [not considered to be _unique to software development_](https://meta.stackoverflow.com/a/413324), something I cannot argue against. I suggest [trying this](https://superuser.com/tags/wsl?tab=Votes) or else just Google search, for example ["Windows Subsystem for Linux" site:superuser.com](https://www.google.com/search?as_q="Windows+Subsystem+for+Linux"+site%3Asuperuser.com). – Henke Aug 02 '22 at 14:52
  • You can install to an arbitrary location https://superuser.com/questions/1572834/is-there-any-way-to-install-wsl-on-non-c-drive – rogerdpack Aug 13 '23 at 05:28

3 Answers3

42

All the files are stored in a ext4.vhd files in the installation directory, which you can't mount directly onto windows as it is in ext4 (obv)

There's two ways to change the location of the above mentioned vhd file the official, tedious way and an unofficial quick and dirty way

The official tedious way

  1. Export the distro to a location with wsl.exe --export <Distro> <FileName> from CMD/PowerShell
  2. Import the distro to a different location with wsl.exe --import <Distro> <InstallLocation> <FileName> [Options]

The problems with this is it's quite time consuming and after you do this, pray that it exported and imported several gigabytes worth of thousands of files without any problems

The quick and dirty way

This involes an unofficial opensource WSL manager called lxrunoffline

To install it (takes like a min at max) read through the instructions by the dev here

If you installed it by manually downloading the binaries from the release page, make sure to install it to a directory in PATH, like C:\Windows

Now the process is simple as lxrunoffline move -n <distroname> -d <destination-folder>

For example lxrunoffline move -n Ubuntu-20.04 -d G:\wsl\

Hope I helped

Edit: typo

zwxi
  • 782
  • 7
  • 9
20

I executed these commands in PowerShell to move my Ubuntu distro from C: to drive D:\wsl-ubuntu :

PS C:\Users\smarc> mkdir D:\wsl-ubuntu  (create new location)
PS C:\Users\smarc> wsl -l -v            (list wsl distros)
NAME                   STATE           VERSION
Ubuntu                 Running         2
PS C:\Users\smarc> wsl --shutdown
PS C:\Users\smarc> wsl -l -v            (verify if is stopped)
NAME                   STATE           VERSION
Ubuntu                 Stopped         2
PS C:\Users\smarc> wsl --export Ubuntu ubuntu.tar  
PS C:\Users\smarc> wsl --unregister Ubuntu
PS C:\Users\smarc> wsl --import Ubuntu D:\wsl-ubuntu\ .\ubuntu.tar --version 2

and reboot the computer at the end.

The only problem I have is that the default user when I started the Ubuntu application is the root. I need to execute $ su sergio to enter in my personal user.

You can delete the ubuntu.tar at the end of process.

#edit 2021-04-13: As pointed out in the comments, I had forgotten the "--export" command.

  • 2
    It feels like you are missing the `wsl --export` step from your answer, right? Also, see [this answer](https://superuser.com/a/1627461/1210833) for information on how to set the username for the newly imported instance. – NotTheDr01ds Apr 12 '21 at 22:44
  • And my personal preference (everyone's will vary, I'm sure) is to create something like `%userprofile%\Documents\WSL\instances` (or `VMs` or `distros`) for the imported instances themselves, and `%userprofile%\Documents\WSL\images` for the exported `.tar` files. That way you can `cd $env:userprofile\Documents\WSL` (in PowerShell) and have easy access to both the images and instances on your system. – NotTheDr01ds Apr 12 '21 at 22:48
  • And there's no reason to reboot, in my experience. The new instance should be available immediately. – NotTheDr01ds Apr 12 '21 at 22:50
  • oh my Gosh! @NotTheDr01ds is right! I missing copy the --export line. I will edit the answer. – Sergio Marcelo C Figueiredo Apr 14 '21 at 00:22
  • 5
    You can solve the issue with your default user with the following command, execute in PS: ` config --default-user `, in ur case `Ubuntu config --default-user sergio` – cm101 Sep 14 '21 at 07:30
  • 2
    for default user issue: create ```echo -e "[user]\ndefault=abhasker" >> /etc/wsl.conf``` and then restart ```wsl --shutdown```. – AbhiNickz Oct 13 '21 at 16:18
  • 1
    If transferring to another machine (esp. something problematic like a Win10 to Win11 transfar), the only working solution I could find was to create a new "empty" container `wsl --install Ubuntu-20.04` (set a username matching your previous install), `wsl -t Ubuntu-20.04`, then replace the newly created ext4.vhdx with your original. See other tips for various methods of moving it onces it's transferred. _note: source was Ubuntu-20.04, adjust your commands to suit.` – Orwellophile Feb 23 '23 at 00:17
11

This is an answer to your last question: use symbolic links

  • open command prompt as administrator
  • shut down wsl vm using wsl --shutdown
  • change folder to C:\Users\Eliran\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu20.04onWindows_79rhkp1fndgsc\
  • move the LocalState folder to another location like Z:\wsl\Ubuntu\
  • create symbolic link with mklink /J LocalState Z:\WSL\Ubuntu\LocalState

I would also edit/create the .wslconfig file from your user folder to move the swap file to the folder where you store your WSL vm's and maybe edit/add options for CPU cores and RAM assignment

[wsl2]
memory=4GB
processors=2
swap=1GB
swapFile=Z:\\WSL\\swap.vhdx
  • memory is the maximum amount your ram that WSL will use;
  • processors is the alocated cores to your WSL vm;
  • swap is the size of the swap file;
  • swapFile is the location of your swap and to my knowledge is used by all WSL vm's; notice the double slashes in the path, they are mandatory for the path.

Start your WSL VM as you normally would.

Gregory Butler
  • 384
  • 2
  • 5