Debian, virtualization

Building custom Vagrant base box

Vagrant has become a common tool used by most of the sys admins who play around with computers. It has made virtualization so easy. Seriously it’s a must have tool. Vagrant supports Puppet/Chef provisioning, which makes it even more powerfull. We just need the base boxes to play around with Vagrant. In this blog i will be explaing about creating custom Vagrant Boxes. I work for DeepRootLinux, developers of the deepOfix Mail Server, a Debian based GNU/Linux distribution. I will be using our distribution for creating base box. It will help us to deploy deepOfix VM in a faster way and we it helps us to test our custom puppet modules into our operating system.

SO first we need to create a base VM in the Virtualbox. Ensure that the network controller is set to “NAT”. For port forwarding to work properly, NAT must be used. There a few point to remember, As per the Vagrant’s Documentation, Vagrant makes some assumptions,

  • The root password is ”Vagrant
  • One user account ”vagrant” with password ”vagrant”.
  • Domain is ”
  • Hostname is ”vagrant-[os-name]”, e.g. vagrant-debian-lenny

If any different values are being used, it has to be specified in the Vagrantfile. I’ve used custom domain name and hostname and i did not mentioned it in my Vagrant file. But it did not created any problem. Anyways Vagrant is using key-based authentication for SSH. So once we SSH into the system, we will login in to the system as the Main user, in our case ”vagrant” user. So we should make this ”vagrant” user as a member of the ”sudo (super user doers)” group, so that we can use “sudo su” to switch to the root user.

Normally, using sudo will always prompt for the user password, we can remove this by modifying the /etc/sudoers file. We just need to add one line into the file ”%sudo ALL=NOPASSWD: ALL”. This will prevent password prompt for the user’s who are the member’s of the sudo group. Once the file is edited, we need to do ”/etc/init.d/sudo restart” to reflect the changes.We can verify that sudo works without a password, but logging into the sudo user account, then sudo which sudo. We should get output similar to “/usr/bin/sudo”.

Now we need to setup Virtualbox Guest Additions. So, first we need to build the necessary packages.

apt-get install linux-headers-$(uname -r) build-essential    # for root user

sudo apt-get install linux-headers-$(uname -r) build-essential    # for sudo user's

We need to insert the guest additions image by using the GUI and clicking on ”Devices” followed by ”Install Guest Additions”. And we need to mount the CDROM.

mount /dev/hd0 /media/cdrom     # where /dev/hd0 is the CDROM block device in deepOfix

And finally, run the shell script which matches our system.

sh /media/cdrom/

Since Vagrant only supports key-based authentication for SSH, we must setup the SSH user to use key-based authentication. We need to copy a public key into ”~/.ssh/authorized_keys” of the ”vagrant” user. Vagrant provides an ”insecure” pair of public and private keys which are available here. Once the public key is copied, we can shut down our VM. And we can start building our base box.

vagrant package --base <box_name>

If there is any custom option to be set like, using a specific port port forwarding, or a specific SSH keys, we can create a Vagrantfile with all custom options and we can use it during the packaging.

vagrant package --base <bxo_name> --vagrantfile Vagrantfile

If everything goes fine, it will generate a base box file. We can use this base box file anywhere with the vagrant.


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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s