更新升级 专属应用 系统故障 硬件故障 电脑汽车 鸿蒙刷机 鸿蒙开发Linux教程 鸿蒙开发Linux命令
当前位置:HMXT之家 > 鸿蒙开发Linux教程 > 在Ubuntu 22.04系统上安装和使用Vagrant的方法

在Ubuntu 22.04系统上安装和使用Vagrant的方法

更新时间:2023-01-03 09:50:51浏览次数:196+次

本文介绍如何在Ubuntu 22.04系统上安装和使用Vagrant的方法。Vagrant是一个开源工具,用于在易于使用的单一工作流中构建和管理虚拟机环境,它专注于自动化、降低开发环境设置时间和提高生产效率。当前Vagrant可用于VirtualBox、KVM、Hyper-V、Docker容器、VMware和AWS。该软件使用Ruby编写,由HashiCorp积极开发。要声明的一点是,Vagrant依赖于系统上现有的管理程序,这可以是VirtualBox、KVM或VMware。

在Ubuntu 22.04上安装Vagrant

在Debian及其衍生产品上安装Vagrant的简单方法是从apt存储库中安装。

安装存储库添加依赖项:

sudo apt update

sudo apt -y install apt-transport-https ca-certificates curl software-properties-common

将官方Vagrant APT存储库添加到您的系统:

curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -

sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"

一旦添加了repo,请继续安装Vagrant:

sudo apt update

sudo apt install vagrant

安装需要几秒钟才能完成,以下是输出的信息:

Hit:1 http://security.ubuntu.com/ubuntu focal-security InRelease

Get:2 http://mirrors.digitalocean.com/ubuntu focal InRelease [265 kB]

Hit:3 https://apt.releases.hashicorp.com focal InRelease

Hit:4 http://mirrors.digitalocean.com/ubuntu focal-updates InRelease

Hit:5 http://mirrors.digitalocean.com/ubuntu focal-backports InRelease

Fetched 265 kB in 1s (340 kB/s)

Reading package lists... Done

Reading package lists... Done

Building dependency tree

Reading state information... Done

The following NEW packages will be installed:

  vagrant

0 upgraded, 1 newly installed, 0 to remove and 47 not upgraded.

Need to get 40.9 MB of archives.

After this operation, 115 MB of additional disk space will be used.

Get:1 https://apt.releases.hashicorp.com focal/main amd64 vagrant amd64 2.2.16 [40.9 MB]

Fetched 40.9 MB in 1s (33.4 MB/s)

Selecting previously unselected package vagrant.

(Reading database ... 65336 files and directories currently installed.)

Preparing to unpack .../vagrant_2.2.16_amd64.deb ...

Unpacking vagrant (2.2.16) ...

Setting up vagrant (2.2.16) ...

在Ubuntu 22.04上使用Vagrant

安装后,您可以检查版本:

$ vagrant --version

Vagrant 2.3.1

下载测试Vagrant Box。在本例中,我将下载Kali Linux:

$ vagrant box add offensive-security/kali-linux

要下载Ubuntu 20.04 Vagrant image,请使用:

$ vagrant box add generic/ubuntu2204 --provide=virtualbox

要使用Vagrant启动VM,您需要创建一个Vagrantfile。Vagrantfile的主要功能是描述项目所需的机器类型,以及如何配置和配置虚拟机:

mkdir boxes && cd boxes

touch Vagrantfile

以下是Vagrantfile内容的示例:

# -*- mode: ruby -*-

# vi: set ft=ruby :

ENV['VAGRANT_DEFAULT_PROVIDER'] = 'virtualbox'

Vagrant.configure("2") do |config|

  ##### DEFINE VM #####

  config.vm.define "ubuntu-01" do |config|

  config.vm.hostname = "ubuntu-01"

  config.vm.box = "generic/ubuntu2204"

  config.vm.box_check_update = true

  end

end

通过运行以下命令启动VM:

$ vagrant up

然后ssh到实例

$ vagrant ssh

要关闭VM,请使用:

$ vagrant halt

休眠VM:

$ vagrant suspend

通过清除所有数据将VM设置为初始状态:

$ vagrant destroy

至此,基本的Vagrant使用方法就讲完了。