8

New to Linux and Ubuntu. I am trying to transfer my old Ubuntu WSL files to my new Windows 11 laptop.

I have gone through similar questions, but none seems to help my case.

I am trying to clone the WSL of my old laptop on a USB hard drive so I can transfer it to my new laptop, but the USB is not listed when I enter lsblk or any other command that lists the drives on laptop.

Results of wsl -l -v from the comments:

  NAME                    STATE      VERSION
  docker-desktop-data     Running    2
* Ubuntu                  Running    2
  docker-desktop          Running    2

I need to transfer files from both Ubuntu and the docker-desktop-data.

NotTheDr01ds
  • 22,381
Leo
  • 181
  • Okay, the Docker part might be slightly more difficult - At least it's not something I've tried myself. I'll get you an answer, but might be a bit. – NotTheDr01ds Sep 14 '22 at 23:16
  • @NotTheDr01ds when i ran the command set default Ubuntu_WSL the following error log :There is no ditribution with the supplied name Wsl/Service/WSL_E_DISTRO_NOT_FOUND. Also the import just take forever. Can you help,please? – Leo Sep 16 '22 at 06:17

3 Answers3

16

If you just want to copy a few files out of Ubuntu, then you can certainly use the \\wsl$\Ubuntu method that @Android776 propose in the other answer. However, that will pretty much only work properly for files that your Linux user created and owns. Other files will lose their permissions and/or owner (typically root), and some system files won't be readable at all that way, (e.g. /etc/shadow).

Since you say that you want to move the Docker data over to the new laptop, you're going to need to use the --export method described below anyway, so you might as well use it for both the Ubuntu filesystem as well as that of Docker.

Backing up is the easy part -- WSL provides the ability to export a distribution, which creates a tar file that can then be imported again, either on the same computer (as a new distribution) or on another system.

I'm going to assume that your USB drive is D:, but substitute whatever drive letter you need below.

From PowerShell:

wsl --export Ubuntu D:\ubuntu.tar
wsl --export docker-desktop-data D:\docker-desktop-data.tar

There's no need to export docker-desktop, since it will be automatically re-created properly when you install Docker Desktop on the new laptop.

Eject the USB drive (properly, using the Eject command from the taskbar) and move it to the other computer.

