Installing Your First Vagrant Box to Run VMs

December 16, 2018 Security and Compliance, MOVEit

Vagrant is one of those tools that you may have heard about before but just have not used yet, but once you decide to use it, it will probably end up in your tool belt.

The motivation behind using Vagrant is creating reproducible, portable virtual environments and can quickly be brought up and disposed of at will. Now, that sounds like something that developers would love and they certainly do, but IT professionals can leverage Vagrant too, especially in DevOps. For instance, have you ever wanted to quickly bring up a test VM to test out a script on? Well with Vagrant you can do that with a few commands. Even better, Vagrant can create your virtual machines on Virtualbox, VMware, Hyper-V, AWS and more.

In this article, I will walk you through installing your first Vagrant box from Vagrant cloud, which is a public repository of boxes you can install for free, and installing CentOS on it. I will demonstrate installing the box on a Mac, but you can use Vagrant with Windows and Linux as well as your host. Since Vagrant boxes are virtual machines, your local platform actually does not matter at all. The intentions of this article is to get your feet a little wet with Vagrant, but as you dive deeper into the tool, you will find you can do so much more.

Installing Vagrant

You can install Vagrant from its official site here, but I prefer to use homebrew on my Mac to install packages when possible. If you have not used homebrew before, I highly recommend.

 Dans-MacBook-Pro:CentOS7 dan$ brew install caskroom/cask/vagrant

==> brew cask install caskroom/cask/vagrant

==> Satisfying dependencies

==> Downloading https://releases.hashicorp.com/vagrant/2.0.1/vagrant_2.0.1_x86_64.dmg

######################################################################## 100.0%

==> Verifying checksum for Cask vagrant

==> Installing Cask vagrant

==> Running installer for vagrant; your password may be necessary.

==> Package installers may write to any location; options such as --appdir are ignored.

Password:

==> installer: Package name is Vagrant

==> installer: Upgrading at base path /

==> installer: The upgrade was successful.

🍺  vagrant was successfully installed!

Wow that was easy! Now we just need to install Virtualbox, which we also can do with homebrew:

Dans-MacBook-Pro:~ dan$ brew install caskroom/cask/virtualbox

Updating Homebrew...

==> brew cask install caskroom/cask/virtualbox

==> Satisfying dependencies

==> Downloading http://download.virtualbox.org/virtualbox/5.2.0/VirtualBox-5.2.0

######################################################################## 100.0%

==> Verifying checksum for Cask virtualbox

==> Installing Cask virtualbox

==> Running installer for virtualbox; your password may be necessary.

==> Package installers may write to any location; options such as --appdir are i

==> installer: Package name is Oracle VM VirtualBox

==> installer: Installing at base path /

==> installer: The install was successful.

🍺  virtualbox was successfully installed!

At this point Vagrant and Virtualbox are installed and we are ready to download and install some boxes.

Installing a CentOS Vagrant Box

Now it is time for some Vagrant magic. Here, I want to install a CentOS virtual machine. A quick search for “CentOS” on Vagrant Cloud leads me to the box here.

For the sake of simplicity, I will show you a very quick way to bring up a Vagrant box. First, I create a directory that will hold the Vagrant configuration data. To interact with this particular box with Vagrant commands, you must either use the id or we must be in this directory. I prefer to just work in the directory.

Dans-MacBook-Pro:~ dan$ mkdir CentOS

Dans-MacBook-Pro:~ dan$ cd CentOS/

Now, I can initialize the box with the command vagrant init:

vagrant init centos/7

This command creates a file in my working directory called “Vagrantfile”.  Inside this Vagrantfile there are just three uncommented lines but keep in mind you can configure many other options such as networking, providers and memory:

Vagrant.configure("2") do |config|

config.vm.box = "centos/7"

end 

This is the quick and dirty way to bring up a box in Vagrant without adding any additional configuration to it.

Now it is time to download and install the CentOS Vagrant box, for this we need just one command vagrant up.

