Windows Subsystem for Linux (WSL) allows you to run Linux distributions on your Windows machine. Over time, you may want to back up or migrate your WSL distributions to another location or device. In this article, we will guide you through the steps to export, unregister, and import a Linux distribution in WSL.
Step-by-Step Guide:
1. Check WSL Versions and Installed Distributions
Before making any changes, you can check the current state of your installed distributions by using the wsl -l -v command:
wsl -l -v
This command lists all installed distributions, their states (Running or Stopped), and their versions.
In the example image, two distributions were listed: Ubuntu and Debian, both using version 2 of WSL and currently stopped.
2. Exporting a WSL Distribution
To create a backup of a WSL distribution, the --export option is used. This will export the distribution into a .tar file, which can be restored later.
wsl --export Debian c:\wsl\backups\debian.tar
- Debian: The name of the WSL distribution to export.
- c:\wsl\backups\debian.tar: The location where the backup is stored as a tar file.
The output indicates the operation completed successfully, meaning the Debian distribution has been exported and backed up.
3. Unregister a WSL Distribution
If you want to remove the distribution from WSL after exporting (for example, to migrate or free up space), you can unregister it using the --unregister command:
wsl --unregister Debian
This command removes the distribution from WSL. However, the actual files on your filesystem are not affected by this step if you have previously exported the distribution.
4. Importing a WSL Distribution
To restore or migrate a distribution from a previously exported .tar file, the --import option is used. This re-imports the distribution into WSL at a specified location.
wsl --import Debian c:\wsl\installs\debian c:\wsl\backups\debian.tar
- Debian: The name to assign to the imported distribution.
- c:\wsl\installs\debian: The directory where the root filesystem of the imported distribution will be stored.
- c:\wsl\backups\debian.tar: The path of the tar file to be imported.
Once the operation completes successfully, the distribution is reinstalled and ready for use. You can verify it by checking the list of installed distributions again:
wsl -l -v
The output shows that Debian has been successfully re-imported.
5. Accessing the Filesystem
Once the import is complete, you can access the WSL distribution by specifying the distribution name in the wsl -d command:
wsl -d debian
In the image, the user navigated into the Debian filesystem and listed files like NTUSER.DAT, Pictures, and others, indicating access to the user directory after the import.
Summary of Commands:
1. List installed distributions:
wsl -l -v
2. Export a WSL distribution:
wsl --export <DistroName> <BackupPath.tar>
3. Unregister a WSL distribution:
wsl --unregister <DistroName>
4. Import a WSL distribution:
wsl --import <DistroName> <InstallPath> <BackupPath.tar>
5. Run a specific WSL distribution:
wsl -d <DistroName>
These commands are useful for managing, migrating, or backing up WSL distributions on your Windows system.