如何使用Vagrant创建一台虚拟机

版本说明

Virtual Box版本:5.2.10
Vagrant版本:2.0.4
电脑系统:windows10 64位
虚拟机:ubuntu 14.0

寻找Box

官网有提供一些Box的下载可以使用,网址是:
https://app.vagrantup.com/boxes/search
截图如下所示,其中红色方法内的就是我们要获取到的信息:
这里写图片描述

初始化

  1. 首先需要创建一个目录用于存放Vagrantfile文件以及Vagrant在工作中的数据。

    mkdir vagrant-workspace/
    cd vagrant-workspace
    mkdir ubuntu1404
    cd ubuntu1404
  2. 开始初始化Vagrant工程

    vagrant init ubuntu/trusty64

    这里的ubuntu/trusty64就是在官网的提供的页面获取的Box的名称。Vagrant发现box的名字的格式为“用户名/box名”,则会使用“https://atlas.hashicorp.com/用户名/box名”来下载该box。对于非官网提供的box,可以通过以下命令创建:

    vagrant init {Box的名称} {Box的url}

    回车之后,命令行窗口输出信息,它告诉我们在文件夹下创建一个文件叫Vagrantfile,可以准备进行运行vagrant up命令了。

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

    Vagrantfile文件里面的注释忽略的话,可以看到有效的内容如下:

    Vagrant.configure("2") do |config|
      config.vm.box = "ubuntu/trusty64"
    end
    d
  3. 启动虚拟机,如果是第一次运行,则会先去下载box文件,如果先前下载过了则跳过

    vagrant up

    经过漫长的等待之后,终于下载完了,输出信息

    Bringing machine 'default' up with 'virtualbox' provider...
    ==> default: Box 'ubuntu/trusty64' could not be found. Attempting to find and install...
        default: Box Provider: virtualbox
        default: Box Version: >= 0
    ==> default: Loading metadata for box 'ubuntu/trusty64'
        default: URL: https://vagrantcloud.com/ubuntu/trusty64
    ==> default: Adding box 'ubuntu/trusty64' (v20180419.0.0) for provider: virtualbox
        default: Downloading: https://vagrantcloud.com/ubuntu/boxes/trusty64/versions/20180419.0.0/providers/virtualbox.box
        default: Download redirected to host: cloud-images.ubuntu.com
        default: 
    ==> default: Successfully added box 'ubuntu/trusty64' (v20180419.0.0) for 'virtualbox'!
    ==> 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: ubuntu1404_default_1525060576422_98609
    ==> 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: Warning: Connection aborted. Retrying...
        default: Warning: Remote connection disconnect. Retrying...
        default: Warning: Connection aborted. Retrying...
        default: Warning: Connection reset. Retrying...
        default: Warning: Connection aborted. Retrying...
        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 => G:/vagrant-workspace/ubuntu1404
  4. 登录虚拟机

    vagrant ssh

关闭虚拟机

使用命令vagrant halt关闭虚拟机

扫描二维码关注公众号,回复: 1057701 查看本文章

删除虚拟机

使用命令vagrant destroy删除虚拟机。请注意,vagrant destroy只会删除虚拟机本身,也即你在Virtualbox将看不到该虚拟机,但是不会删除该虚拟机所使用的box。

猜你喜欢

转载自blog.csdn.net/wang465745776/article/details/80148636
今日推荐