Migrate Linux Subsystem from one PC to another

UPDATE: Now you can simply use the --export and --import capability of WSL2

To backup, simply open Powershell/CMD and do:

wsl --export (distribution) (file path)

(e.g. wsl --export Ubuntu "C:\Users\user1\Documents\ubuntu_wsl_backup.tar")

To restore, you need to copy the tar on the new machine (e.g. in C:\Users\user1\Downloads\), open Powershell/CMD and run:

wsl --import (distribution) (install location) (file path)
(e.g. wsl --import Ubuntu "C:\WSL_VMs\Ubuntu" "C:\Users\user1\Downloads\ubuntu_wsl_backup.tar"

NOTE: install location is a new path where you will store your WSL VMs. I’m not entirely sure where the default ones got stored (I could google it but I can’t be bothered, sorry :-p) and I honestly prefer having all in my own folder (e.g. C:\WSL_VMs).

NOTE n.2: To find the name of the distributions (aka how WSL labelled them), use the command wsl -l

Here the original article – before import/export was implemented/documented…

Are you enjoying your favorite Linux distro running within the Windows 10 Linux Subsystem?

Have you configured all nicely?

What happened if you get a new pc and you’d like to migrate your VM across?

This is what happened to me. And looking around, I found this post that gave me this kinda-dirty way, but did work!

After that, I decided to review the steps, and I’ve added these directories in the exclude’s list, to make clearer the process of export/import:

/dev
/proc
/sys
/run
/tmp
/media
/mnt
/var/cache
/var/run

Of course, if you have important data in these folders and you want to move across too, just update the one-liner below accordingly. 😉

On your OLD PC

  • Open your Linux VM
  • Get inside your Downloads directory (replace <user> with your username):

    cd /mnt/c/Users/<user>/Downloads
  • Make sure to be root (sudo su -)
  • Run:

    tar -cvpzf backup.tar.gz --exclude=/backup.tar.gz --exclude=/dev --exclude=/proc --exclude=/sys --exclude=/run --exclude=/tmp --exclude=/media --exclude=/mnt --exclude=/var/cache --exclude=/var/run --one-file-system /

    NOTE: you could achieve the same using the option --exclude-from=file.txt, and having the list of exclusions in this file. I used a one-liner as it’s quicker to copy and paste.
  • Once done, close your Linux VM
  • Verify that you have a new file called backup.tar.bz in Downloads

On your NEW PC

  • Install from Microsoft Store the same Linux VM (or reinstall in the same way you have done originally on your old pc)
  • Copy across your backup.tar.bz within your new Downloads folder
  • Open the VM that you’ve just installed (minimal setup – this will be completely overwritten, so don’t be bothered too much)
  • Once you’re inside and your backup.tar.bz is in Download, run the following (replace <user> with your username):

    sudo tar -xpzf /mnt/c/Users/<user>/Downloads/backup.tar.gz -C / --numeric-owner
  • Ignore the errors
  • Close and re-open the VM: DONE! 🙂

Happy migration! 😉