Create Multiple WSL Distros on a single machine
Create Multiple WSL Distros on a single machine

Create Multiple WSL Distros on a single machine

Tags
WSL
Ubuntu
Multiple Distros
windows
Tech
Published
July 19, 2024
Author
Quick View

Step 1: Export the Ubuntu that you have already installed.

If you are already using wsl used it, and now you feel like you need multiple wsl environments, and want a clean & pristine version?
first backup your current wsl to prevent any data loss
  1. First thing get the list of distros, and take note of your Ubuntu distro name
    1. wsl --list
  1. back up your wsl using this command
    1. wsl --export Ubuntu "E:\wsl backup\wsl_20240719_bkp.tar"

Step 2: Create a clean WSL distro from the existing one that you are already using.

now to get the pristine version of your wsl without whatever you installed, you need to get the directory using powershell admin
the directory for ubuntu should look like this:
C:\Program Files\WindowsApps\CanonicalGroupLimited.Ubuntu_2204.3.49.0_x64__79rhkp1fndgsc
in the following command replace YOUR_UBUNTU_DISTRO_NAME with a name of your choice for example: ubuntu_dev
$newDistroDir = "E:\WSL\" mkdir $newDistroDir $rootFSDirectory="C:\Program Files\WindowsApps\CanonicalGroupLimited.Ubuntu_2204.3.49.0_x64__79rhkp1fndgsc" wsl --import YOUR_UBUNTU_DISTRO_NAME $newDistroDir ($rootFSDirectory+"\install.tar.gz")

Step 3: Create a new user and make it as default

Launch your newly created wsl distro with the following command:
wsl ~ -d YOUR_UBUNTU_DISTRO_NAME apt update apt upgrade
After you launch, create a new user, of course, by replacing username with a username of your choice, and no need for sudo because you are already running as root
useradd --create-home --user-group --groups adm,dialout,cdrom,floppy,sudo,audio,dip,video,plugdev,netdev --password "encryptedPassword" username
if u put a space for password as I did, you have to enter launch ubuntu as root and change the password for that specific user
wsl ~ -d YOUR_UBUNTU_DISTRO_NAME -u root
and change password
passwd username
Now windows has already modified the windows registry with a default username, however now we need to tell windows to use the default user we just created.
With the same root user,
create a file /etc/wsl.conf with the following contents:
[user] default=username
After setting the default user, you might need to shutdown wsl and restart it, but first you need to exit from root and go back to powershell and run the following command
wsl ~ -t YOUR_UBUNTU_DISTRO_NAME

Recommended next steps

I personally recommend backing up this "ready-to-run" copy of your distribution at this stage. Again, from PowerShell:
wsl --export Ubuntu "ubuntu_base_20240719.tar"
you can then import this new base as much as you need by following the above steps

Extra Steps (Optional)

after you setup a new user, use the distro you just installed and install zsh if you want it 🙂
sudo apt-get install zsh sudo apt-get install git sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
add to the end of .bashrc
if test -t 1; then exec zsh fi
sources: