129

I've just upgraded the Windows 10 in my laptop to Redstone 1. So I had a test of the Linux subsystem (aka. WSL, LXSS or Bash on Windows). Basically, everything is fine, but there is a problem that the RootFS of WSL is located in the %LocalAppData% path (e.g. C:\Users\xyz\AppData\Local), which is also the place where my Windows OS located. It consumes a lot of the SSD hard drive space in my laptop and I hope I can move it to my external hard drive.

There is an another problem. Since the other hard drive partitions are mounted as DriveFS, it does not support some Linux filesystem features like chmod, and some of my Linux project build scripts only works in VoIFS, which only located in / and /home.

So I tried to move the whole WSL RootFS to another hard drive. Firstly I've tried to make a NTFS Junction (hard link) for C:\Users\xyz\AppData\Local\lxss. I've uninstalled the WSL completely and run mklink for the RootFS to another hard drive, and when I reinstall the WSL again, it couldn't create the new user, and it reports error with code 0x80070003.

Then I searched around, and I got this: moving Linux filesystem - issue #449. It seems that moving and making hard link for WSL RootFS only works before Windows 10 Build 14388, it does not work in the Windows 10 Build 14393 RTM. The only way is moving the whole %LocalAppData% to the external hard drive, but I cannot do that since I'm a laptop user and I can't bring an external hard drive everywhere!

So I would like to ask that, is there any other ways to move WSL out of my system partition? If I can't do that, can I create an extra VoIFS work directory in some other places and compile my stuff?

Thanks and regards, Jackson

Tim
  • 4,790
  • 4
  • 33
  • 41
Jackson Ming Hu
  • 1,681
  • 2
  • 14
  • 23

5 Answers5

245

If you are using Windows 10 version 1903 (April 2019 Update) or later, you can try the WSL command line tool with the export and import options. If you are using an older Windows 10 version, you can move the distribution to another drive using lxRunOffline.


Using the WSL command line tool

In Windows 10 version 1903 (April 2019 Update) or later, you can use the wsl.exe command line tool.

1. Export the distribution. Create a .tar file with the distribution to move using wsl.exe --export

wsl.exe --export <DistributionName> <Tar-FileName>

For instance, to export an Ubuntu distribution, you can use

C:\> wsl.exe --export Ubuntu c:\data\ubuntu.tar

2. Import the distribution into the target folder. Then, you can import the exported distribution into another folder

wsl.exe --import <DistributionName> <Folder-To-Install> <Tar-FileName>

For instance, to import the exported Ubuntu into a new UbuntuCustom distribution, you can use

C:\> wsl.exe --import UbuntuCustom d:\wsl\UbuntuCustom c:\data\ubuntu.tar

NOTE: You may check an script for moving WSL distros that use these commands at https://github.com/pxlrbt/move-wsl

3. Delete old installation (optional)

wsl.exe --unregister <OriginalDistributionName>

4. Set up the default user

Edit /etc/wsl.conf and add:

[user]
default = <your username>

5. Run new distribution

wsl.exe -d <NewDistributionName>

Using LxRunOffline

In any Windows 10 version, you can move the distribution to another drive using lxRunOffline.

1. Set permissions to the target folder. First, I think you must set some permissions to the folder where the distribution will be moved. You may use icacls <dir> /grant "<user>:(OI)(CI)(F)" to set the proper permissions.

C:\> whoami
test\jaime

C:\> icacls D:\wsl /grant "jaime:(OI)(CI)(F)"

NOTE: In addition to the above permissions, I have activated the long path names in Windows.

2. Move the distribution. Using lxrunoffline move.

C:\wsl> lxrunoffline move -n Ubuntu-18.04 -d d:\wsl\installed\Ubuntu-18.04

You may check the installation folder using

C:\wsl> lxrunoffline get-dir -n Ubuntu-18.04
d:\wsl\installed\Ubuntu-18.04

3. Run the distribution. after moving the distribution, you can run the distribution using wsl or the same lxrunoffline

C:\wsl> lxrunoffline run -n Ubuntu-18.04 -w
user@test:~$ exit
logout

C:\wsl> wsl
user@test:/mnt/c/wsl$ exit
logout
TankorSmash
  • 12,186
  • 6
  • 68
  • 106
