5 Vagrant Commands You Need to Know

by Dan Franciscus Posted on June 05, 2018

In this article, I will point out five commands that every Vagrant user needs to know.

Vagrant is a favorite among DevOps professionals as it provides a great way to great re-usable and identical test environments. The Vagrant CLI is very easy to use and provides users a way to manage their Vagrant environment efficiently. 

1 - Vagrant Init

To understand Vagrant, you first need to understand one of the basic building blocks – Vagrantfile. A Vagrantfile is basically a configuration file that describes an environment. It will include things like the boxes used, networking, CPU and memory, providers used, shell scripts to run for provisioning among others.

In this example, I want to create a very basic Vagrantfile based on a box used from Vagrant Cloud. To do this I simply run vagrant init with the box address.

First, I will create a directory:

Dans-MacBook-Pro:GitHub dan$ mkdir MyProj

 

Next, lets cd into that directory:

 

Dans-MacBook-Pro:GitHub dan$ cd MyProj/

Here, I will use vagrant init and the –m option which means I will not add any helper comments into my Vagrantfile:

Dans-MacBook-Pro:MyProj dan$ vagrant init -m hashicorp/precise64

A `Vagrantfile` has been placed in this directory. You are now

ready to `vagrant up` your first virtual environment! Please read

the comments in the Vagrantfile as well as documentation on

`vagrantup.com` for more information on using Vagrant.

Finally let’s look into the Vagrantfile, which as we see just specifies the box I will be using:

Dans-MacBook-Pro:MyProj dan$ cat Vagrantfile 

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

  config.vm.box = "hashicorp/precise64"

end

Just like that I have enough to boot up a Vagrant box on my local machine.

 

2 - Vagrant Box

The vagrant box command allows the user to manage the boxes on their local machine. This includes adding, removing, listing and updating. For instance, to view the boxes I have installed on my machine I would run vagrant box list:

Dans-MacBook-Pro:GitHub dan$ vagrant box list

StefanScherer/windows_10        (virtualbox, 2018.04.10)

bento/centos-7.2                (virtualbox, 2.3.1)

bento/centos-7.3                (virtualbox, 201708.22.0)

eratiner/w2016x64vmX            (virtualbox, 1.2.0)

ferventcoder/win2012r2-x64-nocm (virtualbox, 1.0.0)

If I want to add a box, logically I use vagrant box add. Here I want to download and install a CentOS box for the virtualbox provider:

Dans-MacBook-Pro:GitHub dan$ vagrant box add bento/centos-7.3 --provider=virtualbox

==> box: Loading metadata for box 'bento/centos-7.3'

    box: URL: https://vagrantcloud.com/bento/centos-7.3

==> box: Adding box 'bento/centos-7.3' (v201708.22.0) for provider: virtualbox

3 - Vagrant Up

In order to get a Vagrant box up and running, I simply run vagrant up. The trick is, I want to ensure I am in the directory where my Vagrant project is in.

Dans-MacBook-Pro:MyProj dan$ pwd

/Users/dan/GitHub/MyProj

Dans-MacBook-Pro:MyProj dan$ vagrant up

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

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

    default: Box Provider: virtualbox

    default: Box Version: >= 0

==> default: Loading metadata for box 'hashicorp/precise64'

    default: URL: https://vagrantcloud.com/hashicorp/precise64

==> default: Adding box 'hashicorp/precise64' (v1.1.0) for provider: virtualbox

This will automatically download and boot any boxes I need that are specified in Vagrantfile. When the command is complete, my Vagrant machines are up and running.

4 - Vagrant SSH

Although Vagrant has support for RDP and PowerShell for connecting to a particular box, it still works best when using ssh. Up until recently, even Windows machines needed ssh installed and configured in order for a user to connect to them.

Here, I use vagrant ssh to connect to my newly created project:

Dans-MacBook-Pro:MyProj dan$ vagrant ssh

Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-23-generic x86_64)

 

 * Documentation:  https://help.ubuntu.com/

New release '14.04.5 LTS' available.

Run 'do-release-upgrade' to upgrade to it.

 

Welcome to your Vagrant-built virtual machine.

Last login: Fri Sep 14 06:23:18 2012 from 10.0.2.2

 

vagrant@precise64:~$

As you can see, my command prompt changed to “precise64” which is the name of the box I am using in this project. I can now run any command on this box.

5 - Vagrant Destroy

Last but not least, the most destructive of all the Vagrant commands, vagrant destroy. I am sure you can guess what this does – destroys your environment. In my experience, I use this quite a lot when testing out configurations in a Vagrantfile. For instance, let’s say I make a change in Vagrantfile but I do not like the outcome. To get my environment back to a clean slate I can use vagrant destroy, which will simply wipe out all my virtual machines that Vagrant was using. If I add the –f option, Vagrant will not prompt me for confirmation before destroying.

Dans-MacBook-Pro:MyProj dan$ vagrant destroy -f

==> default: Forcing shutdown of VM...

==> default: Destroying VM and associated drives...

Keep in mind, even while using vagrant destroy, the actual boxes I downloaded in my particular project I still have locally, so if I were to use vagrant up again, it will not have to download them again from the Vagrant Cloud.


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.
More from the author

Related Articles

Using AaronLocker to Easily Deploy Microsoft AppLocker
If you are a Windows user, you have likely heard of Microsoft AppLocker. This whitelisting program allows Windows users to protect itself from disk based malware by way of restricting executable programs to a specific list of paths, hashes, or signed applications....
Open Source FTP Clients vs. Proprietary: Pros and Cons
Open source is cool. But when it comes to FTP client solutions, it may be hip to be square. Here's why proprietary software might be best for your company.
Using Sysmon to Trace Malicious Activity on Windows
For Windows IT professionals, perhaps the most popular set of troubleshooting tools is Sysinternals, the creation of none other than Mark Russonivich, Microsoft’s CTO of Azure.
Prefooter Dots
Subscribe Icon

Latest Stories in Your Inbox

Subscribe to get all the news, info and tutorials you need to build better business apps and sites

Loading animation