如何在 WSL 中安装多个不同的 Ubuntu 实例?

在 Windows10中,如何在 WSL 中安装多个单独的 Ubuntu 实例?我希望不同的工作空间有不同的实例。例如,一个用于 Python 开发,一个用于 Ruby 开发,一个用于。网络核心开发等。我知道我可以在 WSL 实例中把所有这些都塞进同一个 Ubuntu 中,但是我更愿意为这些场景中的每一个单独设置一个。这可能吗?

78420 次浏览

It is possible, but requires some work. You can use LxRunOffline - "A full-featured utility for Windows Subsystem for Linux (WSL)".

You can install it via Chocolatey: choco install lxrunoffline, or download and unzip.

You might want to add LxRunOffline.exe to your PATH.

https://lxrunoffline.apphb.com/download/{distro}/{version} will redirect to download page for desired distro. In this case it would be .../ubuntu/xenial or similar, according to lxrunoffline wiki, or you can download directly from Canonical.

Then you can:
LxRunOffline install -n someName -d where/to/install -f path/to/downloaded/distro multiple times with different names and target directories.

Then you can lxrunoffline -w -n someName to start desired installation, and finally, you can create multiple shortcuts on the desktop with different options for specific work spaces.
LxRunOffline available commands:

list           List all installed distributions.
get-default    Get the default distribution, which is used by bash.exe.
set-default    Set the default distribution, which is used by bash.exe.
install        Install a new distribution.
uninstall      Uninstall a distribution.
register       Register an existing installation directory.
unregister     Unregister a distribution but not delete the installation directory.
move           Move a distribution to a new directory.
duplicate      Duplicate an existing distribution in a new directory.
run            Run a command in a distribution.
get-dir        Get the installation directory of a distribution.
get-env        Get the default environment variables of a distribution.
set-env        Set the default environment variables of a distribution.
get-uid        Get the UID of the default user of a distribution.
set-uid        Set the UID of the default user of a distribution.
get-kernelcmd  Get the default kernel command line of a distribution.
set-kernelcmd  Set the default kernel command line of a distribution.
get-flags      Get some flags of a distribution. See https://msdn.microsoft.com/en-us/library/windows/desktop/mt826872(v=vs.85).aspx for details.
set-flags      Set some flags of a distribution. See https://msdn.microsoft.com/en-us/library/windows/desktop/mt826872(v=vs.85).aspx for details.
version        Get version information about this LxRunOffline.exe.

First we have to find the install location of that Windows Store Appx. Here is a Powershell script to find that path. Enter the distribution name at first (e.g. Ubuntu18.04).

$DistroName=Read-Host "Enter Distribution Name"
$path = (Get-AppxPackage "*$DistroName*").InstallLocation
echo $path
Invoke-Item $path
pause

The installation path for Ubuntu 18.04 will be:

%ProgramFiles%\WindowsApps\CanonicalGroupLimited.Ubuntu18.04onWindows_1804.2018.427.0_x64__79rhkp1fndgsc

In the PS script, Invoke-Item will open that path in File Explorer. If that path is not visible or shows any security issue, then grant permission to access that folder from its Properties menu. Now copy just these two required files:

  1. Distribution userspace tarball named as install.tar.gz (or any TAR.GZ file).
  2. Main executable file to install, named as Ubuntu.exe or Ubuntu1804.exe or Ubuntu1604.exe etc.

Here comes the next section. Backup and then delete the registry key HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Lxss. Place those two files in a folder structure like this (or as you want):

C:\MyFiles
|
+-- UbuntuPython
|   |
|   +-- ubuntu.exe
|   +-- install.tar.gz
|
+-- UbuntuRuby
|
+-- ubuntu.exe
+-- install.tar.gz

The folder names should be different. Now double click on the first copied .exe excutable, wait until it installs. Open HKCU\Software\Microsoft\Windows\CurrentVersion\Lxss\<some-GUID> and change the DistributionName string registry to UbuntuPython (or any). Repeat the procedure with every instances. The GUID will be new one for every instances. Make sure you change the DistributionName registry entry to different values for each one, otherwise ubuntu.exe will execute wsl.exe instead of installing. See the source code of those EXE files here GitHub: Microsoft/WSL-DistroLauncher.

