微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

vagrant+virtualbox搭建centos

1.安装vagrant

vagrant下载地址:https://www.vagrantup.com/

选择相应的操作系统和版本下载即可,傻瓜式安装 -> 一直下一步。
命令行输入vagrant -v,显示相应的版本号说明安装成功。

2.安装virtualBox

virtualBox下载地址:https://www.virtualbox.org/

一直认下一步安装就行;

[win10中若出现]安装virtualBox快完成时立即回滚,并提示安装出现严重错误
(1)打开服务
(2)找到Device Install Service和Device Setup Manager,然后启动
(3)再次尝试安装

☆ 这里注意  vagrant和virtualBox之间是有版本关联的,具体请看官网或百度  我这里用的vagrant是 vagrant_2.2.16_x86_64,virtualBox是VirtualBox-6.1.32-149290-Win

3.拉取Box镜像

3.1 创建centos7文件夹,并进入其中[目录全路径不要有中文字符]。

3.2 打开cmd,运行vagrant init centos/7
此时会在当前目录下生成Vagrantfile,同时指定使用的镜像为centos/7。准备好生成镜像的.Box文件(我的是centos-7.4-base.Box)。

3.3 编辑 vagrantfile 

我的配置:虚拟机名字是vm1,ip是11.11.11.111,内存30G,

 

 

 

# -*- 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"

  # 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"
  config.vm.network "private_network", ip: "11.11.11.111"

  # 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|
        vb.memory = "30720"
        vb.name= "vm1"
        vb.cpus= 2
    end
  # 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"
  # 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

3.4 将virtualBox.Box文件添加到vagrant管理的镜像中

(1)添加镜像并起名叫centos/7(这里执行命令的路径要和上面3.2的vagrant init centos/7路径一样):vagrant Box add centos/7 E:\vmBox\centos-7.4-base.Box

 


(2)vagrant Box list 查看本地的Box[这时候可以看到centos/7]

4.启动虚拟机

5.远程连接虚拟机

ip:11.11.11.111

port:22

name:vagrant

password:vagrant

初次连接可能需要先配置私钥,先使用 vagrant ssh-config 命令可以看到 SSH 的配置:

 

$ vagrant ssh-config
Host default
  HostName 127.0.0.1
  User vagrant
  Port 22222
  UserKNownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile E:/VirtualBox/.vagrant.d/Boxes/davy-VAGRANTSLASH-centos-7-base/0/virtualBox/vagrant_private_key
  IdentitiesOnly yes
  LogLevel FATAL

  

 完成。。。

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。