Wednesday, January 14, 2015

How to create a Vagrant Box for VirtualBox

I had to create an development environment and the target was an old Centos version (6.2).

But following steps, in my humble opinion, are applicable also to early versions of Centos and maybe, with the right precautions, even in different distributions:

I have downloaded and installed the minimal iso (CentOS-6.2-x86_64-minimal.iso) and created a new virtualbox machine.

Choose to have enough memory
Choose an hard drive large enough

Disable unnecessary devices like usb, audio.

Setup port ssh port forwarding

Configure default CD/DVD drive to mount CentOS-6.2-x86_64-minimal.iso file

Now you can boot the virtual machine and follow the step by step installation.

Leave everything as default and set root password to "vagrant".

When the virtual machine is up and running login as root/vagrant.

Now you need to configure eth0 network card:

# vi /etc/sysconfig/network-scripts/ifcfg-eth0

and add/change following lines:


now reboot and install sudo:

# yum install sudo

then you need to add default vagrant user:

# vi /etc/sudoers.d/vagrant


# groupadd -g 900 vagrant
# adduser -m -g 900 -u 900 vagrant -p vagrant

Set the password "vagrant"

# passwd vagrant 

now you need to install openssh-server and configure ssh authorized_keys in order to enable vagrant remote configuration and provisioning:

# sudo yum install openssh-server
# sudo yum install wget

# mkdir -p /home/vagrant/.ssh 
# chmod 0700 /home/vagrant/.ssh 
# wget --no-check-certificate -O /home/vagrant/.ssh/authorized_keys 
# chmod 0600 /home/vagrant/.ssh/authorized_keys 
# chown -R vagrant /home/vagrant/.ssh

Now we need to adjust few little settings in order to let everything work:

# vi /etc/ssh/sshd_config

and uncomment line with:

AuthorizedKeysFile      .ssh/authorized_keys 

Edit sudoers configuration

# vi /etc/sudoers

now we need to comment line with:

# Defaults requiretty

We have almost finished, we need to install Virtual Box Guest Additions. To do this we need to install few required packages:

# yum install gcc build-essential linux-headers-server kernel-devel perl acpid

Now you can mount Guest Additions CD from Virtual Box menus:

VirtualBox Machine -> Devices -> Insert Guest Additions CD Image... (Host + D)

# sudo mkdir /media/cdrom
# sudo mount /dev/cdrom /media/cdrom/
# cd /media/cdrom/
# ./ 

In my case, during VBoxLinuxAdditions execution I had the following error:

Building the VirtualBox Guest Additions kernel modules
The headers for the current running kernel were not found. If the following
module compilation fails then this could be the reason.
The missing package can be probably installed with
yum install kernel-devel-2.6.32-220.el6.x86_64

Building the main Guest Additions module                   [FAILED]
(Look at /var/log/vboxadd-install.log to find out what went wrong)

You can avoid the problem installing manually the required package:

# cd ~/
# wget

# yum install kernel-devel-2.6.32-220.el6.x86_64
# cd /media/cdrom/ 
# ./ 

Verifying archive integrity... All good.
Uncompressing VirtualBox 4.3.20 Guest Additions for Linux............
VirtualBox Guest Additions installer
Removing installed version 4.3.20 of VirtualBox Guest Additions...
Copying additional installer modules ...
Installing additional modules ...
Removing existing VirtualBox non-DKMS kernel modules       [  OK  ]
Building the VirtualBox Guest Additions kernel modules
Building the main Guest Additions module                   [  OK  ]
Building the shared folder support module                  [  OK  ]
Building the OpenGL support module                         [  OK  ]
Doing non-kernel setup of the Guest Additions              [  OK  ]
Starting the VirtualBox Guest Additions                    [  OK  ]
Installing the Window System drivers

Could not find the X.Org or XFree86 Window System, skipping.

Now your virtual box machine is ready to be used as vagrant box. You can shutdown it and package with the following command:

$ vagrant package --base vagrant-centos-6.2-x64