The newer wsl command's import/export feature can readily create copies of a distro without installing any additional tools or using RegEdit. For example

cd C:\Users\MattSlay
wsl --export Ubuntu ubuntu.tar
wsl --import UbuntuRuby .\UbuntuRuby ubuntu.tar
wsl --import UbuntuPython .\UbuntuPython ubuntu.tar
wsl --import UbuntuDotNet .\UbuntuDotNet ubuntu.tar
wsl -d UbuntuRuby

wsl -d <distro> launches the distro. If WSL 2 has been installed, then the distro can be converted between versions during --import using the --version option:

wsl --import UbuntuRuby .\UbuntuRuby ubuntu.tar --version 2

A smaller distro, such as Alpine, can make experimentation much faster. Finally, wsl --import can read from standard input - and wsl --export can write to standard output -. This allows a compression program to be used to save disk space if desired.

Side note: The default login of imported distro is root.
For Build 18980 and later, you may create or modify /etc/wsl.conf to set your default login user instead of fiddling with registry or login with wsl -u <username>. Reference

Content of /etc/wsl.conf. It might take effect after shutdown or logout.

[user]
default=<your-default-username>

I recently had the same issue, as I am working with different companies at the same time, and needed a script that could easily create a new WSL2 instance, and just as easily delete it.

Deletion by itself is not hard, as the official command wsl --unregister <distro_name> works just fine, and gets rid of the WSL2 instances perfectly, without leaving any trace.

Creation however, can be troublesome or even tedious. My script does just this: https://github.com/IAL32/WSL2-Create-Distro

The following example creates a WSL2 instance using Ubuntu20.04 (Focal Fossa) using a previously downloaded tarball (the example uses the following one: https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64-wsl.rootfs.tar.gz), creates a user with username myuser and adds it to the group sudo. It also sets the user password to the one given in the pipeline, and the password for the root user, also given in the pipeline.

.\CreateLinuxDistro.ps1 -INPUT_FILENAME .\focal-server-cloudimg-amd64-wsl.rootfs.tar.gz -OUTPUT_DIRNAME "$env:LOCALAPPDATA\Packages\ubuntu2004-test-1" -OUTPUT_DISTRONAME ubuntu2004-test-1 -CREATE_USER 1 -CREATE_USER_USERNAME myuser -ADD_USER_TO_GROUP 1 -ADD_USER_TO_GROUP_NAME sudo -SET_USER_AS_DEFAULT myuser

Under the hood, if a new user is created it will also change its default shell from /bin/sh to /bin/bash.

Rather than duplicating distributions from Microsoft Store or .appx files, another solution is by utilizing Docker in existing Linux distribution running on WSL. This method is actually explained in WSL Docs by Microsoft.

This method is beneficial because you can utilize a variety of distros that have been available in the Docker registry.

For example, you already have Ubuntu-18.04 distribution installed from Microsoft Store.

  1. Run the distribution from terminal
wsl -d Ubuntu-18.04
  1. Install Docker for Ubuntu by following instruction from Docker Docs

  2. Run docker service

service docker start
  1. Run a container with any Linux distribution you desired, for example, Ubuntu 20.04
docker run -d ubuntu:20.04
  1. List all containers, and keep the Container ID of the distribution you desired
docker ps -a
  1. Export the selected container into a .tar file, for example, the Container ID is 123123abcabc and you want to store the exported file in C:\data.
    WSL mounts your Windows directory into /mnt directory on the running Linux. If you have a C:\data directory on Windows, its content can be accessed on your Linux in /mnt/c/data directory.
docker export 123123abcabc > /mnt/c/data/exported-ubuntu-20.04.tar

Now, the exported file is available in C:\data\exported-ubuntu-20.04.tar.

  1. Exit from Linux, back to Windows terminal, then import the exported container as a new distribution with any desired name and stored in any location in Windows
exit
wsl --import anyName C:\data\anyLocation C:\data\exported-ubuntu-20.04.tar
  1. Now, you can see your new distribution in the list and run it
wsl --list -v
wsl -d anyName