Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run multiple WSL2 instances on Windows?

I have installed WSL2 and deployed two machines on it:

  • CentOS
  • Ubuntu-20.04

It is possible to run multiple instances of the Ubuntu-20.04 instance on my win10 machine?

like image 306
古今中 Avatar asked Feb 05 '21 14:02

古今中


2 Answers

Yes, entirely possible. Here are my recommended steps. In PowerShell (you could use cmd also):

  • Create a directory somewhere convenient where you want to work with your WSL images. Of course, I name mine WSL. This does not have to be on your C:\ drive.

  • Create two subdirectories, images and instances. Again, the names are really up to you.

  • wsl -l -v just to confirm your current distribution names.

  • Back up your existing 20.04 distro using something like wsl --export Ubuntu images\2021-02-05_Ubuntu20.04_snapshot.tar (assuming your distribution is named "Ubuntu", of course).

  • Create a new instance using that snapshot using something like wsl --import Ubuntu20.04_clone instances\Ubuntu20.04_clone images\2021-02-05_Ubuntu20.04_snapshot.tar --version 2. You should see an ext4.vhdx file in your instances\Ubuntu20.04_clone directory when it is done. The --version is optional, but shows that you can create WSL1 and WSL2 instances side-by-side if needed. (For instance, WSL1 is much faster if you need to access the NTFS filesystem.)

  • Launch that instance using wsl ~ -d Ubuntu20.04_clone. Or restart Windows Terminal (assuming you are using it) and the new instance should be detected automatically.

  • The new instance will launch as root by default. You will need to set your username by creating a /etc/wsl.conf with the following:

    [user]
    default=me
    

    ... of course, substituting your username.

  • Exit, terminate (via wsl --terminate Ubuntu20.04_clone), and restart that distribution, and confirm that your environment is operating as you'd expect.

  • Now that you have confirmed your new instance operates correctly, the following steps are optional, but recommended. (For an alternative method, see u/Zoredache's comments (plural) in this Reddit thread).

    • "Reset" the old image to a clean state by wsl --unregister <distroname> of the original distribution.

    • Re-run the "App" (technically it's an "App Execution Alias") from the Windows Start Menu with Start->Run and type Ubuntu. This will re-run the configuration steps and create your default user/password/etc. (Thanks @JackoBongo for the suggestion in the comments for this).

    • For convenience, I recommend going ahead and adding the /etc/wsl.conf as described above to this instance. That will keep you from having to do it each time you add or replace an instance. Note: Don't do this if you plan to distribute this image to other users; just if you are doing this for yourself.

    • Back up this "clean copy" using the wsl --export command as above, to something like 2021-02-05_Ubuntu20.04_default.tar. This "clean" version can now be your starting point for a wsl --import if you want to spin up a new Ubuntu 20.04 to test something out without worrying that your apps/customizations from your normal working environment get in the way.

    • Since you are already running multiple distributions (CentOS and Ubuntu), then I'm guessing you are using something like Windows Terminal already. But if you launch Ubuntu through the start menu with the "Ubuntu" entry, then you will probably want to copy your cloned vhdx file back over the newly installed clean version. You'll find the "default" version in %userprofile%\AppData\Local\Packages\CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc (or something similar).

like image 167
NotTheDr01ds Avatar answered Sep 18 '22 10:09

NotTheDr01ds


There is also a GUI on GitHub (my project) for managing multiple WSL2 instances. Might save you a little time.

like image 44
Bostrot Avatar answered Sep 18 '22 10:09

Bostrot