Vagrant基本命令详解

本文详细介绍Vagrant的使用方法,包括基本命令如初始化、启动、登录虚拟机,以及更高级的打包命令。适合初学者快速上手和进阶学习。

正在学习Docker Swarm,接触到了Vagrant。Vagrant是一个创建虚拟机的技术,可以把它认为是一个Vmware,它让我们可以通过代码的方式快速地、可重复地创建针对不同虚拟环境的虚拟机,包括Virtualbox、AWS、Docker等。它使得我们可以一次性地、自动创建多个环境相同的虚拟机,对于软件开发和测试尤其有用,以下是在使用过程中常用到的一些基本命令。

一、Vagrant基本命令详解

1. 显示当前版本

# vagrant --version
Vagrant 2.0.2

2. 列出所有Box

#vagrant box list
iamseth/rhel-7.3 (virtualbox, 1.0.0)

3. 添加一个Box

#vagrant box add [options] <name, url, or path>

1)可以从https://app.vagrantup.com/boxes/search下载各种Vagrant映像文件

#vagrant box add ubuntu/trusty64

2) 通过指定的URL添加远程box

#vagrant box add https://atlas.hashicorp.com/ubuntu/boxes/trusty64

3) 添加一个本地box

命令格式如下

# vagrant box add {box_name} {file_path}

示例如下:

# vagrant box add CentOS7.1 file:///D:/Work/VagrantBoxes/CentOS-7.1.1503-x86_64-netboot.box

4. 初始化一个新VM

# vagrant init ubuntu/trustry64

此命令会在当前目录创建一个名为Vagrantfile的配置文件,内容大致如下:

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/trusty64"
end

当在此目录启动Vagrant后,Vagrant会从互联网下载“ubuntu/trusty64”这个box到本地,并使用它作为VM的映像。

5. 初始化一个新VM

# vagrant up

如果我们想启动任意VM,首先进入有Vagrantfile配置文件的目录,然后执行上面的命令。控制台的输出通常如下:

Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'ubuntu/trusty64'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'ubuntu/trusty64' is up to date...
==> default: Setting the name of the VM: start_default_1518789015107_16928
==> default: Clearing any previously set forwarded ports...
Vagrant is currently configured to create VirtualBox synced folders with
the `SharedFoldersEnableSymlinksCreate` option enabled. If the Vagrant
guest is not trusted, you may want to disable this option. For more
information on this option, please refer to the VirtualBox manual:

  https://www.virtualbox.org/manual/ch04.html#sharedfolders

This option can be disabled globally with an environment variable:

  VAGRANT_DISABLE_VBOXSYMLINKCREATE=1

or on a per folder basis within the Vagrantfile:

  config.vm.synced_folder '/host/path', '/guest/path', SharedFoldersEnableSymlinksCreate: false
==> 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: 4.3.36
    default: VirtualBox Version: 5.2
==> default: Mounting shared folders...
    default: /vagrant => /Users/qianlong/DockerProjects/vagrant/start

6.启用SSH登陆VM

进入Vagrantfile配置文件所在的目录,执行以下命令:

# vagrant ssh

如果需要从虚拟机中退出,直接在虚拟机中的命令行输入exit命令即可

7. 查看VM当前的状态

进入Vagrantfile配置文件所在的目录,执行以下命令:

# vagrant status

如果VM正在运行,你可以在控制台中看到如下信息:

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

8.关闭VM

进入Vagrantfile配置文件所在的目录,执行以下命令:

# vagrant halt

9.销毁VM

命令格式如下:

vagrant destory [name|id]

示例如下:

# vagrant destroy ubuntu/trusty64

如果我们是使用了默认的虚拟机名"default",那其实我们可以直接输入如下命令销毁VM

# vagrant desotry

运行结果如下:

    default: Are you sure you want to destroy the 'default' VM? [y/N] y
==> default: Forcing shutdown of VM...
==> default: Destroying VM and associated drives...

需要注意的是,我们要慎重使用该命令,该命令会停止VM的运行,并销毁所有在VM中创建的资源

二、以下是 Vagrant 常用命令:

Vagrant 是一款用于构建和管理虚拟化开发环境的工具,它可以让你快速地创建、配置和管理虚拟机。

1. `vagrant init`: 初始化一个新的 Vagrantfile。

2. `vagrant up`: 启动虚拟机,如果没有虚拟机,则会自动下载相应的镜像文件。

3. `vagrant ssh`: 连接到正在运行的虚拟机,并通过 SSH 访问其中的操作系统。

4. `vagrant halt`: 关闭正在运行的虚拟机。

5. `vagrant reload`: 重新加载 Vagrantfile 配置文件并重启正在运行的虚拟机。

6. `vagrant destroy`: 销毁当前正在运行的虚拟机及其所有相关资源。

7. `vagrant status`: 显示当前所有 Vagrant 虚拟机状态。

8. `vagrant box list`: 列出当前可用的 Vagrant box 镜像列表。

9. `vagrant box add [box_name] [url]`:添加一个新的 vagrant box 镜像到本地环境中。

10. `vagrant plugin install [plugin_name]`:安装指定插件,例如:`vagrant plugin install vagrant-vbguest`

11. `vagrant global-status`:查看全局状态,即所有与该电脑有关联的 VM 状态

这些命令只是 Vagant 中非常基础、常用且必须的命令,更多高级用法可以查阅 Vagant 官方文档。

三、vagrant package 打包命令

Vagrant 提供了 `vagrant package` 命令来打包当前正在运行的虚拟机或指定的虚拟机为 box 文件。

默认情况下,`vagrant package` 命令将会创建一个名为 `package.box` 的 box 文件,并且该文件将保存在当前工作目录中。您可以通过使用 `-o` 选项来自定义输出文件的名称和路径。

以下是一些常用的 `vagrant package` 命令示例:

1. 打包当前正在运行的虚拟机并指定输出文件名和路径:

   ```
   vagrant package --output mybox.box
   ```

2. 打包指定的虚拟机并指定输出文件名和路径:

   ```
   vagrant package --base myvm --output mybox.box
   ```

3. 使用 Vagrantfile 中定义的设置来打包虚拟机:

   ```
   vagrant package --vagrantfile Vagrantfile.prod
   ```

4. 在打包前先停止虚拟机:

   ```
   vagrant halt
   vagrant package
   ```

注意:在执行 `vagrant package` 命令之前,请确保已经停止了要打包的虚拟机。否则,可能会出现意料之外的问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lxw1844912514

你的打赏就是对我最大的鼓励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值