Using Vagrant to create virtual machines with Virtual Box


I'm a Linux System Admin & I play with lots of virtual machines to try & test various software & services. I use Virtual Box for creating virtual machine. Everything works fine except the part of installing same virtual machine again & again which takes a lot of time installing & configuring it.

Recently I came across a tool called Vagrant which can automate that process or at least can make it easy for us to make a base image & then create virtual machines from that image without any worry.

Now the real work:-

Make sure you've Virtual Box installed on your machine, if not already done, download from https://www.virtualbox.org/wiki/Downloads & install it. Its available for almost all popular platforms.

Download Vagrant from http://downloads.vagrantup.com/ & install it.

Binary packages available for MAC, Windows, debian & RPM based systems.

I used Centos 6 heavily so I installed Centos 6. Location specific mirrors for Centos are available at http://isoredirect.centos.org/centos/6/isos/x86_64/ . Make sure you have Centos6 CD/DVD downloaded.

Now create a virtual machine for you work in Virtual Box. Those who are new to it can check help here http://www.virtualbox.org/manual/ch01.html

Create a new VM by providing information like CPUs & RAM & HDD etc.

Now when machine is up & running. Enable the default network interface (eth0) with DHCP.

vi /etc/sysconfig/network-scripts/ifcfg-eth0
set ONBOOT='yes' and BOOTPROTO=dhcp

Start the network service:-

service network restart

make sure your system is up to date:-

yum update

make sure you've kernel packages to allow installation of Guest Additions:-

yum install gcc make kernel-devel kernel-headers perl

Now shutdown the VM & run following command in your host system:-

Go to the virtual box installation directory.
I've Windows XP as host:
So I run the command:-
"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe modifyvm "vagrant-centos6" --natpf1 "guestssh,tcp,,2222,,22"

where "vagrant-centos6" is the name of virtual machine, change it to your VM name.

This will open SSH port on locahost to the new VM. This can also be done through Virtual Box GUI.

Start the VM & install the Guest additions.

After installation of guest additions, just logoff & login to make sure its working.

Now create the vagrant user:-

groupadd  admin
useradd -G admin -p $(openssl passwd -1 vagrant) vagrant

Install & configure sudo for vagrant user to have passwordless sudo access:-

yum install sudo

visudo

& add the following lines to end of the file:-

%admin ALL=NOPASSWD: ALL

Now su to vagrant user:-

su - vagrant

Create a ssh-key-pair

ssh-keygen -t dsa

Press enter for blank passphrase.

Now run :-

cd .ssh

cat id_dsa.pub >> authorized_keys
chmod 0755 /home/vagrant/.ssh
chmod 0644 /home/vagrant/.ssh/authorized_keys

Make sure to have the private key with you in your host system. Private key is present at /home/vagrant/.ssh/id_dsa

Now you can make any modification like installation softwares etc on the VM.

Finally shut down the VM.

On the host machine, open the Command prompt. Navigate to the directory where the VM is placed, in my case its D:\VM

Now run the commands:-

vagrant package --output centos6.box --base vagrant-centos6

here make sure to replace vagrant-centos6 with your machine name
also centos6.box can be any name you like.

vagrant box add centos6 centos6.box

here centos6 can be any name you like.

vagrant box list  ## this will show the new box

Now you can create new VM really quick by running following commands:-

mkdir "NEW_VM"
cd "NEW_VM"
vagrant init centos6 ## centos6 here should match the one in vagrant box list.
vagrant up


vagrant ssh  ## this will create the SSH session to VM (for linux hosts)

For windows hosts, use putty client & the key private key you saved earlier in this tutorial, to login into the VM through vagrant user.