使用vagrant快速创建linux虚拟机

注意:
virtualbox与vagrant版本需要匹配,vagrant_2.2.4不支持virtualbox 6.0以上的版本,否则启动的时候会报错,提示virtualbox版本过高。
软件下载链接
链接:https://pan.baidu.com/s/13WVUc-Cjxp2FyIynrGe5hQ
提取码:pw6b

  • 安装virtualbox
    版本:VirtualBox-6.0.0-127566
  • 安装vagrant
    版本:vagrant_2.2.4
    安装完成需要重启电脑
  • 初始化虚拟机
$ vagrant init centos/7
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.

执行完上面的命令后,会在当前用户目录下生成Vagrantfile文件。例如:"C:\Users\jm006151\Vagrantfile"

  • 启动虚拟机
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'centos/7' could not be found. Attempting to find and install...
    default: Box Provider: virtualbox
    default: Box Version: >= 0
==> default: Loading metadata for box 'centos/7'
    default: URL: https://vagrantcloud.com/centos/7
==> default: Adding box 'centos/7' (v1905.1) for provider: virtualbox
    default: Downloading: https://vagrantcloud.com/centos/boxes/7/versions/1905.1/providers/virtualbox.box
    default: Download redirected to host: cloud.centos.org
    default: Progress: 0% (Rate: 6717/s, Estimated time remaining: 7:33:42)

下载镜像过程比较漫长,也可以采用先用下载工具下载到本地后,然后使用“ vagrant box add ”添加,再“vagrant up”即可

#将下载的镜像添加到virtualBox中
$ vagrant box add centos/7 E:\迅雷下载\CentOS-7-x86_64-Vagrant-1902_01.VirtualBox.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'centos/7' (v0) for provider:
    box: Unpacking necessary files from: file:///E:/%D1%B8%C0%D7%CF%C2%D4%D8/CentOS-7-x86_64-Vagrant-1905_01.VirtualBox.box
    box:
==> box: Successfully added box 'centos/7' (v0) for 'virtualbox'!

#启动
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos/7'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: Administrator_default_1588497928070_24634
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> 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: No guest additions were detected on the base box for this VM! Guest
    default: additions are required for forwarded ports, shared folders, host only
    default: networking, and more. If SSH fails on this machine, please install
    default: the guest additions and repackage the box to continue.
    default:
    default: This is not an error message; everything may continue to work properly,
    default: in which case you may ignore this message.
==> default: Configuring and enabling network interfaces...
==> default: Rsyncing folder: /cygdrive/c/Users/Administrator/ => /vagrant

  • vagrant ssh连接虚拟机
C:\Users\jm006151>vagrant ssh
Last login: Fri Aug 14 05:14:27 2020 from 10.0.2.2
[vagrant@localhost ~]$ cd /
[vagrant@localhost /]$ ls
bin   dev  home  lib64  mnt  proc  run   srv       sys  usr      var
boot  etc  lib   media  opt  root  sbin  swapfile  tmp  vagrant
[vagrant@localhost /]$
  • 问题汇总
    • vagrant up 启动报错如下
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'centos/7' version '1902.01' is up to date...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 22 (guest) => 2221 (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:2221
    default: SSH username: vagrant
    default: SSH auth method: private key
==> default: Machine booted and ready!
[default] GuestAdditions 5.2.22 running --- OK.
==> default: Checking for guest additions in VM...
==> default: Configuring and enabling network interfaces...
==> default: Rsyncing folder: /cygdrive/c/myvagrant/Borg-web/borg/ => /vagrant
C:/HashiCorp/Vagrant/embedded/gems/2.2.5/gems/vagrant-2.2.5/lib/vagrant/util/io.rb:32:in `encode': "\x83\xBC" from Windows-31J to UTF-8 (Encoding::UndefinedConversionError)
        from C:/HashiCorp/Vagrant/embedded/gems/2.2.5/gems/vagrant-2.2.5/lib/vagrant/util/io.rb:32:in `read_until_block'
        from C:/HashiCorp/Vagrant/embedded/gems/2.2.5/gems/vagrant-2.2.5/lib/vagrant/util/subprocess.rb:194:in `block in execute'
        from C:/HashiCorp/Vagrant/embedded/gems/2.2.5/gems/vagrant-2.2.5/lib/vagrant/util/subprocess.rb:192:in `each'
        from C:/HashiCorp/Vagrant/embedded/gems/2.2.5/gems/vagrant-2.2.5/lib/vagrant/util/subprocess.rb:192:in `execute'
        from C:/HashiCorp/Vagrant/embedded/gems/2.2.5/gems/vagrant-2.2.5/lib/vagrant/util/subprocess.rb:22:in `execute'
        from C:/HashiCorp/Vagrant/embedded/gems/2.2.5/gems/vagrant-2.2.5/plugins/synced_folders/rsync/helper.rb:215:in `rsync_single'
        from C:/HashiCorp/Vagrant/embedded/gems/2.2.5/gems/vagrant-2.2.5/plugins/synced_folders/rsync/synced_folder.rb:48:in `block in enable'
        from C:/HashiCorp/Vagrant/embedded/gems/2.2.5/gems/vagrant-2.2.5/plugins/synced_folders/rsync/synced_folder.rb:47:in `each'
        from C:/HashiCorp/Vagrant/embedded/gems/2.2.5/gems/vagrant-2.2.5/plugins/synced_folders/rsync/synced_folder.rb:47:in `enable'
        from C:/HashiCorp/Vagrant/embedded/gems/2.2.5/gems/vagrant-2.2.5/lib/vagrant/action/builtin/synced_folders.rb:93:in `block in call'
        from C:/HashiCorp/Vagrant/embedded/gems/2.2.5/gems/vagrant-2.2.5/lib/vagrant/action/builtin/synced_folders.rb:90:in `each'
        from C:/HashiCorp/Vagrant/embedded/gems/2.2.5/gems/vagrant-2.2.5/lib/vagrant/action/builtin/synced_folders.rb:90:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/2.2.5/gems/vagrant-2.2.5/lib/vagrant/action/warden.rb:50:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/2.2.5/gems/vagrant-2.2.5/lib/vagrant/action/builtin/before_trigger.rb:23:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/2.2.5/gems/vagrant-2.2.5/lib/vagrant/action/warden.rb:50:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/2.2.5/gems/vagrant-2.2.5/lib/vagrant/action/builtin/after_trigger.rb:26:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/2.2.5/gems/vagrant-2.2.5/lib/vagrant/action/warden.rb:50:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/2.2.5/gems/vagrant-2.2.5/lib/vagrant/action/builtin/synced_folder_cleanup.rb:28:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/2.2.5/gems/vagrant-2.2.5/lib/vagrant/action/warden.rb:50:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/2.2.5/gems/vagrant-2.2.5/lib/vagrant/action/builtin/before_trigger.rb:23:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/2.2.5/gems/vagrant-2.2.5/lib/vagrant/action/warden.rb:50:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/2.2.5/gems/vagrant-2.2.5/lib/vagrant/action/builtin/after_trigger.rb:26:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/2.2.5/gems/vagrant-2.2.5/lib/vagrant/action/warden.rb:50:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/2.2.5/gems/vagrant-2.2.5/plugins/synced_folders/nfs/action_cleanup.rb:19:in `call'

需要修改Vagrantfile,添加代码 Encoding.default_external = 'UTF-8'

# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://vagrantcloud.com/search.
  config.vm.box = "centos/7"
  Encoding.default_external = 'UTF-8'
  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs

参考链接:https://www.cnblogs.com/gaoBlog/p/11410685.html