Several ways to create multiple WSL environments

By setting up multiple WSL (Windows Subsystem for Linux) environments, each environment can be specialized for different purposes and projects. This provides the following advantages

  1. Isolation: Changes made in one WSL instance will not affect other instances. This allows different projects or experiments to work without interfering with each other.
  2. Security and stability: By using WSL instances dedicated to a specific application or development environment, you can safely experiment with new tools and libraries while maintaining system-wide security and stability.
  3. Simplified management of environments: Separating WSL instances for different projects or uses makes it easier to manage the process of recreating, modifying, or updating a particular environment.

Adding and managing WSL instances is relatively easy through Windows configuration and command line tools. However, each instance consumes storage space independently, so consideration should be given to the resources it uses.

The following methods are available for creating multiple instances of the same distribution in WSL (Windows Subsystem for Linux), each requiring certain technical steps.

Using different versions of the distribution

  1. Look for different versions of the distribution: For example, with Ubuntu, you can install different releases (e.g. 18.04, 20.04, 22.04, etc.). These are essentially the same Ubuntu, but are recognized as different versions; check the Microsoft Store or WSL distribution list for available options.

Using a custom Linux image

  1. Create a custom Linux image: This is a more advanced method of creating a custom Linux image and importing it into WSL. This involves creating a Linux image, customizing it, and formatting it appropriately for WSL.

Clone an existing instance

  1. Exporting and importing an existing instance: this is technically relatively simple, but involves creating a “snapshot” of an existing instance and importing it as a new instance. However, this will not be an “initial” instance, but an instance that reflects the state of the instance at the time of export.

Difficulty Levels

  • Different versions of distributions: relatively easy. Depends on available options.
  • Custom Linux images: Advanced knowledge required; in-depth understanding of Linux is required.
  • Cloning an existing instance: Technically easy, but not the initial instance.

All that is required is a basic understanding of Linux and WSL and comfort with command line operations. The easiest way is to use different versions of the distribution, but this depends on the available versions. Creating custom images requires more advanced skills and in-depth understanding, but is more flexible.

Other possible methods

Other methods for managing multiple instances of the same distribution in WSL (Windows Subsystem for Linux) are limited, but the following approaches are possible

1. Manual instance replication

  • Manual replication: This method involves manually copying the file system of an existing instance and importing it as a new instance. This involves a physical copy of the file system and its manual placement in the new instance. However, this method is complex and requires specific knowledge and attention.

2. Use of third-party tools

  • Third-party tools: Another option is to use third-party tools that specialize in the management and replication of WSL instances. These tools may offer features that make WSL operations easier and more efficient. However, care should be taken to ensure the reliability and security of these tools.

3. Use of Docker and other container technologies

  • Use of Docker or other container technology: This method uses Docker or other container technology on WSL to create multiple isolated environments. Docker is integrated with WSL2 and can easily handle Linux containers.

SUMMARY

Of these methods, the easiest and least risky is to use different versions of the distribution or to use third-party tools. Manual replication and the use of Docker require more technical knowledge, but provide a more flexible environment. Each method is a good choice depending on your specific needs and skill level. The method I use most often is to install Docker in a WSL and launch containers, because I have never had to contaminate the inside of a WSL, and I have never had to use Docker in a WSL because I have never had to use Docker in a WSL. However, if you change the Python version or update libraries or dependencies within the same WSL, the following happens Changes in a particular WSL instance can affect applications and services running within that instance. This is based on general Linux system administration principles.

So, in this case, we decided to take the approach of duplicating existing instances and deleting those that we do not need.

First, we will check the name of the existing WSL (Windows Subsystem for Linux) distribution, using the Windows command prompt or PowerShell. The following steps can be used to check.

  1. Open Command Prompt or PowerShell: Type “cmd” or “PowerShell” in the Windows search bar and open the application that appears.
  2. View a list of WSL distributions: Run the following command

    wsl --list --verbose

    Or in short form

    wsl -l -v

    This command displays a list of all installed WSL distributions and indicates the status (running or stopped) and version (WSL 1 or WSL 2) of each distribution.
  3. Confirm distribution name: From the list displayed, confirm the name of the distribution you wish to back up or operate.


In my environment, the following was displayed

C:\Users\minok>wsl --list --verbose
NAME STATE VERSION

  • Ubuntu Stopped 2

WSL (Windows Subsystem for Linux) has an export and import feature that can be used to backup an existing WSL instance and restore it as a new instance. This can also be used to create new instances, which is very useful.

Exporting a WSL instance

  1. Export command: To export an existing WSL instance, use the following command

    wsl --export [distribution name] [filename.tar]

    where [distribution name] is the name of the WSL distribution you wish to export and [filename.tar] is the name of the file to export.

