DEV Community

Cover image for Move WSL File System to another Drive
Camilo Martinez
Camilo Martinez

Posted on • Updated on

Move WSL File System to another Drive

Language: [🇪🇸] Español - [🇺🇸] English


If the C: drive is running out of space or do you like to have your files in 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 whoami command (WSL side):

who am I

Also we need to know the distro name and WSL version, running the wsl --list --verbose command on PowerShell (Windows side):

wsl list

Knowing the username: equiman, the linux distribution: Ubuntu, and confirm that we are using the WSL 2 version, we are ready to...

moving day

Move

All next steep 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 wsl --shutdown command.

Next we are going to create a backup from our Ubuntu linux distribution.

mkdir D:\backup
wsl --export Ubuntu D:\backup\ubuntu.tar
Enter fullscreen mode Exit fullscreen mode

Don't worry if you not see progress indicators. This can be take a while depending how much data you have. Maybe it's good idea take a rest.

waiting

When finish, we need to to unregister the linux distribution from 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!

wsl --unregister Ubuntu
Enter fullscreen mode Exit fullscreen mode

And then, import again on our new disk. I'll use D: disk as an example.

mkdir D:\WSL
wsl --import Ubuntu D:\WSL\ D:\backup\ubuntu.tar
Enter fullscreen mode Exit fullscreen mode

By default Ubuntu will use root as default user, to switch to our user we need to run this commands.

cd $env:USERPROFILE\AppData\Local\Microsoft\WindowsApps
ubuntu config --default-user equiman
Enter fullscreen mode Exit fullscreen mode

Ensure replacing equiman with yours.

Restart WSL running wsl command and it's done.


That’s All Folks!
Happy Coding 🖖

Buy me a coffee

Discussion (0)