Easy and simple guide to create your own Vagrant box (Ubuntu-12.02-64 server bridged) from VirtualBox

vagrant1

GitThis guide assumes that you already installed VirtualBox and Vagrant.

Set up a VirtualBox VM and assign the resources you need (I used precise64 as name), also configure it without the Audio and USB support. In the Network option enable the adapter to “Bridged Adapter” mode and assign the interface you want to use. In the Storage option select the image (iso) of your Ubuntu 12.02 64 Server to the “IDE Controller” and begin the installation according to your needs. If possible name your host as “vagrant”.

Once you installed, you will require to make some changes:

Set your hostname as vagrant

As a generic recommendation use the name vagrant as hostname.

sudo vi /etc/hostname
sudo vi /etc/hosts

Add vagrant user and admin group

sudo groupadd admin
sudo useradd -G admin -d /home/vagrant -s /bin/bash vagrant
sudo passwd vagrant

Edit the sudoers file and add this lines to the file

Defaults env_keep="SSH_AUTH_SOCK"
%admin ALL=NOPASSWD: ALL

If you need to use puppet install it now

sudo apt-get install puppet puppetmaster

Install vagrant’s insecure key in the vagrant’s account

mkdir ~/.ssh
cd ~/.ssh
wget http://github.com/mitchellh/vagrant/raw/master/keys/vagrant
wget http://github.com/mitchellh/vagrant/raw/master/keys/vagrant.pub
mv vagrant.pub authorized_keys
chmod 0700 ~/.ssh
chmod 0640 ~/.ssh/authorized_keys
chmod 0640 ~/.ssh/vagrant

Install VirtualBox guest additions
Mount the VirtualBox guest additions and

sudo apt-get install linux-headers-$(uname -r) build-essential
mount /dev/cdrom /mnt
sudo sh /mnt/VBoxLinuxAdditions.run

Clean your apt’s cache

sudo apt-get clean

Modify the interfaces file

sudo vi /etc/network/interfaces

and add this line to the end of the eth0 configuration interface

pre-up sleep 2

Add the Vagrant’s SSH port

sudo vi /etc/ssh/sshd_config

Add this line after Port 22 declaration

Port 2222

Create your box
Change your position to the path the VirtualBox image is located and create your box.

cd ~/VirtualBox\ VMs/precise64/
vagrant package --base precise64

Add your new box to Vagrant list

vagrant box add precise64 ~/VirtualBox\ VMs/precise64/package.box

Initialize your box

vagrant init precise64

Modify the vagrant configuration file of your box

vi ~/.vagrant.d/boxes/p64test/Vagrantfile

and add this lines in to Vagrant::Config.run

config.vm.network :bridged
config.ssh.host = "vagrant"
config.ssh.port = 2222

Start your box

vagrant up

And that’s it, enjoy!

Basilio Briceño

DevOps evangelist, SoftwareLibre activist, sometimes speaker & eclectic metalhead.

4 comments

  1. anty   •  

    You have a typo:
    sudo useradd -G admin -h /home/vagrant -s /bin/bash vagrant
    should be
    sudo useradd -G admin -d /home/vagrant -s /bin/bash vagrant

    • bbh   •     Author

      You are right, thanks for the update.

  2. Edgardo   •  

    You share interesting things here. I think that your blog can go viral
    easily, but you must give it initial boost and i know
    how to do it, just type in google for – wcnu traffic increase

  3. Rod   •  

    Gracias Basilio.
    Have you had the change to experiment with Packer?
    I work for a software company that runs numerous Apache/Tomcat dev instances, I was wondering if Packer (or perhaps Puppet) would be the right tool to automate creation of new instances. Currently we use a spreadsheet with multiple worksheets (each one represents a server) , each sheet has anywhere from 10 – 30 rows of data in it. The data ranges from port number, username/password, URL, etc…

    Having a tool (or web front end) that allows me to visualize each environment/instance would be magical while having the option to insert new instances or modify and existing instance.

    Any guidance in the right direction would be greatly appreciated.
    Thank you,
    Rodrigo

Leave a Reply to anty Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>