Jaime
  • 5,435
  • 2
  • 18
  • 21
  • 5
    This is the best option so far I think – Jackson Ming Hu Nov 25 '18 at 06:41
  • 13
    I was able to sucessfully run the above commands, but the linux distribution is still present in the %APPDATA% folder, is it safe to remove? – budji Jun 25 '19 at 10:00
  • 4
    In my case command was lxrunoffline move -n Ubuntu -d E:\prog\WSL\Ubuntu – 57ar7up Sep 02 '19 at 19:24
  • @Jaime Can't you just use `wsl --export ` and then `wsl --import ` respectively? – Joe Jan 28 '20 at 01:52
  • 1
    @Joe, you are right. However, you can only use `wsl --export` in recent versions. Only in Windows 10 version 1903 (April 2019 Update), WSL includes options for import and export. If you are using a previous version or some Windows 10 LTSB, you must use `lxrunoffline`. In the most current versions you can use the one or the other. – Jaime Mar 03 '20 at 12:26
  • 12
    The wsl command solution works fine. I wrote a little script to automate these steps: https://github.com/pxlrbt/move-wsl – pixelarbeit Mar 20 '20 at 15:15
  • One concern: what if Ubuntu got updated from the app store, will the migrated one get updated? – dotslashlu May 27 '20 at 08:31
  • You can update the Ubuntu operating system in the WSL instance using the traditional `do-release-upgrade` and `update-manager` command line tools. For instance, you can upgrade your Ubuntu 18 to Ubuntu 20 running `sudo do-release-upgrade`. Traditionally, Microsoft has released [different installers for each Ubuntu version](https://docs.microsoft.com/en-us/windows/wsl/install-manual). For instance, there is a different [installer for Ubuntu 20](https://www.microsoft.com/es-co/p/ubuntu-2004-lts/9n6svws3rx71). I think If you install a new version it will be installed in a different folder. – Jaime May 29 '20 at 14:23
  • 18
    What is missing here in word "move" is to delete the original WSL distribution. The deletion can be done by cmd: wsl --unregister Ubuntu – Xiaowei Song Oct 05 '20 at 17:29
  • 1
    I followed the steps above, and there were two things that I needed to do in order to complete the process: 1) unregister the current distribution before importing the tar file (wsl --unregister Ubuntu), and 2) Set the default user after importing the distribution (https://askubuntu.com/questions/816732/how-to-change-default-user-in-wsl-ubuntu-bash-on-windows-10) – jorgenv Oct 12 '20 at 13:05
  • how can i import to the same distribution? – Thanasis Ioannidis Oct 24 '20 at 12:20
  • @ThanasisIoannidis Do may import the distro with another name. If you want to replace an existing distribution, I think you can remove (uninstall) the distribution and import it again from a backup. – Jaime Feb 19 '21 at 17:03
  • I used `move-wsl` script to move WSL and it worked fine. However, I had to reconfigure the defaut user (with `ubuntu2004.exe config --default-user dev`). Visual studio C++/CMake compilation stopped working untill I add this to /etc/wsl.conf: `[automount] enabled = true options = "metadata"` and restart the VM – Kiruahxh May 31 '21 at 12:29
  • I was able to migrate the WSL 2 files to another location successfully. But what about Docker integration? The distro is check in Docker WSL integration but docker command is not working inside the linux distro. I expose daemon on tcp://localhost:2375 without TLS and got this error: ** -bash: /windir/c/Program Files/Docker/Docker/resources/bin/docker: Permission denied ** – nelsonym Jun 08 '21 at 14:14
  • 3
    After the migration, the default user for the WSL2 VM was always root. To fix it, I had to edit my `/etc/wsl.conf` and add the following: `[user] default = `. Once that's done, terminate and restart the VM for the changes to apply. – Nicola Jun 13 '21 at 15:47
  • Before "wsl --import", you may need to issue "wsl --unregister " and then issue the --import command. Otherwise, it would "A distribution with the supplied name already exists." . – Farrukh Waheed Sep 06 '21 at 23:01
  • This was great once I did the extra steps in the comments above i.e. unregister the initial distro and set the default userid in the registry. I also had to go back in and set the distro as enabled in docker desktop before that would work as that got turned off when I unregistered the previous distro. – Ben Thomson Jan 09 '23 at 11:25
9

WSL does not (currently) support moving or installing distro to non-system drives. However, this is a scenario we are exploring for future Windows releases.

Note that as of Fall Creators Update (FCU), distros are now installed via Windows store and are downloaded to, and expanded to different folders (i.e. not under %localappdata%\lxss).

NOte: While we're working to improve this scenario in future Windows releases, please DO NOT spelunk into the distro folders from Windows or Windows apps remains: https://blogs.msdn.microsoft.com/commandline/2016/11/17/do-not-change-linux-files-using-windows-apps-and-tools/

Rich Turner
  • 10,800
  • 1
  • 51
  • 68
  • 1
    [WSL GH Issue #449](https://github.com/Microsoft/WSL/issues/449) tracks the ask - there are some suggestions in there that may work for you, but the feature to move distros to a non-system drive has not yet been fully-built. – Rich Turner Aug 10 '18 at 19:50
9

Take a look to https://github.com/DDoSolitary/LxRunOffline

scoop install lxrunoffline
choco install lxrunoffline
  • No Internet access required when installing.
  • Install any Linux distros to any location on your disk.
  • Moving existing installations to other locations.
  • Duplicating existing installations. (To create backups.)
  • Register existing installation directories. (For portable usage.)
gavenkoa
  • 45,285
  • 19
  • 251
  • 303
5

I have WSL2 Ubuntu20.04 in Windows Home

Comment from @pixelarbeit

The wsl command solution works fine. I wrote a little script to automate these steps: github.com/pxlrbt/move-wsl

The steps I did:

  1. install gitbash for windows to run the command from github
  2. open the distro, add file

/etc/wsl.conf

[user]
default=<yourAccount>
  1. in CMD "wsl.exe --shutdown"
  2. next time I open the distro, my user is the default one and all looks like it was before, great!

THANK YOU @pixelarbeit!

Update: I use "windows terminal" program from windows store, and somehow setting /etc/wsl.conf didnt always work. But I found, try go to settings and set as starting directory
\\wsl$\Ubuntu-20.04\home\<your-username>. For alpine:
\\wsl$\Alpine\home\<your-username>

Daniel Katz
  • 2,271
  • 3
  • 25
  • 27
  • After wsl move to another drive, Portfowarding does not work. In my case, wsl service restart in powershell. everything works normal as it was. 'Restart-Service LxssManager' – agfe2 Sep 13 '22 at 02:02
2

Can simply move installed distro to other location

stop wsl first: wsl --shutdown

Path to distro stored in registry Computer\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Lxss\{some guid}\BasePath enter image description here move distro to another location and update path in registry. (need move entire CanonicalGroupLimited.Ubuntu22.04LTS_79rhkp1fndgsc directory, not just the LocalState folder)

Nicolai Nikolai
  • 400
  • 3
  • 10