DEV Community

Mohan Murali
Mohan Murali

Posted on • Updated on

Change the directory of WSL

Windows sub-system for Linux (WSL) is an extremely useful tool for developers to improve their development experience in windows operating system. It allows the users to run Linux commands and apps directly in windows without having to use a VM or dual booting your computer. But one issue I faced was that by default, It is installed in c drive of windows. You can use WSL in other drives as well, but to make full use of the speed and functionality offered by WSL, it is recommended to use it in its home drive, which by default is set to C with no option to change it during the time of installation. Now if you are someone like me who has a small C drive with windows installed on it (stored in SSD to speed up the OS boot-time, or some other reason), you might find yourself going out of space in C drive pretty quickly. To overcome this we can change the directory of WSL to some other drive.
To do that we need few things. First you need the WSL command line tool. Please note that WSL command line tools windows version 1903 or later. Chances are, if you have installed WSL, you must have already be having it. If you don't have WSL, then you need to open a command prompt or PowerShell window with admin permission and just use the following command

wsl.exe --install 
Enter fullscreen mode Exit fullscreen mode

This will take care of installing WSL for you along with the WSL command line tool.

Once done you can verify the installation by using the below command

wsl.exe --list
Enter fullscreen mode Exit fullscreen mode

This should show you the list of distributions currently installed in your computer.

Now that we have WSL installed, we will look into how we can change the default directory. First you need to create a .tar file of your existing Linux distro. To do that you must use the below command

wsl.exe --export <Distro Name> <tar file name>
Enter fullscreen mode Exit fullscreen mode

Here Distro Name will be the name of the Linux distribution whose directory you wish to move. will be the path along with file name where your .tar file be generated. If you don't give the path in it will be created in the current directory. For example -

wsl.exe --export Ubuntu20.04 d:\ubuntu.tar
Enter fullscreen mode Exit fullscreen mode

Once this is done verify that the .tar file is generated in the path. Once that is done, you need to take a backup of all the data from your current home path of the distro, cause after this you will loose access to your distro and might loose the files. Once that is done, use the below command to unregister your distro and bring back all the precious space that your distro had hogged up.

wsl.exe --unregister <Distro Name>
Enter fullscreen mode Exit fullscreen mode

Again the Distro Name will be the name of the distro you wish to unregister.
Now the final step is to import the exported .tar file into your desired directory. To do that use the following command

wsl.exe --import <New Distro Name> <Distro Path> <tar file name>
Enter fullscreen mode Exit fullscreen mode

The is the name you wish to identify your distro as in your computer. will be the path you wish to store this distro in. Typically this will be some path in the directory you wish to move your WSL. will be the full path and name of your exported .tar file. An example of the above command will look like

wsl.exe --import Ubuntu d:\wsl\Ubuntu d:\ubuntu.tar
Enter fullscreen mode Exit fullscreen mode

Once the command is executed, your distro will be created in the new path. By default, we will be logged in as the root user. Its not the ideal scenario as the root user has access to change things without needing the password. So it is better to change to a new sudo(sudo user is like admin user in linux) user. You will still have the user and password you created at the time of installing WSL available. You can switch to that user by using the below command

su <username>
Enter fullscreen mode Exit fullscreen mode

where username is the user name of the user you created while installing WSL. You can verify the user details by using the below command in terminal

whoami
Enter fullscreen mode Exit fullscreen mode

If this shows the user name instead of root. Then you have successfully switched to the account. Now you will have to give password for making changes with sudo command.
Its not ideal to have to switch to the new user every time you open your terminal. It would be great if we could set it as the default user just like how it was before we moved the disk. To do that we will need to open a command prompt or windows powershell and add the following command based on your distro

<distro-name> config --default-user <username> 
Enter fullscreen mode Exit fullscreen mode

where username is the name of the user you wish to configure and distro-name will be the name of your distro.

  • for Ubuntu it will be "ubuntu"
  • for openSUSE Leap 42 it will be "opensuse-42"
  • for SUSE Linux Enterprise Server 12 it will be "sles-12"

Now each time you open up your terminal, you will be set as this user by default. You can verify that by running whoami command in your terminal.

Phew, quite a bit of work. You can finally use your WSL without worrying about space.

Hope this article was helpful. Thanks for reading.

Oldest comments (4)

Collapse
 
alimp5 profile image
alimp5

Thanks a lot.
tnx you man :X

Collapse
 
krazykoder profile image
Krazy Koder

Thanks a lot. It worked as you described.

Collapse
 
ashwinmk profile image
ashwin974

if it doesn't work, once the tar file is generated and the unsubscription is done, delete the Ubuntu App before importing the tar file.

Collapse
 
somebrujah profile image
Maxym Kyryliuk

Thanks a lot, you are breathtaking❤