Friday, November 21, 2014

Using VirtualBox and Vagrant to Quickly Set Up an Ubuntu Instance

A Sysadmin's Sandbox

I've spent a lot of time getting servers to do a lot of different things. Of all the engineering disciplines, I'd say sysadmin work is my favorite. The utterly cryptic nature of what appears on a sysadmin's screen married with the fact that they literally hold the keys to the entire infrastructure of your organization is downright comical. Search for "hovering art director" and just know that a sysadmin will never ever experience that, unless of course they report to a sysadmin.

Prerequisites: This guide was written for Mac OS X users.


You need to know about VirtualBox. It's a very useful desktop application that allows you to spin up a number of virtual machines. If you aren't familiar, a virtual machine is an emulation of a computer running on top of your operating system. Virtual machines allow you to run Windows on top of Mac OS X, or Ubuntu on top of Windows, and a countless number of other permutations.

Download one of the VirtualBox platform packages on their website and run the installer.


  1. Navigate to the download page at and install the dmg file
  2. Run the installer
  3. Create a directory, cd into in and run vagrant init
  4. Most of your Vagrantfile will be commented out outside of the following:
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| = "ubuntu/trusty64"

Make sure to uncomment the line "private_network", ip: "dhcp" in your VagrantFile if you want to access your VM by IP address within your network.

Navigate to to search for a specific base box. After specifying your base box, you’ll be taken to a page like this where you can copy the command:

You have two options for setting the base box before you run vagrant up. To spin up an Ubuntu Server 14.04 LTS instance, you can run vagrant init ubuntu/trusty64 or you can set = “ubuntu/trusty64” of an existing Vagrantfile. Well go over all of the custom Vagrantfile configurations in another article. For today, we’ll just focus on getting the box up and running. Now run vagrant up and switch over to VirtualBox.

We have some configurations to take care of before we’re ready to ssh into the machine. Select your instance, click “Settings” and select the “Network” tab.

Now click on port forwarding and you should see a table with the following values:

Name Protocol Host IP Host Port Guest IP Guest Port
ssh TCP 2222 22

This basically means that the guest machine can be accessed on localhost ( through port 2222. So let’s open up the terminal of your choice, and ssh into the machine by running ssh vagrant@ -p2222. You’ll be prompted for a password. Enter “vagrant”. And there you have it. You should be logged into the virtual machine.

Feel free to take a snapshot of your instance's pristine copy through VirtualBox. When you click on your instance in VirtualBox's sidebar, you're normally presented with the details view, but you can click "Snapshots", and save a snapshot you can revert to, as needed. Feel free to save snapshots along the way. Once you get into the habit of spinning up instances, you'll want to learn a bit about auto-provisioning solutions, which we'll cover in another article.


Sending Commands to a Vagrant VM

When you just want to quickly send a command to a VM without having to SSH in to the machine, just run vagrant ssh -c like so:

vagrant ssh -c 'sudo service httpd restart'

Stopping httpd: [  OK  ]
Starting httpd: [  OK  ]

Making the VM Accessible on the Private Network

Vagrant.configure("2") do |config| "private_network", type: "dhcp"

By the way, VMs on the same bridged network have the ability to ssh into each other. Try it yourself by running ssh vagrant@<IP>

Setting Up a Multi-Machine Cluster

Setting Up a Multi-Machine Cluster with a Loop

Reloading Changes to the Vagrantfile

After you've made your changes, run vagrant reload from the same directory as the Vagrantfile.


# by hostname
vagrant destroy [HOSTNAME]

# all
vagrant destroy -f

No comments:

Post a Comment