Camilo Martinez
Posted on November 27, 2021
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 🖖
Posted on November 27, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.