如何在 Windows 上运行多个 WSL2 实例? [英] How to run multiple WSL2 instances on Windows?

查看:214
本文介绍了如何在 Windows 上运行多个 WSL2 实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经安装了 WSL2 并在其上部署了两台机器:

I have installed WSL2 and deployed two machines on it:

  • CentOS
  • Ubuntu-20.04

是否可以在我的 win10 机器上运行 Ubuntu-20.04 实例的多个实例?

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

推荐答案

是的,完全有可能.这是我推荐的步骤.在 PowerShell 中(您也可以使用 cmd):

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

  • 在您想要处理 WSL 图像的方便位置创建一个目录.当然,我的名字是WSL.这不必在您的 C:\ 驱动器上.

  • 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.

创建两个子目录,imagesinstances.同样,名称完全由您决定.

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

wsl -l -v 只是为了确认您当前的发行版名称.

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

使用诸如 wsl --export Ubuntu images\2021-02-05_Ubuntu20.04_snapshot.tgz 之类的东西备份您现有的 20.04 发行版(假设您的发行版名为Ubuntu",属于当然).

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

使用类似wsl --import Ubuntu20.04_clone instances\Ubuntu20.04_clone images\2021-02-05_Ubuntu20.04_snapshot.tgz --version 2.完成后,您应该会在 instances\Ubuntu20.04_clone 目录中看到一个 ext4.vhdx 文件.--version 是可选的,但表明您可以根据需要并排创建 WSL1 和 WSL2 实例.(例如,如果您需要访问 NTFS 文件系统,WSL1 会快得多.)

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.tgz --version 2. You should see a 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.)

使用 wsl ~ -d Ubuntu20.04_clone 启动该实例.或者重新启动 Windows 终端(假设您正在使用它),新实例应该会被自动检测到.

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.

默认情况下,新实例将以 root 身份启动.您需要通过使用以下内容创建 /etc/wsl.conf 来设置您的用户名:

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 and restart that distribution, and confirm that your environment is operating as you'd expect.

既然您已确认新实例正常运行,以下步骤是可选的,但建议执行.(对于另一种方法,请参阅这个 Reddit 线程).

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).

  • 重置"通过原始发行版的 wsl --unregister 将旧图像转换为干净状态.

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

重新运行应用程序"(从技术上讲,它是一个应用程序执行别名")从 Windows 开始菜单使用 Start->Run 并键入 Ubuntu.这将重新运行配置步骤并创建您的默认用户/密码/等.(感谢@JackoBongo 在评论中提出的建议).

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).

为方便起见,我建议继续将上述 /etc/wsl.conf 添加到此实例中.这将使您不必在每次添加或替换实例时都这样做.注意:如果您打算将此图像分发给其他用户,请不要这样做;只要你是为自己做这件事.

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.

备份这个干净的副本";使用上面的 wsl --export 命令,类似于 2021-02-05_Ubuntu20.04_default.tgz.这种干净"如果你想启动一个新的 Ubuntu 20.04 来测试一些东西而不用担心你的应用程序/自定义来自你的正常工作环境,那么版本现在可以成为你 wsl --import 的起点.

Back it up this "clean copy" using the wsl --export command as above, to something like 2021-02-05_Ubuntu20.04_default.tgz. 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.

既然您已经在运行多个发行版(CentOS 和 Ubuntu),那么我猜您已经在使用 Windows Terminal 之类的东西了.但是,如果您使用Ubuntu"通过开始菜单启动 Ubuntu条目,那么您可能希望将克隆的 vhdx 文件复制回新安装的干净版本.你会发现默认"%userprofile%\AppData\Local\Packages\CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc(或类似的东西)中的版本.

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).

这篇关于如何在 Windows 上运行多个 WSL2 实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