Cloning WSL Distributions
Source: Dev.to
Why clone a WSL distribution?
You’ve spent hours setting up a WSL distribution—installing packages, configuring your shell, and tuning your development environment. Before you try something risky (major version upgrade, new tool, or an isolated project), you can create a clone to protect your working setup.
Export & import (tar)
The basic workflow is:
# Export the current distro to a tar file
wsl --export Ubuntu C:\Temp\ubuntu-clone.tar
The tar file contains a complete snapshot of the filesystem (packages, user accounts, configuration files, home directory, etc.).
For large distributions this may take a few minutes and the tar size will be roughly equal to the used disk space inside the distro.
# Import the tar as a new distribution
wsl --import Ubuntu-Dev "D:\WSL\Ubuntu-Dev" C:\Temp\ubuntu-clone.tar
Arguments
| Argument | Meaning |
|---|---|
Ubuntu-Dev | Name of the cloned distribution |
D:\WSL\Ubuntu-Dev | Directory where the new VHDX will be created |
C:\Temp\ubuntu-clone.tar | Tar file produced in the export step |
Restoring the default user
When importing from a tar archive, WSL logs in as root by default. Set the default user by editing /etc/wsl.conf:
# Start the clone as root
wsl -d Ubuntu-Dev -u root
# Append the user section if it does not exist
grep -q "^\[user\]" /etc/wsl.conf 2>/dev/null || cat >> /etc/wsl.conf *