Creating and importing a new instance

  1. Creating a new instance: To create a new instance, first create a directory with the new distribution name.

    wsl --import [new distribution name] [installation path] [filename.tar]

    where [new distribution name] is the name of the new WSL instance, [install path] is the path to the directory where the instance will be installed, and [filename.tar] is the exported file.

With this feature, you can easily duplicate a WSL instance with a specific configuration and environment and use it for different projects and purposes. However, the size of the exported file can be large, so be careful with storage space. In my case, the stable diffusion model takes up a lot of space, so I delete it or move it to another location. Also, depending on the WSL version and settings, some adjustments may be necessary when importing.

In WSL (Windows Subsystem for Linux) 2, imported Linux distributions may appear in Windows Explorer as a section named “Linux”. This is one of WSL 2’s features to facilitate file sharing and access between Windows and Linux. This makes it easy to work with stave diffusion models.

Integration of WSL 2 with Windows Explorer

  1. Ease of access: The “Linux” section displayed in Explorer provides access to the root file system of each installed Linux distribution.
  2. File sharing and editing: This feature allows users to view, edit, and copy files in the Linux file system directly from the Windows side. This greatly improves the efficiency of working between WSL and Windows.
  3. Creation of a new folder: When you import a new WSL instance, a new folder corresponding to that instance will be created in the “Linux” section. This folder provides direct access to the root directory of that particular Linux distribution.

Usage Notes

  • Avoid compatibility issues: Linux file systems have different file system attributes than Windows, so care must be taken when copying files from the Windows side to the Linux file system.
  • Permissions: Linux file permissions are different from Windows, so it is recommended that any changes to file or directory permissions be done on the Linux side.
  • Data integrity: When moving or copying data between Linux and Windows file systems, it is important to operate carefully to maintain data integrity.

This integration feature allows WSL users to work seamlessly between Windows and Linux, greatly improving development and data management efficiency.

Actual command

wsl --export Ubuntu ubbk.tar

command will create the specified compressed file (tar format) in the location (current directory) where the command was executed. To export, enter the following command

wsl --import Ubuntu1 c:\wsl\ubuntu1 ubbk.tar

The specified path cannot be found.
Error code: Wsl/ERROR_PATH_NOT_FOUND

When the above error occurs, it is because the installation folder has not been created in advance. Create it and then import it.

The “installation path” you specify when importing an instance of WSL (Windows Subsystem for Linux) is selected based on several factors. Typically, the appropriate path is selected based on the following considerations

  1. Storage space: Since WSL instances can consume a significant amount of disk space, it is important to choose a drive with sufficient free space.
  2. Access speed: Installing on a solid state drive (SSD) improves performance. Boot times are reduced and file system operations are faster.
  3. Security and permissions: Avoid installing directly to system files or critical drives, and choose a directory to which users have full access rights.
  4. Ease of Administration: WSL instances should be located in an easy-to-remember location so that they can be easily managed. For example, use clear and easy to understand path names such as C:\WSL\ or D:\LinuxDistros\.

Examples of common installation paths

  • Case for installation on the C drive (fast, but puts a load on the system drive).

    C:\WSL\Ubuntu
  • Case for installation on drive D or some other drive (does not affect the system drive and is easier to manage).

    D:\LinuxDistros\Ubuntu

The selected path must be clearly specified within the import command. For example, to import into D:\LinuxDistros\Ubuntu, the command would be

wsl --import Ubuntu D:\LinuxDistros\Ubuntu [path of export file]

The choice of installation path is based on available storage, performance requirements, and ease of administration.

After the import is complete, a folder will be created in Linux in Explorer. You can also check it with the previous command.

wsl --list --verbose

After importing the distribution in WSL (Windows Subsystem for Linux), a .vhdx (Virtual Hard Disk ) file is created. This is part of the virtualization technology used by WSL 2 to store the file system of the Linux instance.

About .vhdx files

  • VHDX format: VHDX is a virtual hard drive format, primarily used by a Windows virtualization solution called Hyper-V. WSL 2 uses this technology to run the Linux kernel, providing high performance and full system call compatibility.
  • File Contents: The .vhdx file contains all data for the WSL instance. This includes system files, user data, installed applications, etc.
  • File Location: This file is stored in the path specified when importing the instance. This location is important for accessing the instance data and for backups.

In WSL (Windows Subsystem for Linux) 2, the .vhdx (Virtual Hard Disk ) file is automatically mounted and used as the file system for the Linux instance. This is due to WSL 2’s use of Hyper-V virtualization technology on the back end, and is done through the following process

  1. Automatic mount at Windows startup: When Windows starts, WSL 2 automatically mounts the .vhdx file and makes it available to the Linux distribution.
  2. Acts as the filesystem for the Linux instance: The mounted .vhdx file acts as the root filesystem for the Linux instance. This means that all files and directories seen within the Linux environment are stored within this .vhdx file.
  3. Transparent access: Users normally do not need to be aware of this mounting process when using WSL 2; when the Linux environment is opened, the file system is automatically made available and normal Linux operations can be performed.
  4. Unmount upon WSL exit: When you exit the WSL 2 instance, the .vhdx files are safely unmounted.

