This quick tutorial is going to cover about Vagrant Command-Line interface through which almost all interaction with Vagrant is done.
1. Common Vagrant Commands
1.1. Get Vagrant version
1 |
vagrant --version |
1 |
vagrant -v |
1 |
Vagrant 1.8.1 |
1.2. Get status of all Vagrant machines
1 |
vagrant global-status |
1 2 3 4 5 6 7 8 |
$ vagrant global-status id name provider state directory ----------------------------------------------------------- 105c303 workstation virtualbox poweroff D:/Working/vagrant/centos7 bd1f511 default virtualbox running D:/Working/vagrant/centos7-3 f19b559 default virtualbox poweroff D:/Working/vagrant/centos1 2c24ad6 default virtualbox poweroff D:/Working/vagrant/ubuntu16.04 |
1 |
vagrant global-status --prune |
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.
1 |
vagrant box list |
An example output on my console:
1 2 |
centos/7 (virtualbox, 1603.01) ubuntu/trusty64 (virtualbox, 20160406.0.0) |
1.3. Add a box
1 |
vagrant box add ADDRESS |
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 |
vagrant box add ubuntu/trusty64-juju |
1.3.2. Add a remote box specified by URL
1 |
vagrant box add https://atlas.hashicorp.com/ubuntu/boxes/trusty64 |
1.3.3. Add a local box
1 |
vagrant box add CentOS7.1 file:///D:/Work/VagrantBoxes/CentOS-7.1.1503-x86_64-netboot.box |
Another article relates to adding the local box on Vagrant:Add Local Box To Vagrant
1.4. Remove a box
1 |
vagrant box remove NAME |
To list all the boxes, you can use the command: vagrant box list. For example,
1 2 3 4 5 6 7 8 |
$ vagrant box list centos7 (virtualbox, 0) morungos-centos67.box (virtualbox, 0) ub14 (virtualbox, 0) ubuntu/xenial64 (virtualbox, 20160721.0.0) ubuntu/xenial64 (virtualbox, 20160830.0.0) ubuntu16.04 (virtualbox, 0) ubuntu16.04trusty (virtualbox, 0) |
1.4.1. Remove a box with shorthand name
1 |
vagrant box remove ubuntu16.04 |
1.4.2. Remove a box with specific version
1 |
vagrant box remove ubuntu/xenial64 --box-version=20160721.0.0 |
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
1 |
vagrant init ubuntu/trusty64-juju |
1 2 3 |
Vagrant.configure(2) do |config| config.vm.box = "ubuntu/trusty64-juju" end |
If you want to search for the boxes, you can go to Hashicorp website
3.2. Start a VM
1 |
vagrant up |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
Bringing machine 'default' up with 'virtualbox' provider... ==> default: Box 'ubuntu/trusty64-juju' could not be found. Attempting to find a nd install... default: Box Provider: virtualbox default: Box Version: >= 0 ==> default: Loading metadata for box 'ubuntu/trusty64-juju' default: URL: https://atlas.hashicorp.com/ubuntu/trusty64-juju ==> default: Adding box 'ubuntu/trusty64-juju' (v20160707.0.1) for provider: vir tualbox default: Downloading: https://atlas.hashicorp.com/ubuntu/boxes/trusty64-juju /versions/20160707.0.1/providers/virtualbox.box ==> default: Waiting for cleanup before exiting... default: Progress: 0% (Rate: 0/s, Estimated time remaining: --:--:--):--) |
3.3. Get state of a VM
1 |
vagrant status |
3.4. SSH to a VM
1 |
vagrant ssh |
3.5. Shutdown the VM
Simply go to the folder has the Vagrantfile and issue below command
1 |
vagrant halt |
3.6. Destroy the VM
1 |
vagrant destroy [name|id] |
Example:
1 |
vagrant destroy ubuntu/trusty64-juju |
This command will stop the VM and destroy all resources that were created during the machine creation process.
3.7. Suspend a VM
1 |
vagrant suspend |
3.8. Resume a VM
1 |
vagrant resume |
3.9. Reload a VM
The vagrant reload command restarts the VM. It is equivalent to the following:
1 2 |
vagrant halt vagrant up |
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:
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