How to clone a WSL 2 distro and set it up
Tags: docker, ubuntu, wsl2, linux
Published: 2023-12-09 04:23:22

If you are a fan of Ubuntu and you want to move your existing distro to a new device, you might be wondering how to do it without losing your data and settings. In this blog post, I will show you how to export and import your Ubuntu distro using Windows Subsystem for Linux, **specifically** (WSL) version 2.

Before we start, make sure you have WSL version 2 installed embedded and enabled on both your old and new devices. You can check your WSL version by opening PowerShell as an administrator and running the command:

**`wsl --list --verbose`**

If you see version 2 under the STATE column, you are good to go. If not, refer to a different article. WSL 1 does not support the import/export functionality.

### Step 1: Export Your Ubuntu Distro

On your old device, open PowerShell as an administrator and run the following command to shut down any running instances of your Ubuntu distro:

**`wsl --shutdown`**

Then, run the following command to export your Ubuntu distro to a tar file:

**`wsl --export <DistributionName> <FilePath>`**

Replace `<DistributionName>` with the name of your Ubuntu distro (you can find it by running wsl --list) and `<FilePath>` is the full path to the destination location for your tar file. For example: hello

**`wsl --export Ubuntu C:\Users\Alice\ubuntu-backup.tar`**

This will create a tar file named ubuntu-backup.tar in the C:\Users\Alice folder. You can choose any name and location for your tar file, but make sure it has enough space to store your distro.
___
### Step 2: Transfer Your Tar File

Now that you have exported your Ubuntu distro, you need to transfer it to your new device. You can use any method you prefer, such as a USB drive, cloud storage, or network sharing. Just make sure you copy the tar file to a location that is accessible from your new device.
___
### Step 3: Import Your Ubuntu Distro

On your new device, open PowerShell as an administrator and run the following command to import your Ubuntu distro from the tar file:

**`wsl --import <DistributionName> <InstallLocation> <PathToYourUbuntuExport>`**

Replace `<DistributionName>` with the name you want to give to your imported Ubuntu distro (it can be different from the original name), `<InstallLocation>` with the directory where you want to install the distro (preferably create a new folder in the current drive), and `<PathToYourUbuntuExport>` with the full path to the tar file. For example:

**`wsl --import Ubuntu-old C:\WSL\Ubuntu-old C:\Users\Alice\ubuntu-backup.tar`**

This will create a new WSL distribution named Ubuntu-old in the C:\WSL\Ubuntu-old folder.
***Note:*** You can choose any name and location for your imported distro, but ensure they are not already used by another WSL distribution.
___
### Step 4: Set Your Imported Ubuntu Distro as Default

To use your imported Ubuntu distro, you need to set it as the default WSL distribution. You can do this by running the following command in powershell:

**`wsl.exe -d <DistributionName>`**

Replace `<DistributionName>` with the name of your imported Ubuntu distro. For example:

**`wsl.exe -d Ubuntu-old`**

This will launch your imported Ubuntu distro in PowerShell. You can also use Visual Studio Code or the Ubuntu app to access your imported distro.

***Note:*** You might find yourself logged in as the root user when you launch your imported distro. Use **`su <Username>`** to login to any user that was on the imported distro. Replace `<UserName>` with the actual username.

***optional setting***
If you wish to set a user as the default user in your imported distro

The current [Microsoft recommended](https://docs.microsoft.com/en-us/windows/wsl/wsl-config#user-settings) way of setting the default username in an instance is to create a /etc/wsl.conf or append to the already existing file, the following setting:
```
[user]
default=<username>
```
`<username>` to be your selected default username.

Exit your distro/instance, then issue a **`wsl --terminate <distroname>`** from PowerShell or CMD. When you restart, the default user should be set.

This is safer and less error-prone than the registry-based methods.
 
If the optional setting doesn't work, other methods including this one were gotten from [here](https://superuser.com/questions/1566022/how-to-set-default-user-for-manually-installed-wsl-distro/1627461#1627461)
___
That's it! You have successfully transferred your Ubuntu distro to a new device using WSL version 2. Enjoy!