Vagrant + Virtualbox构建虚拟机

环境:Window7
一、访问Vagrant官网下载对应版本,傻瓜式安装,安装完成后通过vagrant验证是否安装成功;安装路径中不要存在中文字符。成功的话就如下图:
Vagrant + Virtualbox构建虚拟机_第1张图片

二、 访问VirtualBox官网选择对应系统版本下载安装virtual box,傻瓜式安装;([win10中若出现]安装virtualbox快完成时立即回滚,并提示安装出现严重错误
(1)打开服务
(2)找到Device Install Service和Device Setup Manager,然后启动
(3)再次尝试安装)

三.安装centos7

  1. 找个位置创建个文件夹,用来存放centos7,并进入其中(目录路径不能有中文字符)
  2. 在此目录下打开cmd,运行vagrant init centos/7,此时会在此目录下生成一个Vagrantfile文件,可以在Vagrantfile文件修改一些参数,如:虚拟机名称,内存大小,cpu等等;附通用Vagrantfile文件,如下:
# -*- 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"	#centos/7的镜像名称,与第4步

  # 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 "public_network"		#网络为共享网络

  # 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"
  # end
      config.vm.provider "virtualbox" do |vb|	#配置内存,虚拟机名称,cpus
        vb.memory = "3000"
        vb.name= "huang-centos7"
        vb.cpus= 2
    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
  # Puppet, Chef, Ansible, Salt, and Docker 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.下载centos/7对应得virtualbox.box。这边我提供个百度网盘的下载地址:
centos/7对应得virtualbox.box 提取码:d7y8

4.添加镜像并起名为centos/7 ,运行命令:

vagrant box add centos/7 第3步下载的virtualbox.box文件目录(如:D:\virtualbox.box)

5.查看box列表

vagrant box list  --查看本地的box[这时候可以看到centos/7]

6.启动创建虚拟机,同时打开安装好的box查看启动状态

vagrant up
  1. vagrant常用命令
    (1)vagrant ssh
    进入刚才创建的centos7中
    (2)vagrant status
    查看centos7的状态
    (3)vagrant halt
    停止/关闭centos7
    (4)vagrant destroy
    删除centos7
    (5)vagrant status
    查看当前vagrant创建的虚拟机
    (6)Vagrantfile中也可以写脚本命令,使得centos7更加丰富
    但是要注意,修改了Vagrantfile,要想使正常运行的centos7生效,必须使用vagrant reload
    (7) vagrant halt 优雅关闭
    vagrant up 正常启动
    至此,使用vagrant+virtualbox搭建centos7完成,后面可以修改Vagrantfile对虚拟机进行相应配置

若想通过Xshell连接centos7
1.使用centos7的默认账号连接
在centos文件夹下执行vagrant ssh-config
关注:Hostname Port IdentityFile
IP:127.0.0.1
port:2222
用户名:vagrant
密码:vagrant
文件:Identityfile指向的文件private-key
2.使用root账户登录
vagrant ssh 进入到虚拟机中
sudo -i
vi /etc/ssh/sshd_config
修改PasswordAuthentication yes
passwd修改密码,比如abc123
systemctl restart sshd
使用账号root,密码abc123进行登录

最后则能通过xshell连接了

如有转载请请务必保留此出处:https://blog.csdn.net/hometing218/article/details/103814749

你可能感兴趣的:(Centos7,centos)