Dans-MacBook-Pro:CentOS dan$ vagrant up

Bringing machine 'default' up with 'virtualbox' provider...

==> default: Box 'centos/7' could not be found. Attempting to find and install...

    default: Box Provider: virtualbox

    default: Box Version: >= 0

==> default: Loading metadata for box 'centos/7'

    default: URL: https://vagrantcloud.com/centos/7

==> default: Adding box 'centos/7' (v1710.01) for provider: virtualbox

    default: Downloading: https://vagrantcloud.com/centos/boxes/7/versions/1710.01/providers/virtualbox.box

    default: Progress: 20% (Rate: 8793k/s, Estimated time remaining: 0:00:30)/Us==> default: Successfully added box 'centos/7' (v1710.01) for 'virtualbox'!

==> default: Importing base box 'centos/7'...

==> default: Matching MAC address for NAT networking...

==> default: Checking if box 'centos/7' is up to date...

==> default: Setting the name of the VM: CentOS_default_1509818704898_8630

==> default: Clearing any previously set network interfaces...

==> default: Preparing network interfaces based on configuration...

    default: Adapter 1: nat

==> default: Forwarding ports...

    default: 22 (guest) => 2222 (host) (adapter 1)

==> default: Booting VM...

==> default: Waiting for machine to boot. This may take a few minutes...

    default: SSH address: 127.0.0.1:2222

    default: SSH username: vagrant

    default: SSH auth method: private key

    default:

    default: Vagrant insecure key detected. Vagrant will automatically replace

    default: this with a newly generated keypair for better security.

    default:

    default: Inserting generated public key within guest...

    default: Removing insecure key from the guest if it's present...

    default: Key inserted! Disconnecting and reconnecting using new SSH key...

==> default: Machine booted and ready!

==> default: Checking for guest additions in VM...

    default: No guest additions were detected on the base box for this VM! Guest

    default: additions are required for forwarded ports, shared folders, host only

    default: networking, and more. If SSH fails on this machine, please install

    default: the guest additions and repackage the box to continue.

    default:

    default: This is not an error message; everything may continue to work properly,

    default: in which case you may ignore this message.

==> default: Rsyncing folder: /Users/dan/CentOS/ => /vagrant

Dans-MacBook-Pro:CentOS dan$ /Users/dan/CentOS

The amount of time needed to download and install is generally based on the size of the box. In this instance, it took about two minutes to have a CentOS box ready for me to logon to. As you can see from the output of vagrant up, there is a lot that goes on as the box is made ready. Virtual networking is setup for the VM, include performing port forwarding for SSH (port 22) and SSH keys are created so that you can logon with a key to the virtual machine.

To ensure my virtual machine is running I can use vagrant status:

Dans-MacBook-Pro:CentOS dan$ vagrant status

Current machine states:

 

default                   running (virtualbox)

Connecting to a Vagrant Box via SSH

You can connect to a Vagrant box via SSH, PowerShell or RDP, based on the type of box. Since we are running a Linux box I will use SSH. To connect via SSH to a vagrant box, we use the vagrant ssh command. This logs us right into the box just like a normal SSH session would. We authenticate with key-based authentication as our private key is located in  “/.vagrant/machines/default/virtualbox”

Dans-MacBook-Pro:virtualbox dan$ vagrant ssh

Last login: Sat Nov  4 18:19:35 2017 from 10.0.2.2

[vagrant@localhost ~]$

Just like that, I have a fully functional CentOS virtual machine up and running on my Macbook in Virtualbox.

Shutting Down and Destroying Vagrant Boxes

If I am done using a box, I can simply run the vagrant halt command, which will gracefully shutdown my boxes:

Dans-MacBook-Pro:CentOS dan$ vagrant halt
 

Dan Franciscus

Dan Franciscus is a systems engineer and VMware Certified Professional (VCP) specializing in VMware, PowerShell, and other Microsoft-based technologies. You can reach Dan at his blog (http://www.winsysblog.com/) or Twitter at @dan_franciscus.