Create multiple WSL2 instances of Ubuntu

Sometimes you just want a clean WSL2 container to do some stuff, but by default you can not install more then one Ubuntu installations next to each other. So let’s download an Ubuntu image and create the container manually, it doesn’t take long!

Choose a directory

Open up your command prompt in Windows, and create a directory to store the Ubuntu image and the container itself, and navigate to that directory

mkdir \247web

cd \247web

Download WSL image: Ubuntu 22.04 LTS

In this case we are going to download the ‘22.04 LTS’ release, but you can download any other release, just remove the filename from the url and look in the directories what’s available.

curl https://cloud-images.ubuntu.com/wsl/jammy/current/ubuntu-jammy-wsl-amd64-wsl.rootfs.tar.gz -o ubuntu-22.04.tar.gz

-img-

Create WSL2

We can now create a new WSL2 machine by importing the downloaded image (the name we are going to use in this example is ‘247web’)

wsl --import 247web "C:\247web" "C:\247web\ubuntu-22.04.tar.gz"

List all WSL instances to see your new container.

wsl -l -v

-img-

Login the first time (root)

Success! The new WSL instance is created! Now login to the new WSL container

wsl -d 247web

By default we are logged in as ‘root’, we want to create a new username:

MY_USERNAME=247web
useradd -m -G sudo -s /bin/bash "$MY_USERNAME"
passwd "$MY_USERNAME"

# (Enter your new password for this user twice)

Login as new user by default

echo -e "[user]\ndefault=${MY_USERNAME}" > /etc/wsl.conf

Apply default username

First exit the container

exit

Back in the command prompt: restart the entire WSL container, to apply the default user

wsl --terminate 247web

(Optional) Hide the machine name from the console

# Login again
wsl -d 247web

Now in the WSL2 container you can update your profile settings (~/.bashrc). You can see/edit all settings with ‘nano ~/.bashrc’, or just run the following commands:

# Make a backup of the 'bashrc' file (profile settings) if it doesn't exist yet
[ ! -f ~/.bashrc_backup ] && cp ~/.bashrc ~/.bashrc_backup

# Remove the machine name from the console
sed -i 's/\]\\u@\\h\\\[/\]\\u\\[/g' ~/.bashrc

# Apply the console-name changes directly, and exit the container
source ~/.bashrc

exit

Congratulations!

Now you have a nice clean additional WSL2 machine, and it’s stored in your folder as ‘ext4.vhdx’ (you can remove the downloaded Ubuntu image if you want).

-img-

You can login again from the command prompt:

wsl -d 247web

Or for a better experience, open up Windows Terminal > Open a new tab > select 247web

-img-

You are now directly logged in to the home directory of your user when using Windows Terminal, and also have some nice colors!
( Use the ‘Windows Terminal Preview’ mode for that! )

DANGER ZONE

Note: to permanently delete the entire container, type: wsl –terminate

Note: to permanently delete the entire container, type: wsl –terminate <wsl2-name>

Sources: