Vagrant for DevOps: Custom Linux boxes

In one of the previous posts I explained how to create a custom box running Windows.
So in this post I’ll show how you can create your custom box running Linux – both CentOS and Ubuntu.
I won’t use Packer in this example – just regular VirtualBox and shell commands.

Here’re some basic steps to create a custom vagrant box with Linux:

1. Download suitable ISO from official site or any other source if you will;
2. Create VirtualBox VM and disable USB and Audio;
3. Install updates and necessary software (“Development Tools”, nano wget ncdu tree ssmtp lynx htop net-tools telnet unzip);
4. If you want to run Puppet or Cher you need to install clients, but I prefer Ansible for small infrastructures;
5. Create administrator user “vagrant” with well-known password “vagrant” during or after the installation;
6. Allow password-less sudo by adding with visudovagrant ALL=(ALL) NOPASSWD:ALL
7. Disable UseDNS options in /etc/ssh/sshd_config if it’s present (in Ubuntu and CentOS they’re not enabled by default)
8. Disable requiretty using visudo (relevant for CentOS 7);
9. Download well-known ssh key and set correct permissions for the Open-SSH:

mkdir -p /home/vagrant/.ssh
chmod 0700 /home/vagrant/.ssh
wget https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub -O /home/vagrant/.ssh/authorized_keys
chmod 0600 /home/vagrant/.ssh/authorized_keys 
chown -R vagrant /home/vagrant/.ssh

10. Enable AuthorizedKeysFile in /etc/ssh/sshd_config (uncomment AuthorizedKeysFile %h/.ssh/authorized_keys string; however it’s already been done in CentOS7);

11. Install VirtualBox guest tools process for:

CentOS6:

yum install -y gcc kernel-devel perl (it's not necessary if you have already installed "Development Tools")
mkdir /media/cdrom/
mount /dev/cdrom /media/cdrom/
/media/cdrom/VBoxLinuxAdditions.run
modinfo vboxguest

CentOS7:

yum install -y gcc kernel-devel bzip2
mkdir /media/cdrom/
mount /dev/cdrom /media/cdrom/
/media/cdrom/VBoxLinuxAdditions.run
modinfo vboxguest

Ubuntu:

apt-get install make gcc
mount /dev/cdrom /media/cdrom/
/media/cdrom/VBoxLinuxAdditions.run
modinfo vboxguest

 

12. Prepare hard drive for compression:

dd if=/dev/zero of=/EMPTY bs=1M 
rm -f /EMPTY

 

13. Create box from VM:

vagrant package --base %VMName% --output %BoxName%.box

 

14. Import box to local Vagrant installation:

vagrant box add %BoxName% %BoxName%.box --provider virtualbox

 

15. Test the created box:

vagrant init %BoxName%
vagrant up --provider virtualbox

Screen Shot 2016-06-12 at 23.24.59

16. Upload the box to the Atlas repo or any other places;
17. Download & test box on some other Vagrant host.

Vagrant for DevOps table of contents