Build multiple WSLs of the same version

WSL (Windows Subsystem for Linux) usually installs a Linux distribution from the Microsoft Store, but you can also build it using your own images. When WSL (Windows Subsystem for Linux) and Docker Desktop were installed on the same Windows PC, the WSL distribution sometimes moved by itself. Normally, when Docker Desktop is installed, it defaults to the WSL 2 distribution used by Docker, and it appears that the WSL distribution settings may be changed in the process. Furthermore, the Microsoft Store associated WSL (Windows Subsystem for Linux) does not allow two installations of the same version of the same distribution (it is possible by exporting and importing, which means duplication after all). Below are the steps to install a distribution using a custom Linux image in WSL 2.

Steps

  1. Enable WSL 2 (if not already enabled)
    wsl --install
    Or, to enable it manually:
    dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
    dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
  2. Obtaining a custom Linux image
    For example, download the appropriate root file system (rootfs) image from the official Ubuntu site or other sources.
    https://cloud-images.ubuntu.com/
  3. Import the custom image
    wsl --import --version 2
    Example:
    wsl --import MyCustomDistroName C:\WSL\MyCustomDistro C:\path\to\rootfs.tar.gz --version 2
  4. Check the imported distribution
    wsl -l -v
    The above command will confirm that the new distribution has been installed.
  5. Start a custom distribution
    wsl -d

Supplementation

  • How to create a root file system: To create a root file system yourself, you can, for example, use Docker to run the base Linux image as a container and save its contents as a tarball.
    docker run --name mydistro ubuntu
    docker export mydistro > mydistro.tar

Now you can start working with your custom Linux distribution on WSL. If you check in Explorer after importing, you should see it added to your Linux icon.

Also, a folder has been created where the compressed file ( rootfs.tar.gz ) is located, and you should find ext4.vhdx in it. Possible methods for creating a custom root file system image include the following

Method 1: Create a custom image using an existing Linux environment
Method 2: Create a custom image using Debootstrap
Method 3: Create a minimal custom image using BusyBox
Method 4: Migrate existing Ubuntu environment to WSL
Method 5: Migrate VirtualBox or VMware environment to WSL

If you import a custom image, you will be logged in as the root user by default. Distributions installed from the store have an initial setup wizard that creates a user for you, but custom images do not have such a procedure.

We strongly recommend that you create a regular user, even in a custom-created WSL environment. Below we discuss the importance of creating users and why it is important to do so.

Importance of Creating Users

1. Increased security

  • Restrictions on root privileges:.
    • If you are logged in as the root user by default, you run the risk of accidentally executing commands that affect the entire system. For example, it could delete critical system files or change system settings.
    • Creating a normal user and performing routine tasks as that user will limit operations with root privileges and improve overall system security.

2. System Stability

  • Secure Operation:.
    • The root user has access to all system files and settings, increasing the risk of system corruption due to mishandling. Using a regular user minimizes this risk.

3. Security Best Practices

  • Standard Practices:.
    • Many security best practices recommend that routine operations not be performed with root privileges. This reduces the attack surface of the system.
    • If necessary, only certain operations can be performed with root privileges using the sudo command.

To resolve these issues, you can create a new user and set that user as the default user. Follow the steps below to configure the settings.

Another way to change the default user for a WSL distribution is to use the following command. This is especially useful for Ubuntu distributions installed from the store, for example.

Procedure for Changing the Default User

This procedure shows how to create a new user, set that user as the default user, and restart WSL.

Procedure for setting the default user in a custom WSL distribution

1. Creating a New User

First, create a new user in WSL

adduser myusername

Next, add the new user to the sudo group and grant it administrative privileges

gpasswd -a myusername sudo

2. Edit the configuration file to set the default user

Edit the WSL configuration file to set the new user as the default user

nano /etc/wsl.conf

Once the file is open, add the following

[user]
default=myusername

When editing is complete, pressCtrl X, press Y to save changes, and press Enter to close the file.

3. Restart WSL

Restart WSL for the settings to take effect: open a Windows command prompt or PowerShell and run the following command

wsl --shutdown

Alternatively, you can shut down the system from within the WSL by running the following command

shutdown now

4. Verify that the configuration has taken effect

Re-start the WSL and verify that you are logged in as the new user. You can verify that you are logged in as the default user by running the following command

whoami

Now that the new user is set as the default user, you will be logged in as this user every time you start WSL. For your information, /etc/wsl.conf had the following description, which I added below.

[boot].
systemd=true

Install Docker and NVIDIA Container Toolkit. The following page describes how to install them.

https://docs.docker.com/engine/install/ubuntu

https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/latest/install-guide.html

Please note that you can use Docker commands without sudo.

Steps to use Docker as a non-root user

1. Create a Docker group

First, create a docker group if one does not exist. Normally, a docker group is created automatically when you install Docker, but to be sure, run the following command to check.

sudo groupadd docker

2. Add a user to the Docker group

Next, add users who want to use Docker to the docker group.

sudo usermod -aG docker myusername

3. Log out and log in for the changes to take effect

After adding the user to the group, log out and log back in for the changes to take effect; for WSL, simply restart WSL.

wsl --shutdown

4. Confirmation of Operation

After logging back in, verify that you can run Docker commands as a non-root user.

docker run hello-world
or
docker ps
etc.

To verify that the NVIDIA Container Toolkit is installed correctly, you can perform the following steps. This includes checking the toolkit version and testing the availability of the GPU.

Steps to verify installation

1. Check the version of the NVIDIA Container Toolkit

First, check the version of the NVIDIA Container Toolkit to see if it is installed.

nvidia-container-cli --version

If this command works correctly and displays version information, the toolkit is installed.

Please share if you like it!
TOC