Skip to content

Testing and Development With Vagrant

Daniel Roberson edited this page Jul 9, 2017 · 2 revisions

Often, I need to test something on an operating system or distribution I am not currently using or reference the operating system itself. Off the top of my head, there are several situations that I can come up with:

  • Testing if a script works.
  • Determining a package name.
  • Inspecting the contents of default files.
  • Obtaining hashes of executables, libraries, etc to see if they are default or have been modified.
  • Determining the version of software running and if patches have been applied.
  • Plenty more.

To solve this, I use Vagrant/VirtualBox on a Kimsufi hosted server. I do this on my personal workstations and laptops as well, but when I'm using something more lightweight such as a Chromebook or at a site that I do not have access to my more powerful equipment, this method is extremely handy.

High-level overview:

  • Get a server at Kimsufi or another provider that gives you actual hardware OR lets you run nested VMs.
  • I used Ubuntu 16.04 for the host machine, but other Linux distros work just as well. The install process will be roughly the same, but package tools and other specifics will be different. I've ran this setup on FreeBSD as well on a personal workstation and the results were as good as I'd hoped.
  • I had to get rid of the Kimsufi patched kernel in order to compile the Virtualbox kernel modules:
  • apt update && apt upgrade && apt install linux-generic linux-generic headers
  • change GRUB_DEFAULT=0 to GRUB_DEFAULT=1 in /etc/defaults/grub (or whatever entry in grub corresponds to the correct kernel!!)
  • update-grub
  • reboot
  • At this point, your kernel should be something like: 4.4.0-83-generic after it has been rebooted. Check this with 'uname -r'

Next, you will need to install Virtualbox:

  • Go to https://www.virtualbox.org/wiki/Linux_Downloads and follow the instructions. Basically, do the following:
  • Add the Virtualbox repo to /etc/apt/sources.list
  • Add the Oracle public key to apt: sudo apt-key add oracle_vbox_2016.asc
  • apt update
  • apt install virtualbox-5.1
  • Make sure the vbox kernel modules got loaded: lsmod | grep vbox
  • If there are no results for the command above, you will need to troubleshoot why.
  • Running VBoxManage --version should show no errors.

After Virtualbox is installed, its time for Vagrant:

Next, create a user that isn't root:

  • useradd -m user
  • usermod -a -G vboxusers user
  • chsh user to set the shell
  • passwd user

As your newly created user:

  • mkdir xenial64
  • cd xenial64
  • vagrant init ubuntu/xenial64
  • vagrant up
  • ... Wait or Vagrant to download the box
  • vagrant ssh within xenial64 directory will log you into your VM..

So now once this all works, you can download a huge variety of boxes and create your own. See https://app.vagrantup.com/boxes/search for options. Keep in mind that anyone can create boxes and upload them (you can too!) so they might be of questionable origin. Many distros and projects maintain official copies, so I prefer to use those if possible.

Running "vagrant help" will show the basic usage. In general, I use the following commands the most:

  • vagrant up # starts a vm
  • vagrant halt # halts a vm
  • vagrant ssh # connects to the vm using ssh
  • vagrant destroy # reverts the vm to its original state

From here, you can do a lot of things. You can set up Vagrantfiles to deploy more than one VM at a time, automatically install/configure software on your boxes, change amount of cores and ram your VMs use, and a number of other things. See the documentation for Vagrant for more information.

Clone this wiki locally