In my last post, I described how to install and manage Windows Subsystem for Linux (WSL) in Windows 11. In today's post, you'll learn how to export and import a Linux distribution in WSL.

Do you remember running the wsl --list --online command? The list you saw there was very short, and if you could not find your favourite Linux distro in that list, this guide is for you. When you use this command, it shows the list of Linux distributions that are available in the Microsoft online store, but if the distribution of your choice isn’t available there, WSL allows you to export and import any Linux distribution.

Why use export and import

The export/import function of WSL come in handy when your favourite Linux distro is not available in the Microsoft store, but you still want to use it with WSL.

The export/import function is also useful for backup purposes. Suppose you have fully set up your favourite Linux distro and something goes wrong—you’ll have to do everything from scratch. To prevent this situation, you could use the export function to keep a backup of your Linux distro: you can keep as many backups as you like (as long as you have enough storage space). These backups can also be restored on a new machine running the same version of WSL. Isn’t that great?

In this section, I will explain how you can get a .tar file for a Linux distro and import the distro from the .tar file into WSL. For the sake of this guide, I am assuming that my favourite Linux distro is Fedora, which is not available in the Microsoft online store, but I still want to be able to run it in WSL.

Prerequisites

Before you follow this guide, there are certain prerequisites your system should meet:

  • WSL2 must already be installed in your system;
  • Docker Desktop for Windows should be installed (it can be downloaded from the following official link: https://docs.docker.com/desktop/windows/install/ )
  • the Docker Desktop for Windows should be using “WSL2-based engine” (this can be set in the General settings of the Docker Desktop).

    Docker Desktop Use WSL2 based engine

    Docker Desktop Use WSL2 based engine

  • The Docker Desktop for Windows should have “WSL Integration” enabled (it can be turned on in the WSL Integration settings of the Docker Desktop).

    Docker Desktop Enable WSL integration

    Docker Desktop Enable WSL integration

Exporting the tar ball from a Docker container

First of all, we need to run our favourite Linux distro (Fedora in this case) in a docker container inside the WSL Linux distro and then create a .tar file from that container.

Windows Subsystem for Linux List of distros

Windows Subsystem for Linux List of distros

You can use any distro, but since we have Kali Linux distro already installed in our system, we will continue using it. All we will need to do is use a docker inside this Linux distro.

Connect to the Kali Linux distro using wsl -d kali-linux command and run the following docker command:

docker run -t fedora ls -a
Docker pull and run Fedora

Docker pull and run Fedora

When you run this command for the first time, it will pull the latest Fedora image from the Docker hub and run a container. It may take a while for the command to do its job.

Once that’s done, you can see the running container and use the following command:

docker container ls -a
Docker view container and container ID

Docker view container and container ID

After running this command, you should see the container ID for the Fedora Docker container you have just started.

Now we will grab that container ID in a variable and use it to export the Fedora container into a .tar file:

ContainerID=$(docker container ls -a | grep -i fedora | awk '{print $1}')

Once you have the Docker container ID stored in $ContainerID, you can verify it using the echo command, as shown in the image below:

Grabbing the Docker container ID

Grabbing the Docker container ID

Remember that when using WSL your Windows drives are mounted in the /mnt directory—you can verify that using the following command:

df -Th
Check the mount points and create a directory

Check the mount points and create a directory

My D drive was mounted under /mnt/d, so I created a new /mnt/d/wslroot directory to store the Fedora .tar file.

The final step is to export the Docker container using the $ContainerID variable and export the file to the /mnt/d/wslroot directory we have created earlier. To do this, run the following command:

docker export $ContainerID > /mnt/d/wslroot/fedora.tar
Docker Export .tar file

Docker Export .tar file

Once this is done, your .tar file is ready, and we will then import this file into WSL in the next section.

Importing a .tar file into WSL

Now that you have the .tar file ready for your favourite Linux distro, we need to import it inside WSL. The syntax of the import command is as follows:

wsl --import <DistroName> <InstallLocation> <FileName>

Where:

  • the <DistroName> is the name you would like to give your distro;
  • the <InstallLocation> is the directory location where the imported distro will be stored;
  • the <FileName> is the complete path and name of the .tar file we have created earlier.

To import our Fedora distro, we will run an import command as shown below:

wsl --import Fedora D:\wslroot D:\wslroot\fedora.tar
WSL Import .tar file

WSL Import .tar file

Having run this command, we can see our Fedora Linux distro has been installed successfully. You will see a file named ext4.vhdx stored in the D:\wslroot directory that we specified during import.

View the imported WSL distro

View the imported WSL distro

To run the new distro, just type a wsl -d fedora command, and you will be logged in using the default root account. Now you could customize your Fedora distro the way you like.

Exporting the .tar archive from a WSL distro

Once you have your Linux distro customized and set up the way you want, you can export this distro into a .tar file again to create a backup. This will help you transfer your WSL distro to a new machine or restore your Linux distro in case something goes wrong or you mess something up.

To export the WSL distro into a .tar file, run the following command:

wsl --export Fedora D:\wslbackup\fedora_backup_20220303.tar
Export the WSL distro for backup

Export the WSL distro for backup

Once the command is complete, you will see a backup .tar file created inside the D:\wslbackup\fedora_backup_20220303.tar directory that we specified during export.

To restore the distro from the backup, you will need to use the import method we discussed in the previous section.

Subscribe to 4sysops newsletter!

That’s all for this guide. We have now learned how to get our own Linux distro up and running in WSL when it is not available in the Microsoft store and how to back up and restore the WSL distros from .tar files.

0 Comments

Leave a reply

Your email address will not be published. Required fields are marked *

*

© 4sysops 2006 - 2023

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account