主页 > 知识库 > vagrant+virtualBox构建虚拟机的方法

vagrant+virtualBox构建虚拟机的方法

热门标签:南充电销外呼系统 贵阳网络外呼系统软件 地图标注小程序 南昌外呼系统定制 株洲电销 陕西办理400电话 信阳电销外呼系统怎么样 宿州外呼系统公司 海外工厂地图标注

一、介绍

Vagrant是一个构建和管理虚拟机(VirtualBox)的工具。

我的环境:Mac + Vagrant + VirtualBox

网盘下载地址:链接: https://pan.baidu.com/s/1LKacXPQcgh3MjgWiIZ0zhQ 密码: n998

二、安装

VirtualBox: https://www.virtualbox.org/wiki/Downloads
Vagrant: http://downloads.vagrantup.co

三、在线创建虚拟机

1.在官网找到自己想要安装的操作系统:https://app.vagrantup.com/boxes/search

2.点进去,执行命令创建并启动虚拟机:https://app.vagrantup.com/centos/boxes/7

注意:需要单独创建一个目录,在该目录下执行vagrant命令,一个目录对应一个虚拟机。vagrant init会创建一个配置文件Vagrantfile

三、离线创建虚拟机

1.添加

vagrant box add centOs7 /Users/xianbin.yang/Documents/vagrant/centos-7.0-x86_64.box

➜ vagrant vagrant box add centOs7 /Users/xianbin.yang/Documents/vagrant/centos-7.0-x86_64.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'centOs7' (v0) for provider:
  box: Unpacking necessary files from: file:///Users/xianbin.yang/Documents/vagrant/centos-7.0-x86_64.box
==> box: Successfully added box 'centOs7' (v0) for 'virtualbox'!

2.初始化

vagrant init centOs7

➜ vagrant vagrant init centOs7
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.

3.启动

vagrant up

➜ vagrant vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centOs7'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: vagrant_default_1612583375721_1893
==> 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: 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.28
  default: VirtualBox Version: 6.1
==> default: Mounting shared folders...
  default: /vagrant => /Users/xianbin.yang/Documents/vagrant

四、常用命令

命令 作用
vagrant box add 添加box的操作
vagrant init 初始化box的操作,会生成vagrant的配置文件Vagrantfile
vagrant up 启动本地环境
vagrant ssh 通过 ssh 登录本地环境所在虚拟机
vagrant halt 关闭本地环境
vagrant suspend 暂停本地环境
vagrant resume 恢复本地环境
vagrant reload 修改了 Vagrantfile 后,使之生效(相当于先 halt,再 up)
vagrant destroy 彻底移除本地环境
vagrant box list 显示当前已经添加的box列表
vagrant box remove 删除相应的box
vagrant status 获取当前虚拟机的状态


五、Vagrantfile常用配置

1.配置固定ip、内存、cpu

# -*- mode: ruby -*-
# vi: set ft=ruby :
 
Vagrant.configure("2") do |config|
 config.vm.box = "centOs7"
 config.vm.network "private_network", ip: "192.168.33.10"
 config.vm.provider "virtualbox" do |vb|
   vb.cpus = 4
   vb.memory = "8192"
 end
end

注意:配置后需要vagrant reload

2.配置root用户和ssh免密

vagrant用户登录:vagrant ssh

设置root密码:sudo passwd root

配置ssh免密:ssh-copy-id root@192.168.33.10

ssh登录:ssh root@192.168.33.10

3.关闭防火墙

检查防火墙状态:systemctl status firewalld
关闭防火墙:systemctl stop firewalld
设置开机禁用防火墙:systemctl disable firewalld.service

参考链接:

https://learn.hashicorp.com/tutorials/vagrant/getting-started-index?in=vagrant/getting-started

到此这篇关于vagrant+virtualBox构建虚拟机的文章就介绍到这了,更多相关virtualBox构建虚拟机内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

标签:郑州 开封 玉林 晋城 汕头 石嘴山 三明 拉萨

巨人网络通讯声明:本文标题《vagrant+virtualBox构建虚拟机的方法》,本文关键词  vagrant+virtualBox,构建,虚拟机,;如发现本文内容存在版权问题,烦请提供相关信息告之我们,我们将及时沟通与处理。本站内容系统采集于网络,涉及言论、版权与本站无关。
  • 相关文章
  • 下面列出与本文章《vagrant+virtualBox构建虚拟机的方法》相关的同类信息!
  • 本页收集关于vagrant+virtualBox构建虚拟机的方法的相关信息资讯供网民参考!
  • 推荐文章

    上一篇:VMware虚拟机桥接方式实现上网互通的方法

    下一篇:idea集合docker实现镜像打包一键部署