Vagrant基本命令详解

Vagrant基本命令详解

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

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中创建的资源

 

作者:蓝色的咖啡

链接:https://www.jianshu.com/p/b7d599403aa6

来源:简书

简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/112341.html原文链接:https://javaforall.net

(0)
上一篇 2021年11月9日 下午3:00
下一篇 2021年11月9日 下午4:00


相关推荐

  • 顺序图详解

    顺序图详解顺序图一 顺序图概要 顺序图用来表示用例中的行为顺序 当执行一个用例行为时 顺序图中的每条消息对应了一个类操作或状态机中引起转换的事件 顺序图展示对象之间的交互 这些交互是指在场景或用例的事件流中发生的 顺序图属于动态建模 顺序图的重点在消息序列上 也就是说 描述消息是如何在对象间发送和接收的 表示了对象之间传送消息的时间顺序 浏览顺序图的方法是 从上到下

    2026年3月17日
    1
  • ASP.NET访问Excel 失败的解决方法(错误号:80070005,8000401a)

    ASP.NET访问Excel 失败的解决方法(错误号:80070005,8000401a)用asp.net把值写入Excel在本地测试通过,然后提交服务器后老是写入不成功 并提示错误:RetrievingtheCOMclassfactoryforcomponentwithCLSID{00024500-0000-0000-C000-000000000046}failedduetothefollowingerror:80070005.在网络上查找了许多资料,

    2022年7月25日
    18
  • nest expression &amp; Pyparsing[通俗易懂]

    nest expression &amp; Pyparsing

    2022年1月24日
    51
  • cursor开发刷题小程序教程

    cursor开发刷题小程序教程

    2026年3月16日
    2
  • 服务器系统tcpip.sys,tcpip.sys

    服务器系统tcpip.sys,tcpip.systcpip sys 文件说明 tcpip sys 文件是系统库中一个十分重要的文件 如果缺少了这款 tcpip sys 文件 您的设备或者电脑将会出现蓝屏或者无法开机的情况 马上下载 tcpip sys 这款文件修复吧 tcpip sys 官方版是系统中自带的文件 Tcpip sys 是微软使用的 TCP 并发连接数限制文件 默认值为 10 非常重要 很多朋友可能遇到 tcpip sys 导致的蓝屏 系统黑屏无法启动的问题

    2025年9月25日
    7
  • 物联网平台架构设计

    物联网平台架构设计现在网上讨论的有关物联网的帖子非常之多,但大部分都是介绍理论或者有关硬件,通讯相关的问题,比如物联网模块,物联网通讯协议MQTT、XMPP、NB_IOT等,个人认为这些只是物联网中一部分,而涉及到物联网的设备如何管理,用户如何管理,数据包如何解析,大数据如何展示等也是物联网模块中非常重要的部分,所以作者就根据自身工作中总结出来的建构在云端的物联网平台基本架构分享给大家,并基于此架构如何一步一步来开发

    2022年5月9日
    39

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号