2

I would like to know how I can change the location where I store the folder that contains the information of a Debian WSL2 container. According to the Microsoft Doc it is located in

C:\Users\<UserName>\.wslconfig 

but I cannot find this folder

Testing a solution

%UserProfile%\wslconfig.

Image

Image translated text:

Windows can't find 'C:/Users/David/wslconfig'. Check the spelling and try again
4
  • Nothing is stored in the path you have indicated. Look for the file in %UserProfile%\wslconfig. I have no way to check this (I use WSL1) but I do know just by looking at it that your path is wrong. Commented Aug 11, 2022 at 20:38
  • @SeñorCMasMas Hello, I show you what appears to me when doing what you tell me. Sorry if you don't understand the text of the image, I have the pc in Spanish Commented Aug 11, 2022 at 20:49
  • Entiendo el texto. No vi la foto. Lo siento hermano. No tengo nada ahora. :( Commented Aug 11, 2022 at 21:52
  • @DavidGonzalo - In that case you should provide a translation to English. You are getting that error because you need to create the error. It's also a configuration file, it's not the actual location of the distribution, that exists within a directory contained in %LocalAppData%
    – Ramhound
    Commented Aug 11, 2022 at 23:20

1 Answer 1

5

Note: I've discovered that there's an earlier version of this question here. I've posted a new answer on that question with information on the wsl --import --vhd option, which wasn't available at the time I wrote this answer. I'm leaving this one in place, since it did ask a slightly different question (that I answered) as well, but most users should see the updated answer on the earlier question.


Older answer:

A few notes:

According to the Microsoft Doc it is located in

C:\Users\<UserName>\.wslconfig

Well, no, that's not where the distribution is stored for WSL. That's simply one of the WSL configuration files. You can't find it because it doesn't exist (nor is needed) by default. You would only need to create it if you wanted to override one of the default settings.

WSL distributions installed from the Microsoft Store will typically be in a package specific directory under:

%userprofile%\AppData\Local\Packages\

You can get the actual location by running the following in PowerShell:

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

But with that said, you shouldn't access this directory directly.

Instead, use the built-in WSL features to:

  • backup the existing distribution and restore it to the new location
  • set the default username in the new distribution
  • unregister the old distribution

Specifically:

Please don't just copy and paste the commands below without attempting to understand what they are doing. Please run each command separately, so that you can handle any errors that might occur.

First, exit the existing distribution (Debian, in your case).

Then, in PowerShell (as a normal user, not Admin):

wsl -l -v
# Confirm the existing distribution name
# Modify variables below as needed/desired
$WSL_ROOT="D:\WSL"
$WSL_IMAGE_PATH="${WSL_ROOT}\images"
$WSL_OLD_DISTRO_NAME="Debian"
$WSL_NEW_DISTRO_NAME="Debian_WSL2"
$WSL_INSTANCE_PATH="${WSL_ROOT}\instances\${WSL_NEW_DISTRO_NAME}"

mkdir $WSL_IMAGE_PATH
mkdir $WSL_INSTANCE_PATH

# Create a backup/export of the existing Debian in     `D:\WSL\images\Debian.backup.tar`
wsl --shutdown
wsl --export $WSL_OLD_DISTRO_NAME "${WSL_IMAGE_PATH}\${WSL_OLD_DISTRO_NAME}.backup.tar"

# Import that backup into the new location/distribution:
wsl --import $WSL_NEW_DISTRO_NAME $WSL_INSTANCE_PATH "${WSL_IMAGE_PATH}\${WSL_OLD_DISTRO_NAME}.backup.tar" --version 2

# Start the new distro:
wsl ~ -d $WSL_NEW_DISTRO_NAME    

You next need to set the default username, since WSL doesn't "remember" it for --imported distributions. From inside the new Debian distro:

sudo -e /etc/wsl.conf
# Yes, you are already root, but `sudo -e` is just a fairly safe way to invoke the default editor regardless of distribution

Add the following:

[user]
default=<your_normal_username>

Save and exit the editor.

exit the shell/WSL.

Back in the same PowerShell session (so all the variables are still intact):

wsl --terminate $WSL_NEW_DISTRO_NAME
wsl --set-default $WSL_NEW_DISTRO_NAME
wsl ~

You should now be running as your normal user in the new Debian location.

To confirm, wsl.exe -l -v should show that the new distro is running, and the old one is not. Additionally, from PowerShell:

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

... will show the installed distributions and their locations.

After confirming that everything is working properly:

Warning: This is a destructive and unrecoverable operation

$WSL_OLD_DISTRO_NAME="Debian"
wsl --unregister $WSL_OLD_DISTRO_NAME

You can also uninstall the Debian app from the Store as well, but I personally recommend keeping it around.

2
  • Thanks for the good detailed answer. Only improvement I suggest is to use the optional --vhd argument to save backup images in .vhdx format (and change ".backup.tar" to ".backup.vhdx" in your Powershell). Faster and more space efficient than tar, plus added benefit is you have the option to use other VHD software to read/write contents of the backup 's filesystem if needed.
    – gb96
    Commented Feb 11, 2023 at 0:17
  • @gb96 Thanks! And agreed, it could use an update for the --vhd argument, which had only been available for a few months when the answer was originally written, and was only for WSL Preview for Windows 11 at the time. I think I transitioned over to it myself shortly after writing the answer (finally having given up on getting SSH to work properly with the newer releases ;-). Thanks for pointing that out - I'll try to get an update done for it. Commented Feb 11, 2023 at 1:13

You must log in to answer this question.

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