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 http://download.virtualbox.org/virtualbox/debian/oracle_vbox.asc -O- | sudo apt-key add –
sudo -s echo “deb http://download.virtualbox.org/virtualbox/debian 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
wget http://files.vagrantup.com/packages/eb590aa3d936ac71cbf9c64cf207f148ddfc000a/vagrant_1.0.3_i686.deb
sudo dpkg -i vagrant_1.0.3_i686.deb

/opt/vagrant/bin/vagrant
Now vagrant is ready on the host box

1, create a basic Ubuntu 12.04 vagrant box
/opt/vagrant/bin/vagrant box add precise32 http://files.vagrantup.com/precise32.box
mkdir cub1
cd cub1
/opt/vagrant/bin/vagrant init
vi Vagrantfile, with below

config.vm.box = “precise32”
config.vm.network :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
exit
then ssh to VM with new user
ssh new-user@IP
make the sudo passwordless
sudo visudo
add at bottom
user-name ALL=(ALL)NOPASSWD: ALL
exit

on the host machine, generate key pair if not yet
ssh-keygen -t rsa
ssh new-user@vm-ip mkdir -p .ssh
cat ~/.ssh/id_rsa.pub | 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
add
config.ssh.username = “new-user”
config.ssh.private_key_path = “~/.ssh/id_rsa”
config.package.name = “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 package.box)
add to local vagrant
/opt/vagrant/bin/vagrant box add precise32cub1base ./package.box
/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 config.vm.box = “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

Ubuntu set up internal DNS servers

On my windows PC, I started two ubuntu 12.04 servers with VM player. Named them “panda” and “tiger”, and set the network to “bridge” mode (this way, they are accessible from outside of the hosted PC). Their IP addresses are 10.189.10.x

I also have two spare PCs, and installed Ubuntu 12.04 server on them, named “lion” and “bear”. All 3 computers are connected to same network switch, and all have IP address at 10.189.19.x

I decided to use the box “lion” as the DNS server, whose IP address is 10.189.19.x

sudo apt-get install ssh bind9 bind9utils
then edit the files under /etc/bind, add the internal zone, I assigned domain “rocks” for all the internal servers.
Restart bind9 sudo /etc/init.d/bind9 restart
if there is error, check /var/log/syslog

to test locally
note Ubuntu 12.04 uses “resolvconf” to manage the “resolve.conf” file, you can not edit that file directly any more.
need to update the /etc/resolvconf/resolv.conf.d, create a “tail” file
sudo vi /etc/resolvconf/resolv.conf.d/tail
search rocks
nameserver 10.189.19.x
sudo resolvconf -u
more /etc/resolv.conf to check the current name resolving rules

Use “nslookip” or “dig” to test.

On the other servers, need to copy the “head” file so they will use the new internal DNS server

install git server on ubuntu

first create the pub key for current user
ssh-keygen
generated key is in /home/me/.ssh/id_rsa.pub
now copy it to /tmp
cp /home/me/.ssh/id_rsa.pub /tmp/me.pub

sudo apt-get install install git, git-daemon-run

create a new user “git”
sudo adduser \
–system \
–shell /bin/bash \
–gecos ‘git version control’ \
–group \
–disabled-password \
–home /home/git \
git

now as user git
sudo su git
git clone git://github.com/sitaramc/gitolite
mkdir -p $HOME/bin
gitolite/install -to $HOME/bin
$home/bin/gitolite setup -pk /tmp/me.pub
exit
now back to regular user “me”
cd $home
git clone git@ubuntu:gitolite-admin
cd gitolite-admin
cd conf
vi gitolite.conf
here you can add repository, change the allowed user
save the file
git add .
git commit -m ‘update conf’
git push origin master

to add a new user
cd /home/me/gitolite-admin/keys
vi jack.pub
copy/paste the public key
save
git add/commit/push

on windows can use sysgit
it can generate keys for you

on eclipse to use remote git server
first make sure to have the keys in c:/users/me/.ssh
(presumably id_rsa.pub)
add a file “config” in .ssh directory

Host ubuntu
HostName ubuntu
User git
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa

(here ‘ubuntu’ is the host name of the remote server, you may add to the /windows/system32/driver/etc/host file)

or through
ecpise-> preferences -> networking -> ssh2