DEV Community

Cover image for Vagrant CLI Commands Cheat Sheet

Posted on

Vagrant CLI Commands Cheat Sheet

In a previous post, I explained what Vagrant is, how to install it, how to use it to install virtual machines and how to connect to installed VM using it. If you haven't already seen that post you can check it out here, check it out first and head back to this post to familiarize yourself with these commands.

When running Vagrant commands, you must be in a directory containing a Vagrantfile or you will receive the following error: A Vagrant environment or target machine is required to run this command.

non vagrant directory

So be sure to navigate to the directory where you have your Vagrantfile saved.

Vagrant CLI Commands

Below are most of the most basic vagrant commands you will likely use in your everyday interaction with vagrant. Nothing too deep or too complicated.

📌 vagrant init : This command initializes vagrant and generate a Vagrantfile with default configurations specifying the box you supplied in the command.

vagrant init

📌 vagrant init --minimal: Generate a Vagrantfile without all the additional instructional comments that vagrant adds by default.

📌 vagrant box add :This will download a box image to your computer.

📌 vagrant up: Start up a virtual machine in vagrant. If this is your first time of running it, it will build the virtual machine using the configurations specified in your Vagrantfile. Subsequently, it just starts up your already created VM.

vagrant up

📌 vagrant ssh: Remotely access your vm in vagrant via ssh.

vagrant ssh

📌 vagrant ssh-config: Retrieve the IP address and port of your virtual machine (basically to view your ssh configuration just as the name states). This command will only work when your VM is running.

vagrant ssh-config error

When the command is successfully run, you should see something that resembles the below

vagrant ssh-config

📌 vagrant halt: Halt the machine, this attempts a graceful shut down. Here the machine turns off just like when you shut down your physical machine.

vagrant halt

📌 vagrant suspend: This command will save the state of your machine, just like when you hibernate your physical computer. A suspended machine is a machine that is turned off but not shut down.

vagrant suspend

📌 vagrant resume: This resumes a Vagrant managed machine that was previously suspended using vagrant suspend. This command will not start up a machine that was powered off using vagrant halt, if you attempt to start a shut down machine you will get the below error.

vagrant resume error

When the command is successfully run, you should see something that resembles the below

vagrant resume

📌 vagrant reload: This command is very useful when modifications have been made to the Vagrantfile. It is the equivalent of running the vagrant halt and vagrant up command. It is required for changes made in the Vagrantfile to take effect.

vagrant reload

📌 vagrant status: This will tell you the state of the machines Vagrant is managing. It comes in very handy when you forget whether you VMs are still running, halted or suspended, trust me it's very easy to forget especially when you start managing a lot of virtual machines with vagrant.

vagrant status

📌 vagrant destroy: stops and deletes all the virtual machine created with vagrant and also destroys all resources that were created during the machine creation process.

Top comments (0)