Skip to main content

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.

Comments

Popular posts from this blog

CentOS / Redhat : Configure CentOS as a Software Router with two interfaces

Linux can be easily configured to share an internet connection using iptables. All you need to have is, two network interface cards as follows: a) Your internal (LAN) network connected via eth0 with static ip address 192.168.0.1 b) Your external WAN) network is connected via eth1 with static ip address 10.10.10.1  ( public IP provided by ISP ) Please note that interface eth1 may have public IP address or IP assigned by ISP. eth1 may be connected to a dedicated DSL / ADSL / WAN / Cable router: Step # 1: Enable Packet Forwarding Login as the root user. Open /etc/sysctl.conf file # vi /etc/sysctl.conf Add the following line to enable packet forwarding for IPv4: net.ipv4.conf.default.forwarding=1 Save and close the file. Restart networking: # service network restart Step # 2: Enable IP masquerading In Linux networking, Network Address Translation (NAT) or Network Masquerading (IP Masquerading) is a technique of transceivin

Virtual Box and Alt/Tab Keys

I use virtual box for all my testing activities. It comes too often that I have a virtual box VM window open & I want to switch to my host machine to see some stuff like tutorials etc.. If you press the alt+tab combination it just works inside the VM & doesn't switches to host machine. In these scenarios you can press the host key once ( not hold it ) & then whatever you press goes to host machine. So in general where host key is the default Right Ctrl, just press Right Ctrl once & now press the alt+tab & it will switch you out to host machine. This is really helpful when you have the VM windows open or you're working on seamless mode. Hope it help others too.

AMD Radeon™ HD 7670M on Ubuntu 12.04

Update:   Recently I install kubuntu 13.10 and there is no problem with graphics. It just works  fine out of the box. I've seen many blog posts on how to make AMD HD7670M work on Ubuntu 12.04, specially when its in switchable graphics board like Dell Inspiron 15R 5520. I tried many things to make it work so that I could use the cinnamon desktop on ubuntu & other things too.. But to my surprise even the drivers from AMD site didn't work. Then I tried a combination of those blog posts I read & somehow I became successful in running the full graphics including compiz settings inside My Ubuntu Machine. Following are the steps I followed & it worked... 1. Create a backup of your xorg configuration file: sudo cp /etc/X11/xorg.conf /etc/X11/xorg.conf.BAK 2. Remove/purge current fglrx and fglrx-amdcccle : sudo apt-get remove --purge fglrx* 3. Install the driver: sudo apt-get install fglrx fglrx-amdcccle 4. Install additional