Vagrant – Create a Linux Box


Previous post: Vagrant ? Virtual Machine Settings

In this post we will create our own box. This example is for Linux distributions; we will deal with windows later. Here are the steps:

1. In Virtualbox create a virtual machine manually and install your favorite linux distribution. Do not forget to update it and install anything you need in your box.

2. In the virtual machine install Virtualbox add-ons. Reboot the machine.

3. Create a user named vagrant and add it to sudoers group.

$ sudo adduser vagrant
$ sudo addgroup vagrant sudo

4. Use visudo and add the line below in /etc/sudoers file so that no password is asked for sudo.

vagrant ALL=(ALL) NOPASSWD: ALL 

5. Switch to vagrant user and create its .ssh folder and authorized_keys file. In the file put the insecure key for vagrant so that it can connect.

$ su - vagrant
$ mkdir .ssh
$ echo ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ== vagrant insecure public key >> .ssh/authorized_keys
$ chmod 600 .ssh/.ssh/authorized_keys

6. Install ssh if it is not already installed and enable it.

$ sudo apt-get install -y ssh
$ sudo update-rc.d ssh enable
$ sudo service ssh restart

Now you are ready to create your box. Only info you need to know is the name of the Virtualbox. Here is our example screenshot. We will create a box from virtual machine puppet.

Vagrant - Create a Linux Box

Command to create a box from a virtualbox virtual computer:
“vagrant package –base virtualboxname –output boxname”
virtualboxname: this is the name of virtual computer.
boxname: this is the name of the box that will be created.Here we are creating a box from virtual computer puppet. The box name is ubuntu.box

$ vagrant package --base puppet --output ubuntu.box

Now you can distribute the new box or use it for yourself. However you also need to know how to import it as a box in Vagrant. Here is the command:

$ vagrant box add --name linuxpc ubuntu.box

linuxpc is the name of the box that will be imported and defined in Vagrant. You can use any name.
ubuntu.box is the box file that we have just created.

— The end of post —