Vagrant使用方法

1.虚拟机操作

1.1 查看vagrant中有多少个虚拟机

E:>vagrant box list
centos7 (virtualbox, 0)

这个命令的返回结果表示我的vagrant 中有一个虚拟机,虚拟机的名字是centos7

1.2 新增一个vagrant虚拟机

E:>md centos7test

E:>cd centos7test

E:centos7test>vagrant box add centos7test E:vagrantvagrant-centos-7.2.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'centos7test' (v0) for provider:
    box: Unpacking necessary files from: file:///E:/vagrant/vagrant-centos-7.2.box
    box: Progress: 100% (Rate: 117M/s, Estimated time remaining: --:--:--)
==> box: Successfully added box 'centos7test' (v0) for 'virtualbox'!

E:centos7test>vagrant box list
centos7     (virtualbox, 0)
centos7test (virtualbox, 0)

参数解析:

  • add 表示新增虚拟机
  • centos7test 这个是虚拟增虚拟机的名字
  • E:vagrantvagrant-centos-7.2.box 这个是镜像文件所在位置

1.3 创建一个vagrant配置文件

E:centos7test>vagrant init centos7test
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.

1.4 打开虚拟机

先cd 到虚拟机所在目录。然后使用vagrant up命令启动虚拟机

E:centos7test>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos7test'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: centos7test_default_1538399987116_82773
==> 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 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.30
    default: VirtualBox Version: 5.2
==> default: Mounting shared folders...
    default: /vagrant => E:/centos7test

1.5 关闭虚拟机

使用vagrant halt 命令关闭虚拟机

E:centos7test>vagrant halt
==> default: Attempting graceful shutdown of VM...

1.6 重载虚拟机配置文件

使用vagrant reload 命令重载虚拟机配置文件

E:centos7test>vagrant reload

2.虚拟机配置

虚拟机配置都放在虚拟机所在目录下,文件名是Vagrantfile,没有扩展名。

2.1 修改端口映射

先打开Vagrantfile, 发现都是# ,井号代表注释。在 Vagrant.configure(“2”) do |config| 下面新增一行()

config.vm.network "forwarded_port", guest: 80, host: 8080

这句话代表 把虚拟机的80端口映射成宿主机的8080端口

2.2 修改虚拟机内存

配置文件里有写好的配置方法,直接修改这个就行了,但是要注意把注释打开。

  config.vm.provider "virtualbox" do |vb|  
    # Customize the amount of memory on the VM:
    vb.memory = "1024"
  end

2.3 修改网卡设置

先打开Vagrantfile。在 Vagrant.configure(“2”) do |config| 下面新增一行()

  config.vm.network "private_network", ip: "192.168.56.10"