- Install K3s, a lightweight, production-grade Kubernetes distro - Mon, Mar 20 2023
- VMware NSX Advanced Load Balancer: Installation and configuration - Fri, Mar 10 2023
- NetCrunch 13: Real-time network monitoring, traffic analysis, alerts, and performance reporting - Tue, Mar 7 2023
One of the exciting new features that has been introduced in the past couple of years for Microsoft Windows is the Windows Subsystem for Linux (WSL). Previously, developers and DevOps professionals had to spin up a Linux VM or dual-boot a system to access a Linux environment quickly. With WSL, developers and others can bring up a WSL environment and interact with Linux in what looks like a simple command window.
However, once developers begin to heavily utilize their WSL environment, how can they save a copy of the WSL environment? Can you copy the WSL distro image used on one machine to another? This post will consider exporting and importing WSL distros.
What is Windows Subsystem for Linux?
Specifically for developers, WSL allows full access to the GNU/Linux command-line, various utilities, and Linux applications without the need to run a desktop virtualization hypervisor, dual-boot a system, or spin up a network-accessible virtual machine on an enterprise hypervisor.
Microsoft has made it even easier to run WSL with Linux distros for it in the Microsoft Store. Simply searching for "Linux" in the Microsoft Store will return the latest distros available for download in WSL.
WSL2 extends the features and capabilities of WSL. With WSL2, Microsoft has increased file system performance and included the full Linux kernel for increased system call compatibility. WSL2 also runs inside a utility virtual machine that is transparent to the end user. For the most part, the end user experience with WSL2 is the exact same as WSL.
Export and import WSL distros
As developers and DevOps professionals rely on WSL for daily development and as part of their toolset for access to Linux command-line tools, utilities, and applications, many organizations may now be thinking about providing disaster recovery for highly customized WSL environments. Starting with Windows 10 version 1903, Microsoft included new export/import functionality that effectively exports a copy of the WSL environment and allows importing it again, either on the same or on a different machine.
Now, developers can save a copy of their WSL environment as a backup. Developers can also use WSL to move or copy the environment to a different development machine. It provides an almost container-like experience, in that everything needed to import the same environment onto another Windows 10 workstation is included with the export process of the WSL image. Let's take a look at the process of exporting and importing a WSL Linux image.
The requirements are quite simple.
- Windows 10 1903 or higher
- A location to store the .tar file—This can either be local or a remote location (network drive)
Exporting a WSL Linux image
Before exporting the WSL Linux image that you want to import onto the same system or another, you need to list the images and discover the image names. The image name is used during the export process. To list the WSL images, use the following command:
As you can see below, there is one WSL image that is available for export—Ubuntu20.04. This is the current default WSL image available on the system.
Next, comes the actual export of the WSL Linux image. The command to perform the actual export of the WSL image is the following:
wsl --export <WSL Image Name> <Export file>
You do want to keep in mind that to export the WSL image from your source Windows 10 workstation, you will need to make sure you do not have a WSL command window open.
The Ubuntu 20.04 WSL image .tar file after export was around 1.7 GB. As mentioned above in the requirements section, you can also export your WSL images to a network location as well. This can either be a mapped network drive or a UNC path.
Importing an exported WSL Linux image back into WSL
Once you have the exported WSL image .tar file, you can copy the file to another Windows 10 workstation if you want and import the WSL image there. In addition, you can simply import the WSL image back into WSL using a different image name. In this way, you can effectively "clone" a WSL Linux image and run these on the same WSL Windows 10 workstation.
The command to import a WSL Linux distro image into WSL is as follows:
wsl –import <Image Name you choose> <Directory to store and run the image> <Directory location of the exported .tar file>
As you can see below, the import of the WSL Linux distro image from an exported .tar backup file was successful. When listing the available WSL images, the UbuntuCustom WSL Linux image name used during the import is available as a WSL image.
To run the specific WSL Linux distro imported from the exported .tar backup file:
wsl -d <WSL imported image name>
This allows running a specific WSL Linux distribution without making the distribution the default.
Automating the export of WSL Linux distro images
The export/import functionality in WSL opens up the possibility of exporting WSL images contained on your WSL host. Using PowerShell or the command shell, developers or IT ops can create scripts that automate the workflow:
Subscribe to 4sysops newsletter!
- Stop the running WSL instances.
- Perform an export.
- Start the WSL instance once again.
WSL is a great platform that allows developers, DevOps, and others to have the command-line interface, utilities, and Linux apps needed without having to provision a dedicated Linux virtual machine for that purpose. The WSL platform is presented to the end user as a simple command window that provides access to the Linux environment. As developers and others become increasingly reliant on the functionality that WSL provides, having the ability to export and backup WSL Linux distros is important. This can be done easily and can even be automated using the built-in export/import feature.
Want to write for 4sysops? We are looking for new authors.
Thanks for this guide – really useful.
I also needed to update the registry using the PS script found here: https://blog.codonomics.com/2020/09/setting-default-user-for-imported-wsl.html because when you import a distro, the default user doesn’t get set.