A base box typically consists of only a bare minimum set of software for Vagrant to function. As an example, a Linux box may contain only the following:html
In addition to this, each provider may require additional software. For example, if you are making a base box for VirtualBox, you will want to include the VirtualBox guest additions so that shared folders work properly. But if you are making an AWS base box, this is not required.git
Creating a base box is actually provider-specific. This means that depending on if you are using VirtualBox, VMware, AWS, etc. the process for creating a base box is different.github
Provider-specific guides for creating base boxes are linked below:docker
本文以VirtualBox爲例centos
本文用VirtualBox安裝CentOS Minimal7,因此選擇Linux 64位類型安全
In terms of system resources, keep the machine minimal — number of CPUs and RAM size can later easily be changed in each user's Vagrantfile. I will use one CPU and 1024MB of RAM (less RAM will not allow you to use the graphical installer).less
Disable any non-necessary hardware in a base box such as audio and USB controllers. These are generally unnecessary for Vagrant usage and, again, can be easily added via the Vagrantfile in most cases.dom
With the new machine selected, click Settings in the toolbar and make the following adjustments:ssh
Do not forget to enable the network interface and configure it to use DHCP to obtain an IP address.curl
# vi /etc/sysconfig/network-scripts/ifcfg-enp0s3 ONBOOT=yes BOOTPROTO=dhcp
By default, Vagrant expects a "vagrant" user to SSH into the machine as. This user should be setup with theinsecure keypairthat Vagrant uses as a default to attempt to SSH.
添加vagrant用戶
useradd vagrant
設置vagrant密碼爲vagrant
passwd vagrant
配置不安全的keypair
mkdir -p /home/vagrant/.ssh curl https://raw.githubusercontent.com/hashicorp/vagrant/master/keys/vagrant.pub --output /home/vagrant/.ssh/authorized_keys chmod 0700 /home/vagrant/.ssh chmod 0600/home/vagrant/.ssh/authorized_keys chown -R vagrant:vagrant /home/vagrant/.ssh
When Vagrant boots a box and detects the insecure keypair, it will automatically replace it with a randomly generated keypair for additional security while the box is running.
這對祕鑰是Vagrant默認提供的,你能夠換爲本身的。你能夠經過以下命令,查看私鑰所在位置:
vagrant ssh-config
Vagrant does not actually use or expect any root password. However, having a generally well known root password makes it easier for the general public to modify the machine if needed.
Publicly available base boxes usually use a root password of "vagrant" to keep things easy.
This is important!. Many aspects of Vagrant expect the default SSH user to have passwordless sudo configured. This lets Vagrant configure networks, mount synced folders, install software, and more.
Using the visudo command, add the line following line at the and of the sudoers file:
vagrant ALL=(ALL) NOPASSWD: ALL
Additionally, Vagrant does not use a pty or tty by default when connected via SSH. You will need to make sure there is no line that has Defaults requiretty
in it. Remove that if it exists.
In order to keep SSH speedy even when your machine or the Vagrant machine is not connected to the internet, set the UseDNS configuration to no in the SSH server configuration.
To accomplish this, login as root, open /etc/ssh/sshd_config
, find the line which reads #UseDNS yes
and change it to UseDNS no
.
Restart the SSH daemon with systemctl restart sshd.service
after that.
VirtualBox Guest Additions must be installed so that things such as shared folders can function. Installing guest additions also usually improves performance since the guest OS can make some optimizations by knowing it is running within VirtualBox.
Install the packages needed for compiling the VirtualBox guest additions which Vagrant needs to configure shared folders:
yum install bzip2 gcc kernel-devel-`uname-r` perl
Insert Guest Additions CD image from VirtualBox' menu which enables you to mount the filesystem and install the guest additions:
經過【Devices】【Insert Guest Additions CD image...】
mount /dev/cdrom /media /media/VBoxLinuxAdditions.run umount /media
還可經過Vagrant插件vagrant-vbguest
You can now reduce the VMs file size by running the following commands. They fix fragmentation issues and allow the machine to be compressed more efficiently.
dd if=/dev/zero of=/EMPTY bs=1M rm -f /EMPTY
Congratulations — your virtual machine is now ready to be packaged by Vagrant after powering it off with the poweroff
command.
而後經過以下命令導出爲box
vagrant package --base centos7-minimal
centos7-minimal
is the machine's name as you have set it in VirtualBox' GUI.
Packaging needs some time, but soon you will see that a file called package.box has been created in this directory.
You can now import this base box into Vagrant with the command
agrant box add centos7-minimal package.box
After that, you can init a Vagrant box with vagrant init centos7-minimal
in any directory and use it like any other Vagrant box.