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,082
Leo
  • 181
  • 1
  • 1
  • 5

3 Answers3

15

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,082
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.

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