If you work a lot with VMs in you local development environment and wish you could easily reproduce your individual customized VMs, this tutorial is exactly the one you are looking for. I will show you how to create VMs costumized for your needs with just one simple command “vagrant up”.
Let’s get started
My development environment looks like this:
- Mac OS X El Capitan (10.11.2)
- VirtualBox 5.0.10
- Vagrant 1.8.1
We will create our own base box for a CentOS 7 (latest Version 7.2 to be precise) Guest OS in VirtualBox.
Step 1: Creating the Virtual Maschine
Hint: I already downloaded the latest CentOS 7 Minimal ISO from here.
Create a new VM in VirtualBox with the following settings:
- Name: vagrant-centos7-64
- Type: Linux
- Version: Red Hat (64-bit)
- Memory: 1024
- Create hard disk:
- Size: 40 GB
- Type: VDI (VirtualBox Disk Image)
- Storage on physical hard disk: Dynamically allocated
Change VM’s settings:
- Disable Audio
- Disable USB
- Set Network Adapter 1 to NAT
Add this port forwarding rule (leave the IP fields empty):
- Name: SSH
- Protocol: TCP
- Host Port: 2222
- Guest Port: 22
Load the CentOS Minimal ISO
- Power on the VM
Step 2: Installing and configuring your VM
When installing the Guest OS pay attention to some important settings:
- Set the Hostname: vagrant-centos7-64
- Enable NIC on boot
- Set root password to “vagrant”
I think it is since release 7 of CentOS, Red Hat and Oracle Linux, that the Network interfaces are not enabled on boot. If you forget to set this option you (and Vagrant) are not able to connect to the VM via SSH. This is required for provisioning the VM.
Setting the root password to “vagrant” is not required to make Vagrant work. It is just best practice. On all the official base boxes from Hashicorp the password for root is set to “vagrant”.
When the installation is finished the VM will be rebooted.
After the reboot we will be able to login to the VM via SSH
ssh firstname.lastname@example.org -p 2222
There are some tasks to prepare the box to be provisioned by Vagrant.
- First of all, update the maschine.
yum update -ywill get all the patched files and install them for you
- Add user “vagrant”:
adduser vagrantand set the password to “vagrant” as well
- Add Vagrant’s insecure private key:
$ mkdir /home/vagrant/.ssh $ curl 'https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/vagrant.pub' >> /home/vagrant/.ssh/authorized_keys $ chown -R vagrant /home/vagrant/.ssh $ chmod 0700 /home/vagrant/.ssh $ chmod 0600 /home/vagrant/.ssh/authorized_keys
- Give sudo rights to the vagrant user
$ echo "vagrant ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/vagrant
- Install VirtualBox Guest Additions
- Insert Guest Additions CD by choosing “Devices -> Insert Guest Additions CD Image …”
#Install dependencies yum groupinstall "Development Tools" yum install kernel-devel #Mount the Virtualbox Additions CD ISO mount /dev/cdrom /mnt/ cd /mnt ./VBoxLinuxAdditions.run