








通过 Vagrant 创建虚机需要先导入镜像文件,默认存储位置是在用户目录下的 .vagrant.d 目录下,对于 Windows 系统来说,就是 C:\Users\用户名.vagrant.d。如果后续会用到较多镜像,或者C 盘空间比较紧缺,则可通过创建环境变量 VAGRANT_HOME 来设置该目录。

变量名: VAGRANT_HOME
变量值:(根据你的目录写,文件夹.vagrant.d可以是别的名字)

命令:vagrant box add osName --name
例子:vagrant box add centos/7 --name centos7
如果box已经下载到本地(推荐,官方仓库下载慢):
命令:vagrant box add filePath --name
例子:vagrant box add C:\Users\LENG\Downloads\CentOS-7-x86_64-Vagrant-2004_01.VirtualBox.box --name centos7

这时候步骤3.1配置的文件夹下就会增加一个box

命令:vagrant box list



# -*- 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 = "centos7"# Disable automatic box update checking. If you disable this, then# boxes will only be checked for updates when the user runs# `vagrant box outdated`. This is not recommended.# config.vm.box_check_update = false# Create a forwarded port mapping which allows access to a specific port# within the machine from a port on the host machine. In the example below,# accessing "localhost:8080" will access port 80 on the guest machine.# NOTE: This will enable public access to the opened port# config.vm.network "forwarded_port", guest: 80, host: 8080# Create a forwarded port mapping which allows access to a specific port# within the machine from a port on the host machine and only allow access# via 127.0.0.1 to disable public access# config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"# Create a private network, which allows host-only access to the machine# using a specific IP.config.vm.network "private_network", ip: "192.168.33.10"# Create a public network, which generally matched to bridged network.# Bridged networks make the machine appear as another physical device on# your network.# config.vm.network "public_network", ip: "192.168.56.10"# Share an additional folder to the guest VM. The first argument is# the path on the host to the actual folder. The second argument is# the path on the guest to mount the folder. And the optional third# argument is a set of non-required options.# config.vm.synced_folder "../data", "/vagrant_data"# Provider-specific configuration so you can fine-tune various# backing providers for Vagrant. These expose provider-specific options.# Example for VirtualBox:## config.vm.provider "virtualbox" do |vb|# # Display the VirtualBox GUI when booting the machine# vb.gui = true## # Customize the amount of memory on the VM:# vb.memory = "1024"# endconfig.vm.provider "virtualbox" do |vb|# 名称vb.name = "centos7-1"# CPU大小vb.cpus = 2# 内存大小vb.memory = "2048"end# View the documentation for the provider you are using for more# information on available options.# Enable provisioning with a shell script. Additional provisioners such as# Ansible, Chef, Docker, Puppet and Salt are also available. Please see the# documentation for more information about their specific syntax and use.# config.vm.provision "shell", inline: <<-SHELL# apt-get update# apt-get install -y apache2# SHELL
end
命令:vagrant up
经过上述方法解决后,执行vagrant up,则可以正常启动

命令:vagrant ssh

命令:su root
密码默认是vagrant

将 PasswordAuthentication no 改成 yes

命令:systemctl restart sshd
| 命令 | 作用 |
|---|---|
| vagrant -v/version | 查看Vagrant版本 |
| vagrant global-status | 查看 Vagrant 当前所有已安装系统 |
| vagrant box add filePath --boxName | 将镜像(Box)添加到本地仓库 |
| vagrant box list | 查看所有已添加box |
| vagrant box remove boxName | 移除已添加box |
| vagrant init centos7 | 初始化虚拟机系统 |
| vagrant validate | 编辑Vagrantfile之后,不确定编写是否正确,使用该命令进行验证 |
| vagrant up | 启动虚拟机系统 |
| vagrant ssh | 连接虚拟机系统 |
| vagrant reload | 重新启动虚拟机系统 |
| vagrant status | 查看虚拟机系统状态 |
| exit | 退出ssh连接的虚拟机系统 |
| vagrant halt | 关闭虚拟机系统 |
| vagrant package | 打包虚拟机系统 |
| vagrant destory | 删除虚拟机系统 |
上一篇:驱动开发2