基于Windows 7 的Vagrant box制作

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/mayancheng7/article/details/89706536

vagrant_1.9.5网盘链接:链接:https://pan.baidu.com/s/1_fUv64V6E5bAFaUFE0PbRw
提取码:6fwt

VirtualBox-5.1.26网盘链接:链接:https://pan.baidu.com/s/17f1iK5rPe1tiKQKhAiUvOg
提取码:mizv

  1. 制作box的准备以及约定。
user:root&yx
passwd:vagrant

# 使用xshell等ssh工具登录
# 配置源
# 安装配置所需要的软件
#下方是最主要的操作
yx用户下新建vagrant的公钥
# 1. 下载官方公钥 
wget https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/vagrant.pub && echo "1. vagrant.pub下载完成" 
# 1.1 可以直接新建authorized_keys文件,内容为下列公钥
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ== vagrant insecure public key

# 2. 重命名authorized_keys,移动到.ssh文件下 
mv vagrant.pub .ssh/authorized_keys && echo "2. 重命名为authorized_keys" 
# 3. 修改authorized_keys文件权限 除了属主vagrant以外,group和其他用户都不可写 
chmod go-w /home/vagrant/.ssh/authorized_keys && echo "3. 修改authorized_keys权限完成" cat .ssh/authorized_keys

# 安装完成关机前做如下清理操作

# 删除网卡mac信息
rm -f /etc/udev/rules.d/70-persistent-net.rules

# 删除临时文件
yum clean all &&  apt-get clean
rm -rf /tmp/*
rm -f /var/log/wtmp /var/log/btmp

# 清除命令历史记录
history -c
> .bash_history
sudo su - vagrant
history -c
> .bash_history

网卡1必须设置为NAT,然后再端口转发加上图2.网卡2建议为桥接。

 

声音,usb设备禁用。此处截图重点关注声音和USB设备。网卡请忽略

找到虚拟机的安装路径,右击鼠标→在此处打开命令窗口
vagrant package --base centos --output Centos-20190417.box

centos //VirtualBox虚拟机中的要做box的主机名,类似

Centos-20190417.box //打包之后的名字,其他格式都是固定的。

一般四五分钟就好。devenv //本次新加box的自定义名称,随意
vagrant box add devenv Centos-20190417.box

vagrant init devenv
# 用以下的配置文件替换新生成的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://atlas.hashicorp.com/search.
  config.vm.box = "devenv"

  config.ssh.shell = "bash -c 'BASH_ENV=/etc/profile exec bash'"
  config.ssh.username = 'yx'
  config.ssh.password = 'vagrant'
  # 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
  config.vm.hostname = 'php1'
  # 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.
  # config.vm.network "forwarded_port", guest: 3357, host: 3358

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  # config.vm.network "private_network", type: "dhcp"
  config.vm.network "private_network", ip: "172.28.128.3"

  # 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.0.46",netmask:"255.255.0.0"
  # config.vm.network "public_network", auto_config: false

  # manual ip
  # config.vm.provision "shell",
  #     run: "always",
  #     inline: "ifconfig eth1 192.168.9.22 netmask 255.255.252.0 up"

  # config.vm.provision "shell",
  #     run: "always",
  #     inline: "route add default gw 192.168.11.254"

  # 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"
  # config.vm.synced_folder ".", "/home/yx/sites"
  # config.vm.synced_folder ".", "/home/yx/sites",type: "rsync", rsync__auto: true, rsync__args: ["--verbose", "--rsync-path='sudo rsync'", "--archive", "--delete", "-z"]
  # config.winnfsd.uid = 1
  # config.winnfsd.gid = 1
  # config.winnfsd.logging = "off"
  config.vm.synced_folder "D:/work/sites", "/home/yx/sites"# ,nfs: true
  config.vm.synced_folder "D:/work/server/sites-conf","/home/yx/server/nginx/sites-conf"
  config.vm.synced_folder "D:/work/server/ssl","/home/yx/server/ssl"
  # 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 = "2048"
  # end
  config.vm.provider "virtualbox" do |v|
    v.memory = 2048
    v.cpus = 4
  end
  #
  # View the documentation for the provider you are using for more
  # information on available options.

  # Define a Vagrant Push strategy for pushing to Atlas. Other push strategies
  # such as FTP and Heroku are also available. See the documentation at
  # https://docs.vagrantup.com/v2/push/atlas.html for more information.
  # config.push.define "atlas" do |push|
  #   push.app = "YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME"
  # end

  # 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
  #   sudo apt-get update
  #   sudo apt-get install -y apache2
  # SHELL
end

vagrant box list

vagrant up

vagrant status

vagrant halt

vagrant box remove

猜你喜欢

转载自blog.csdn.net/mayancheng7/article/details/89706536