Vagrant Command-Line Interface

This quick tutorial is going to cover about Vagrant Command-Line interface through which almost all interaction with Vagrant is done.

Vagrant Command-Line Interface

Vagrant

1. Common Vagrant Commands

1.1. Get Vagrant version

or
The output should be similar to below:
These commands also can be to check whether Vagrant is ready to work in our environment or not.

1.2. Get status of all Vagrant machines

For example, here is the list of VMs and their states on my machine.
To prune all invalid machines, we can use the command with –prune option:

2. Vagrant commands used for boxes

This section covers some basic Vagrant commands that can be used to manage Vagrant boxes.

2.1. List all boxes

We may need to list all the boxes on our PCs, even just to know the names.

An example output on my console:

1.3. Add a box

1.3.1. A shorthand name of box

If we start the VM, Vagrant will download the box from the public catalog of available Vagrant images

1.3.2. Add a remote box specified by URL

1.3.3. Add a local box

Another article relates to adding the local box on Vagrant:Add Local Box To Vagrant

1.4. Remove a box

Note that because a box may have many versions and providers (VirtualBox, VMWare), you may need to list all the box first in order to know which one should be removed.

To list all the boxes, you can use the command: vagrant box list. For example,

1.4.1. Remove a box with shorthand name

1.4.2. Remove a box with specific version

3. Vagrant commands  used for virtual machines

This section list all Vagrant commands that can be used to manage Vagrant virtual machines which were created from Vagrant Boxes.

3.1. Initialize a new VM

This command will create a configuration file named Vagrantfile in the current directory. The content is similar as below:
When we start Vagrant in this directory, Vagrant will download the box: ubuntu/trusty64-juju from the internet to local and use it as the image of the VM.

If you want to search for the boxes, you can go to Hashicorp website

3.2. Start a VM

If we want to start any VM, simply go to the folder which the Vagrantfile exist. The command will start the VM and the output console is as below:

3.3. Get state of a VM

The above command reports the state of a machine in the current directory.

3.4. SSH to a VM

If you want to ssh to the VM, simply go to the folder has the Vagrantfile and issue above command. Note that the command only works if we have ssh client installed on the host machine. For Windows machine, we often don’t have it. To make sure the command will work on the Windows, please refer to this article: Running Vagrant SSH on Windows

3.5. Shutdown the VM

Simply go to the folder has the Vagrantfile and issue below command

3.6. Destroy the VM

Example:

This command will stop the VM and destroy all resources that were created during the machine creation process.

3.7. Suspend a VM

The command will suspend the VM and remember its state.

3.8. Resume a VM

The above Vagrant command will resume a suspended machine. Note that the vagrant up command will work the same in this case.

3.9. Reload a VM

The vagrant reload command restarts the VM. It is equivalent to the following:

4. Conclusions

The tutorial has shown us about Vagrant Command-Line Interface which can be used to interact with Vagrant. On next post, I’d like to share some basic configurations we often need customize for our Vagrant VM. Below are other related articles for your references:

Vagrant Tutorial

Install Vagrant on Ubuntu 16.04 LTS (Xenial Xerus)

Vagrant Docker Provider Tutorial

How To Change Vagrant Virtual Machine Name

Where Does Vagrant Store Its Boxes After Downloading?

Copy Files and Folders From Host To Guest In Vagrant