On Windows 11 on the new computer:

  • Start an Admin PowerShell.

  • wsl --install (if you haven't already)

  • Reboot when told.

  • Complete the installation, which will install a new Ubuntu -- You won't use this one.

  • Remove this newly installed Ubuntu. From a non-Admin PowerShell:

    wsl --unregister Ubuntu
    

    Important: Only do this on the new computer, and only immediately after installing this way. This will remove all files from the newly installed Ubuntu version.

  • Next, we restore the Ubuntu distribution that we backed-up to the USB drive. Attach the USB drive (again, assuming D:).

  • Again, from a non-Admin PowerShell:

    # Adjust paths and names below as desired
    mkdir $env:USERPROFILE\WSL\instances\Ubuntu_WSL2
    wsl --import Ubuntu_WSL2 $env:USERPROFILE\WSL\instances\Ubuntu_WSL2 D:\ubuntu.tar --version 2
    wsl --set-default Ubuntu_WSL2
    wsl ~ 
    

    Note that you need to name the new Ubuntu something besides "Ubuntu", or "Ubuntu-20.04", etc. These are the names used by the Store installer, and this installation will be "separate" from that. It's better to not "confuse" the Store installers if you do ever run them again (on purpose or accidentally).

  • From within this Ubuntu, all your files should be present, but you'll be root instead of your normal user. You need to set the "default" user by:

    sudo -e /etc/wsl.conf
    
  • And add:

    [user]
    default=your_username
    

    your_username should be the username you were using on Ubuntu on the old laptop.

  • Now exit

  • And restart WSL, and you should be back to normal

Let me know how that works out for you, and I'll work on adding directions for restoring docker-desktop-data as well.

NotTheDr01ds
  • 22,381
  • woow, thanks for all this. I have ran into some issues when it comes to this part # Adjust paths and names below as desired $env:USERPROFILE\WSL\instances\Ubuntu_WSL2 wsl --import Ubuntu_WSL2 $env:USERPROFILE\WSL\instances\Ubuntu_WSL2 D:\ubuntu.tar --version 2 wsl --set-default Ubuntu_WSL2 wsl ~ i am getting wsl/error path not found – Leo Sep 15 '22 at 07:55
  • i think the issue comes from the fact there is no WSL\instances\Ubuntu in my user profile directory – Leo Sep 15 '22 at 09:15
  • @Leo Sorry, that's my typo! The first line is supposed to be a mkdir - I only copied/pasted part of the line, unfortunately. That will create the directory so that the subsequent --import command will work. – NotTheDr01ds Sep 15 '22 at 12:52
  • no worries. i have ran it it has been a little while now, i will let you know once complete :). Thanks again – Leo Sep 15 '22 at 19:40
  • @Leo Looks like your last comment went onto your question, rather the answer here. Responding down here. It looks like, if your comment is correct, that you mistyped wsl --set-default Ubuntu_WSL2 (left off the "2")? I thought that might have been my typo again .... – NotTheDr01ds Sep 16 '22 at 10:49
  • As for the slow import, how big is the tar file? It may be that the USB drive is slow? You might try copying the tar from the USB drive to the new laptop directly, then running the wsl --import with the new name? Of course, if it's already imported at this point, you won't need to do that part again anyway (until the Docker part) ... – NotTheDr01ds Sep 16 '22 at 10:51
  • i see , ok , thanks so much, i have transfer the file on the laptop as advised, it 40gb so it must be why. I am waiting patiently now :) – Leo Sep 16 '22 at 16:42
  • alright, @NotTheDr01ds i left it ran overnight so at least 16hrs ended up crashing my laptop and the export haven't been completed. can you advise please? – Leo Sep 17 '22 at 10:06
  • @Leo Ack! I haven't seen that happen in about 3 years. You said, "the export haven't been completed", but I thought from previous comments that you had actually completed the export and were on to the --import step? – NotTheDr01ds Sep 17 '22 at 16:15
  • sorry for typo i meant import not export and yes it just took forever and the end my laptop crashed with error message saying something timeout – Leo Sep 19 '22 at 11:16
  • @Leo I have an alternative method, but I'm worried that it will fail as well. You originally said you were copying to a "USB hard drive". Is it really a hard drive, or is it a Flash drive? I'm going to start writing up another answer, but it will fail as well if there's a problem with the drive. – NotTheDr01ds Sep 19 '22 at 21:34
  • thanks for being there with me still. So it a flash drive usb not a hard drive. – Leo Sep 20 '22 at 05:49
  • 1
    is there any reason not to do wsl --install --no-distribution instead of wsl --install then wsl --unregister Ubuntu? – codekoriko Sep 09 '23 at 07:51
  • 2
    @asiera Good point - Not any longer. The --no-distribution option was added in Windows 11 22H2, which came out shortly after I wrote the answer. It's even easier than that, since we also now have the wsl --import-in-place option, you can just take the ext4.vhdx files from the old system to the new one directly, without having to export first. I'll write up a new answer (since the old one still applies to older releases) with the updated/easier methods. – NotTheDr01ds Sep 09 '23 at 19:09
  • @asiera Unless you want to take a stab at it? – NotTheDr01ds Sep 09 '23 at 19:15
  • the wsl --import-in-place it was I did originally but I had worst across file-system performance than on my previous laptop. (ie coping from /home/ to /mnt/d). But the .Tar export-import method didn't improved thing – codekoriko Sep 13 '23 at 04:28
  • @asiera Hmm - Probably worth a new question (if you haven't already). Copying from /home/ to /mnt/d is actually a network copy (even locally), but I haven't seen any issues of it getting dramatically worse for any reason. That operation is already known to be pretty slow in WSL2. Is there any chance you were copying different sizes of files? WSL2 can copy a single large file to Windows faster than it can copy a large number of small files. – NotTheDr01ds Sep 13 '23 at 10:37
  • 1
    @NotTheDr01ds Have you had the chance to write a new answer? By "you can just take the ext4.vhdx files from the old system to the new one directly", do you mean one can put the ext4.vhdx to replace the vhdx file in the Store distro location directly? – astroboylrx Jan 22 '24 at 00:38
3

Here is the official answer from Microsoft:

  1. On your source machine, run the following commands in cmd prompt:

    wsl --list
    

    That will give you the value for <Distribution Name> that you'll use below. For <FileName> you can do something like ubuntu-export.vhdx

    wsl --export --vhd <Distribution Name> <FileName>
    
  2. Copy ubuntu-export.vhdx to the target machine. You can place it pretty much anywhere you'd like. Maybe put it in a new folder in %HOMEPATH% called wsl-distros.

  3. On your target machine, navigate to the directory that you copied the file to, and open cmd prompt in that directory, then run the following commands:

    wsl --import-in-place <Distribution Name> <FileName>
    

Source: https://learn.microsoft.com/en-us/windows/wsl/basic-commands#export-a-distribution

You can repeat that process for both of your distros.

  • This approach also requires the sudo -e /etc/wsl.conf and setup of default user as described in https://askubuntu.com/a/1429427/1595115. – sax Sep 27 '24 at 12:44
0

Great question - I think I might have a solution.

Here's how to transfer WSL files:

On the old PC, make sure your Ubuntu WSL is running and booted. Then, open RUN (just go to start and type run) and type in \\wsl$\Ubuntu

This is where the files for your WSL VM are stored. You can copy all the folders you see there to copy everything across though their might be some issues. I recommend you just copy the contents of \\wsl$\Ubuntu\home\your-username, but you can try copying the entire \\wsl$\Ubuntu directory.

Then, on the new PC, create a new WSL environment, and copy the files you copied earlier to somewhere in Windows, like C:\Users\yourusername\Documents. Afterwards using RUN navigate to \\wsl$\Ubuntu.

If you copied your entire WSL directory, select all the files in \\wsl$\Ubuntu and then paste your copied files from C:\Users\yourusername\Documents.

If you just copied your home directory, just navigate to \\wsl$\Ubuntu\home\username, delete all the other files and paste your existing files.

Your new and old usernames will need to be the same for this to work.

I hope this helps, Android776

  • If you want to copy the do a full transfer pacakges etc., copy the entire \$wsl\Ubuntu directory. – IntegralPilot Sep 15 '22 at 00:41
  • 2
    Copying the entire filesystem that way just won't work since (a) it runs under user permissions, and many critical files require root to read them, and (b) permissions and ownership of any files copied this way will be lost, and (c) it would be impossible to properly reconstruct docker-data-directory if done this way. – NotTheDr01ds Sep 15 '22 at 02:16