create VMs on Ubuntu 12.04 with Vagrant

Now that I have two separate PCs running Ubuntu 12.04 server, I want to start two VMs on each server through Vagrant.

to install vagrant , first need to install Oracle VirtualBox
below is tested working

wget -q -O- | sudo apt-key add –
sudo -s echo “deb precise contrib” >> ~/virtualbox.list
sudo mv ~/virtualbox.list /etc/apt/sources.list.d/
sudo apt-get update
sudo apt-get install linux-headers-$(uname -r)
sudo apt-get purge virtualbox virtualbox-dkms virtualbox-ose-qt virtualbox-qt
sudo apt-get install virtualbox virtualbox-dkms virtualbox-ose-qt virtualbox-qt
install vagrant
sudo dpkg -i vagrant_1.0.3_i686.deb

Now vagrant is ready on the host box

1, create a basic Ubuntu 12.04 vagrant box
/opt/vagrant/bin/vagrant box add precise32
mkdir cub1
cd cub1
/opt/vagrant/bin/vagrant init
vi Vagrantfile, with below = “precise32” :bridged
config.vm.customize [“modifyvm”, :id, “–memory”, 1024]
config.vm.share_folder “hostshared”, “/tmp/share”, “/tmp/share”
config.vm.provision :shell, :inline => “ifconfig”

Now start vm
/opt/vagrant/bin/vagrant up
/opt/vagrant/bin/vagrant ssh
ifconfig to get the assigned IP address

2, add my user
ssh vagrant@IP (password vagrant)
add new user and put into admin group (so it can sudo)
sudo useradd -d /home/new-user -g admin -m new-user
sudo passwd new-user
then ssh to VM with new user
ssh new-user@IP
make the sudo passwordless
sudo visudo
add at bottom

on the host machine, generate key pair if not yet
ssh-keygen -t rsa
ssh new-user@vm-ip mkdir -p .ssh
cat ~/.ssh/ | ssh new-user@vm-ip ‘cat >> .ssh/authorized_keys’
now you can ssh new-user@vm-ip without entering password.

3, repackage
cp Vagrantfile Vagrantfile.package
vi Vagrantfile.package
config.ssh.username = “new-user”
config.ssh.private_key_path = “~/.ssh/id_rsa” = “precise32cub1base”
(did not work)
run command to create the new base box from VM
/opt/vagrant/bin/vagrant package –vagrantfile ./Vagrantfile.package
This will create a base box named “precise32cub1base” in local directory
(in my testing it did not work though, the file still named
add to local vagrant
/opt/vagrant/bin/vagrant box add precise32cub1base ./
/opt/vagrant/bin/vagrant box list

test the new base box at local
mkdir ~/test-box
cd ~/test-box
/opt/vagrant/bin/vagrant init
vi Vagrantfile
change = “precise32cub1base”

start VM
/opt/vagrant/bin/vagrant up

test with new user
ssh new-user@IP (works without entering password)

to use this new base box on other hosts, make sure to copy the id_rsa key file to the ~/.ssh directory
and mkdir /tmp/share because the packaged Vagrantfile is included whenever you create VM with this base box


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s