Language: [🇪🇸] Español - [🇺🇸] English
If the C:
drive is running out of space or do you like to have your files on a different disk than your Operating System, we can move our WSL2 file system to another drive in a few steps.
Prerequisites
If we are using a username different than root
, we need to know it running the command (WSL side):
# Linux Terminal
whoami
Also, we need to know the distro name and WSL version, running the command on PowerShell (Windows side):
#PowerShell
wsl --list --verbose
Knowing the username: equiman
, the linux distribution: Ubuntu
, and confirming that we are using the WSL 2
version, we are ready to...
Move
All next steps need to be done on PowerShell (Windows side).
First, we need to close all terminals that are using WSL, and then shut it down to avoid data corruption running the command.
# PowerShell
wsl --shutdown
Next, we are going to create a backup from our Ubuntu
linux distribution.
# PowerShell
mkdir D:\backup
wsl --export Ubuntu D:\backup\ubuntu.tar
Don't worry if you do not see progress indicators. This can take a while depending on how much data you have. Maybe it's a good idea to take a rest.
When finished, first of all, and more important ensure that the D:\backup\ubuntu.tar
file exists. Then we need to unregister the linux distribution from the C:
drive. It will also release the disk space taken by WSL.
⚠️ Beware that everything that was stored on the WSL drive will be lost, and the command won't ask for a confirmation, so make sure you backup everything you need first!
# PowerShell
wsl --unregister Ubuntu
And then, import it again on our new disk. I'll use the D:
disk as an example.
# PowerShell
mkdir D:\WSL
wsl --import Ubuntu D:\WSL\ D:\backup\ubuntu.tar
Use the
"
(double-quote) character to surround the paths if they have blank spaces
By default Ubuntu will use root
as the default user, to switch to our user we need to run these commands.
# PowerShell
cd $env:USERPROFILE\AppData\Local\Microsoft\WindowsApps
ubuntu config --default-user equiman
Ensure replacing
equiman
with yours.
Restart WSL by running the command:
# PowerShell
wsl --distribution Ubuntu
Recover
If something goes wrong in the process, don't worry, we can restore our WSL disk from the backup.
Didn't You skip the backup step? right?
Just repeat the steps after backup.
That’s All Folks!
Happy Coding 🖖
Top comments (8)
Thanks for this.
Note: the 'wsl' command at the end wouldn't work because the default was set to another distribution (if you have any others like docker-desktop). You can specify the distribution with:
or set the default distribution for wsl:
I've updated the article to be more specific and avoid confusion with de default distribution.
Thanks!
Thank you for this guide! Ubuntu for example will no longer appear in Apps and Features (if installed via Microsoft Store ... "Ubuntu by canonical etc") but that was no matter to me since I got to move it off my dwindling-space C: drive
Beautifully written and works like charm!
Life saver! Clear and simple walkthrough!
I got stuck in the export step. doesn't it have an option to show progress?
Sadly not, but I'll recommend you to run the
npx npkill
command and remove all the downloadednode_modules
and also delete unnecessary files before making the backup. That will help the process to be shorter.Thanks for the tip! It's a very cool tool!