如何在 Linux 上使用 Multipass 创建和启动 Ubuntu VM

本指南解释了什么是 多通道 以及如何在 Linux 中通过 Multipass 在几秒钟内立即创建和启动 Ubuntu VM。

什么是多通道?

有许多程序和工具可用于部署 Ubuntu VM 以进行测试和学习。 他们之中有一些是 码头工人, 虚拟机, LXD, LXC, Proxmox, 流浪汉, VirtualBox虚拟机 等等

我经常使用 KVM、Vagrant 和 Virtualbox 来测试多个 Linux 发行版上的各种应用程序。 今天,我们将学习另一个名为的虚拟化实用程序 多通道.

无论您是普通用户还是开发人员或系统 admin,您可以在 Linux 中使用 Multipass 立即创建和启动 Ubuntu VM。

Multipass 是一个轻量级的命令行虚拟机管理器,由 典范 团队,用于在本地计算机上快速部署 Ubuntu 实例。

由于 Multipass 为您提供最新的实例,因此您不必在发布时浪费时间进行更新! 使用 Multipass,您可以快速启动新的 Ubuntu VM 并使用它们来测试您的代码或应用程序。

它使用 Linux 上的 KVM、Windows 上的 Hyper-V 和 macOS 上的 HyperKit 以最小的开销运行虚拟机。 它也可以使用 VirtualBox 在 Windows 和 macOS 上。

您可以直接从本地主机在 VM 中运行命令,也可以登录到 VM 的 shell 并从那里运行。 还可以将主机系统的目录挂载到实例中,并在主机系统和 VM 之间共享文件和文件夹。

Multipass 是一个支持 GNU/Linux、macOS 和 Windows 的跨平台应用程序。

在 Linux 上安装多通道

Multipass 可用作 折断 包,因此它可以安装在任何支持的 Linux 操作系统上 snapd.

从版本 16.04 LTS、Ubuntu 风格、Solus 3 及更高版本、Zorin OS 等开始,所有 Ubuntu 版本中都预安装了 Snap。在其他 Linux 系统上,您可以按照本文所述安装 Snapd 关联.

安装 Snapd 后,使用以下命令安装 Multipass:

$ sudo snap install multipass

如果由于任何原因它不起作用,请尝试以下命令:

$ sudo snap install multipass --classic

成功安装 Multipass 后,您将看到以下输出:

multipass 1.0.2 from Canonical* installed

现在让我们继续在本地系统中部署 Ubuntu 实例。

在 Linux 上使用 Multipass 创建和启动 Ubuntu VM

使用 Multipass 运行 Ubuntu VM 非常简单直接。

1. 启动 Ubuntu 虚拟机

要下载并启动一个新的 Ubuntu VM,只需运行:

$ multipass launch --name ostechnix-instance

或者,

$ multipass launch -n ostechnix-instance

这里 ostechnix 实例 是我的 Ubuntu 虚拟机的名称。 您可以根据自己的喜好命名虚拟机。

上述命令将下载一个全新的最小 Ubuntu 实例并自动启动它。

在 Linux 上使用 Multipass 创建和启动 Ubuntu VM

默认情况下,Multipass 将下载当前的 Ubuntu LTS 版本。

2.列出虚拟机

要列出系统上当前下载的 VM,请运行:

$ multipass list

样本输出:

Name                    State             IPv4             Image
ostechnix-instance      Running           10.39.86.203     Ubuntu 18.04 LTS

正如您在上面的输出中看到的,一个名为“ostechnix 实例” 启动并运行 Ubuntu 18.04 LTS 版本。 并且实例的IP地址是 10.39.86.203.

3. 从本地系统执行虚拟机中的命令

Multipass 允许您在 Ubuntu 实例中运行命令。 为了 example,我们可以使用以下命令找到正在运行的 VM 的系统详细信息:

$ multipass exec ostechnix-instance -- lsb_release -a
使用多通道在 VM 中运行命令

4. 启动 Ubuntu VM 的外壳

无需从本地系统向 Ubuntu VM 运行命令,您可以直接启动其 SHELL 并运行命令或随意进行任何测试。

$ multipass shell ostechnix-instance
使用多通道启动 Ubuntu VM 的 shell使用多通道启动 Ubuntu VM 的 shell

类型 "exit" 从实例中注销。

5. 查找可用实例以启动

正如我已经说过的,Multipass 将为您提供当前的 LTS 版本,即 Ubuntu 18.04. 但是,如果您愿意,可以启动其他可用的 VM。

要列出所有可用的 VM,请运行:

$ multipass find

样本输出:

