如何在 Linux 中将下载的 .box 文件添加到 Vagrant

Linux 发行版有多种不同格式可供下载。 最广泛使用的格式是 ISO。 除了 ISO,它们还通过预配置的映像分发,例如 .box 对于流浪者, .vbox 对于虚拟盒子, .vmdk 对于 Vmware, .qcow2 用于 KVM/openStack,并压缩 RAW 等等,因此您可以快速获取您选择的图像并使用相应的虚拟化应用程序运行它。 这个简短的指南解释了如何添加下载的 .box 文件到 Linux 操作系统中的 Vagrant。

还不知道的小伙伴们, 流浪汉 是用于构建和维护虚拟软件开发环境的开源软件。 它提供了一个干净、易于配置、可重现和可移植的开发环境。 这 .box 是 Vagrant 环境的一种格式和扩展。 vagrant box 只是基本图像。 现在很多操作系统都在 .box 图像文件格式。

添加已下载 .box 在 Linux 中向 Vagrant 提交文件

今天想试试新出的 Fedora 我的系统上有 33 个操作系统。 与其从 ISO 文件手动下载和安装,我决定抓取现成的 Fedora 33 Vagrant box 并在其中运行几天 VirtualBox 看看它是如何工作的。 所以我去了 Fedora 官方下载页面并下载了 .box 文件为 Fedora 33. 然后我在我的 Ubuntu 桌面上安装了 Vagrant,如下面的指南所述:

  • 如何在 Linux 上安装 Vagrant

安装 Vagrant 后,我​​去了我下载的位置 .box 文件并使用命令将其添加到 Vagrant:

$ vagrant box add --name fedora33 Fedora-Cloud-Base-Vagrant-33-1.2.x86_64.vagrant-virtualbox.box

这里, 软呢帽33 是我分配给虚拟机的名称和 “Fedora-Cloud-Base-Vagrant-33-1.2.x86_64.vagrant-virtualbox.box” 是个 .box 我从下载的文件 Fedora 下载页面。

样本输出:

==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'fedora33' (v0) for provider: 
    box: Unpacking necessary files from: file:///home/sk/Vagrant/Fedora-Cloud-Base-Vagrant-33-1.2.x86_64.vagrant-virtualbox.box
==> box: Successfully added box 'fedora33' (v0) for 'virtualbox'!
在 Linux 中将下载的 .box 文件添加到 Vagrant

.bo为提供程序添加了 x 文件。 就我而言,提供者是 Oracle VirtualBox.

让我们通过列出可用的 Vagrant 框来验证它,如下所示:

$ vagrant box list 
fedora33 (virtualbox, 0)
列出 Vagrant 框

接下来使用命令初始化 Vagrant 框:

$ vagrant init fedora33

样本输出:

A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

现在开始 Fedora 虚拟机通过运行以下命令:

$ vagrant up

样本输出:

Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'fedora33'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: Vagrant_default_1606997309282_57379
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: 
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default: 
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: The guest additions on this VM do not match the installed version of
    default: VirtualBox! In most cases this is fine, but in rare cases it can
    default: prevent things such as shared folders from working properly. If you see
    default: shared folder errors, please make sure the guest additions within the
    default: virtual machine match the version of VirtualBox you have installed on
    default: your host and reload your VM.
    default: 
    default: Guest Additions Version: 6.0.0 r127566
    default: VirtualBox Version: 6.1
==> default: Rsyncing folder: /home/sk/Vagrant/ => /vagrant
开始 Fedora 命令行中的流浪框开始 Fedora 命令行中的流浪框

这 Fedora 33 vagrant box 下运行 VirtualBox.

打开 Virtualbox 管理器并检查它是否正在运行:

Fedora  vagrant box 正在运行 VirtualBoxFedora vagrant box 正在运行 VirtualBox

您还可以使用以下命令从终端检查 vagrant 框的状态:

$ vagrant status

样本输出:

Current machine states:

default                   running (virtualbox)

The VM is running. To stop this VM, you can run `vagrant halt` to
shut it down forcefully, or you can run `vagrant suspend` to simply
suspend the virtual machine. In either case, to restart it again,
simply run `vagrant up`.

是的,虚拟机正在运行!

通过以下方式连接并访问正在运行的虚拟机 ssh 如下所示:

$ vagrant ssh

开始使用虚拟机:

Last login: Thu Dec  3 12:13:42 2020
[[email protected] ~]$ cat /etc/redhat-release 
Fedora release 33 (Thirty Three)
[[email protected] ~]$ 

请注意,我下载了 Virtualbox 盒子图像,所以 Fedora 虚拟机在 Virtualbox 中自动启动。 如果您已经下载了 libvirt/kvm 映像,它将在 kvm 管理程序下运行。

好吧,这就是现在的全部。 你现在学会了如何添加下载的 .box 文件在 Linux 中为 vagrant。 您还学习了如何启动 Vagrant 框以及如何连接以从命令行访问它。

要了解有关 Vagrant 使用的更多信息,请参阅以下指南:

  • Vagrant 教程 – Vagrant 入门

希望这可以帮助。