The main options for starting different distributions and instances of WSL (Windows Subsystem for Linux) using different distributions and instances are as follows

1. Booting a specific distribution from the command line

You can start a specific WSL distribution directly from the command prompt or PowerShell. To do this, use the following command

wsl -d [distribution name]

For example, to start the installed “Ubuntu” distribution

wsl -d Ubuntu

2. Changing the default distribution

You can set a specific distribution as the default and simply run the wsl command to boot that distribution. To change the default distribution, use the following command

wsl --set-default [distribution name]

Example:
wsl --set-default Ubuntu

After making this setting, simply type wsl and run it to start the distribution set as default.

3. Start from the Windows Start Menu

The installed WSL distribution will usually also appear in the Windows Start menu. You can select a specific distribution from the Start menu to start it.

4. Creating Shortcuts

You can also create a shortcut on the desktop or taskbar to launch a specific distribution. The target of the shortcut is set to the command (e.g., wsl -d Ubuntu ) that launches the specific distribution.

5. Use scripts

If you need to start a distribution with a complex startup sequence or a specific configuration, you can create a batch file or script containing the startup command and run it to start the WSL distribution.

Using these methods, you can easily switch between different distributions within the WSL environment.

If you duplicate (export and import) a distribution in WSL (Windows Subsystem for Linux), the new instance may require the user account to be set up again when it is first started. This may result in logging in as the root user by default.

Setting up a new user account

To set up a non-root user in a new instance, perform the following steps

  1. Create a user: After logging in as the root user in the new instance, create a new non-root user account. For example, if the user name is “username”, use the following command

    adduser username
  2. Grant sudo privileges to user: To grant sudo privileges to the new user, add the user to the sudo group.

    usermod -aG sudo username
  3. Log in as new user: Next time, log in with the new user account.

Changing the default user

To change the default user for a new instance from root to the new user, execute the following command

wsl --set-default-user username

where “username” is the name of the newly created non-root user.

This command changes the default user for the specified WSL instance. This will automatically log you in as the new non-root user the next time you start that WSL instance.

These steps provide the basic user settings for secure use of the new WSL instance. In my environment, the replicated WSL defaulted to root. I believe the following method may also be possible.

su username - command can be used in Linux environments such as Ubuntu to switch from the current user to a user named username.-(hyphen) to log in with that user’s environment variables and settings loaded. Since it is most likely duplicated, username can be the username and password of the original duplicate. This command is especially useful when switching from the root user to another user.

Usage

  1. Log in as root user: Initially log in as the root user. This may be done automatically when you start a WSL instance.
  2. Switching users: Run the command su username - where username is the name of the user you want to switch to. where username is the name of the user you want to switch to.

    su username –
  3. Working in the user’s environment: This command switches to username‘ s user environment. This allows you to work with that user’s home directory, environment variables, etc. applied.

Notes

  • To use the su command, you must know the password for that user.
  • The su command will only switch users for the current session. To change the system-wide default user, you must use the wsl --set-default-user command.
  • The su command will start a new shell and change the user, but you must run the exit command to return to the original shell.

After exporting an instance of WSL (Windows Subsystem for Linux), if you no longer need its compressed files (in tar format), especially if the import was successful and you are sure that the new instance is running without problems, it is It is recommended that you delete the compressed file. In my environment, it was about 50 GB. There are several reasons for this

  1. Save disk space: Since WSL instances can use a significant amount of space, deleting backup files that are no longer needed can save valuable disk space.
  2. Organize and Simplify Management: By regularly deleting old or unnecessary files, you can organize your system and simplify file management.
  3. Security: Old backups and system images can contain security risks, so it is important to regularly delete them and keep them up-to-date.

Deletion Methods

Files can be deleted using Windows Explorer or at the command prompt or PowerShell. For example, to delete a file at the command prompt or in PowerShell, do the following

del [file path]

where [file path] is the path to the file to be deleted. For example, to delete C:\WSL_Backups\UbuntuBackup.tar

del C:\WSL_Backups\UbuntuBackup.tar

Notes.

  • Before deleting the file, make sure that the import has completed successfully and the new instance is working as expected.
  • File deletion may be irreversible, so choose the files to be deleted carefully.
  • If you are retaining files for backup purposes, consider storing them in a secure location, such as an external drive or cloud storage.

It is also possible to export and duplicate a WSL (Windows Subsystem for Linux) distribution and import it to another PC. This allows the same Linux environment to be moved or shared between different machines. This process is especially useful if you want to unify your development environment and settings.

Please share if you like it!
TOC