Image                   Aliases           Version          Description
snapcraft:core          core16            20200221         Snapcraft builder for Core 16
snapcraft:core18                          20200221         Snapcraft builder for Core 18
core                    core16            20200213         Ubuntu Core 16
core18                                    20200210         Ubuntu Core 18
16.04                   xenial            20200218.1       Ubuntu 16.04 LTS
18.04                   bionic,lts        20200218         Ubuntu 18.04 LTS
19.10                   eoan              20200129         Ubuntu 19.10
daily:20.04             devel,focal       20200226         Ubuntu 20.04 LTS

如您所见,Snapcraft 核心、Ubuntu 核心、Ubuntu 16.04、18.04、19.10 和 20.04 每日构建可用。

要启动任何其他实例,请指定图像名称,如下所示。

$ multipass launch --name ostechnix-instance 19.10

此命令将启动运行 Ubuntu 19.10 的实例。

6. 创建具有自定义规格(CPU 数量、硬盘大小和内存大小)的实例

默认情况下,Multipass 将创建 Ubuntu 虚拟机 1 个 CPU, 5GB硬盘1GB 内存. 这对于基本测试来说已经足够了。

但是,有时您可能希望创建一个配置低于或高于默认配置的 VM。 这也是可能的!

以下命令将启动具有 2 个 CPU、2 GB RAM 和 20 GB 硬盘大小的 VM。

$ multipass launch -c 2 -m 2G -d 20G -n ostechnix-instance

大小可以指定后缀 ķ (对于 KB), (对于 MB), G (对于 GB)。

  • 允许的最小 CPU 数: 1
  • 允许的最小硬盘大小: 512 MB
  • 允许的最小内存大小: 128 MB

7.查找正在运行的实例信息

可以通过以下命令查看正在运行的实例信息:

$ multipass info ostechnix-instance

样本输出:

Name:           ostechnix-instance
State:          Running
IPv4:           10.39.86.203
Release:        Ubuntu 18.04.4 LTS
Image hash:     3c3a67a14257 (Ubuntu 18.04 LTS)
Load:           0.08 0.02 0.01
Disk usage:     999.1M out of 4.7G
Memory usage:   74.0M out of 985.1M

8. 暂停实例

要暂停正在运行的实例,请运行:

$ multipass suspend ostechnix-instance

验证它是否已暂停或未使用 info 命令:

$ multipass info ostechnix-instance

样本输出:

Name:           ostechnix-instance
State:          Suspended
IPv4:           --
Release:        --
Image hash:     3c3a67a14257 (Ubuntu 18.04 LTS)
Load:           --
Disk usage:     --
Memory usage:   --

如你所见,我的 ostechnix 实例 被暂停。

9. 启动和停止实例

要停止正在运行的 VM,请运行:

$ multipass stop ostechnix-instance

启动虚拟机:

$ multipass start ostechnix-instance

10. 从 Multipass 托盘图标管理 Ubuntu 虚拟机

如果您对 Multipass 命令行选项不满意,可以从 Multipass 托盘图标管理 VM。

从主机系统的菜单或仪表板启动 Multipass GUI。 然后,您将在系统托盘上看到 Multipass 图标。

多通道系统托盘图标多通道系统托盘图标

通过多通道托盘图标,您可以:

  • 启动/停止虚拟机,
  • 打开正在运行的 VM 的 shell 访问权限,
  • 禁用/启用 Multipass 自动启动选项,
  • 退出多通道。

11. 删除虚拟机

完成 Ubuntu 实例后,如果不再需要它们,可以将其删除。

首先停止要删除的 VM。

$ multipass stop ostechnix-instance

然后,使用以下命令将其从本地主机中删除:

$ multipass delete ostechnix-instance
$ multipass purge

12. 寻求帮助

运行以下命令以查看可用选项和子命令的列表以及说明。

$ multipass help

样本输出:

Usage: multipass [options] <command>
Create, control and connect to Ubuntu instances.

This is a command line utility for multipass, a
service that manages Ubuntu instances.

Options:
  -h, --help     Display this help
  -v, --verbose  Increase logging verbosity, repeat up to three times for more
                 detail

Available commands:
  delete    Delete instances
  exec      Run a command on an instance
  find      Display available images to create instances from
  get       Get a configuration setting
  help      Display help about a command
  info      Display information about instances
  launch    Create and start an Ubuntu instance
  list      List all available instances
  mount     Mount a local directory in the instance
  purge     Purge all deleted instances permanently
  recover   Recover deleted instances
  restart   Restart instances
  set       Set a configuration setting
  shell     Open a shell on a running instance
  start     Start instances
  stop      Stop running instances
  suspend   Suspend running instances
  transfer  Transfer files between the host and instances
  umount    Unmount a directory from an instance
  version   Show version details

要查看每个子命令的帮助部分:

$ multipass help <sub-command>

例子:

$ multipass help launch

资源: