Vagrant CLI Commands Cheat Sheet
Tags: vagrant, linux, cloud, cli
Published: 2024-03-11 10:24:35

In a [previous post](https://dev.to/chigozieco/install-virtual-machine-with-vagrant-remotely-access-it-4k5j), 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](link again), check it out first and head back to this post to familiarize yourself with these commands.

**:bulb: PRO TIP**
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3psca0k8nu44u6l6c07g.png)

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.

<hr>

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

![vagrant init](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wyp0unnume8c45tqefcl.png)

<hr>

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

<hr>

📌 **vagrant box add <box name>**:This will download a box image to your computer.

<hr>

📌 **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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0szlss0usgqb999im8i8.png)

<hr>

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

![vagrant ssh](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4dm4wuv706ko6wjf4qeo.png)

<hr>

📌 **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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/se8vehs25jsik5pxa96j.png)

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

![vagrant ssh-config](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t51scza46k0m3jp8lffa.png)

<hr>

📌 **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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ffms67pjteqrikago0h3.png)

<hr>

📌 **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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/92odh5qtfy846e28xa1c.png)

<hr>

📌 **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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eatjb7vqmznqtk8vl08y.png)

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

![vagrant resume](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t5oco5gn4d1bm3mwozhv.png)

<hr>

📌 **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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ufn0lm5dnqamkgea4xcq.png)

<hr>

📌 **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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3opwheum0h84spi98hi8.png)

<hr>

📌 **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.