Skip to main content

Managing VM Lifecycle

1. Starting VM

Command Description Notes
vagrant up Starts the VM and provisions it if needed Default behavior starts all VMs defined in Vagrantfile
vagrant up <vm-name> Starts a specific VM defined in Vagrantfile Useful when multiple VMs are defined
vagrant up --no-provision Starts the VM without running provisioners Useful for debugging or when provisioning is not needed

Example: Starting a VM

vagrant up
# Output:
# Bringing machine 'default' up with 'virtualbox' provider...
# Preparing network interfaces based on config...
# ...
# VM started!

Example: Starting a specific VM

vagrant up web-server
# Output:
# Bringing machine 'web-server' up with 'virtualbox' provider...
# ...
# VM started!

2. Suspending VM

Suspending a Vagrant VM saves its current state to disk and stops the virtual machine. This allows you to resume the VM later with its previous state intact.

Command Description Notes
vagrant suspend Suspends the running VM State is saved to disk, VM is stopped

Example: Suspending a VM

$ vagrant suspend
Note: Suspending is useful for saving resources when the VM is not in use but you want to preserve its state.
Warning: Suspending does not save the VM's network configuration changes permanently. Changes made to the network after suspension may be lost upon resumption.

3. Resuming VM

Command Description Notes
vagrant resume Resumes a suspended VM VM must be in suspended state

Example: Resume a suspended VM

vagrant resume
Note: The vagrant resume command only works if the VM is in a suspended state. If the VM is halted or running, this command will not have any effect.

4. Halting VM

Command Description Notes
vagrant halt Stops the VM gracefully Equivalent to shutting down the VM from the UI
vagrant halt --force Stops the VM forcefully Bypasses graceful shutdown; may cause data loss

Example: Halting a VM

vagrant halt
Note: The VM will be in a "saved" state and can be resumed with vagrant resume or vagrant up.

5. Reloading VM

Reloading a Vagrant VM applies configuration changes from the Vagrantfile without destroying the VM. It's useful for updating network settings, synced folders, or other configurations.

Command Description Notes
vagrant reload Gracefully shuts down and restarts the VM Applies changes from Vagrantfile
vagrant reload --hard Forcefully restarts the VM (equivalent to halt + up) Does not preserve VM state
vagrant reload --provision Reloads and re-runs provisioners Useful after config changes that require provisioning

Example: Reloading with Provisioning

vagrant reload --provision
Note: The vagrant reload command will not re-run provisioners unless explicitly requested with the --provision flag.
Warning: Using vagrant reload --hard will cause a full shutdown and restart, which may interrupt running processes or services.

6. Destroying VM

Command Description Notes
vagrant destroy Destroys the VM instance and removes all associated files. Deletes the VM from the hypervisor. Use with caution as this action is irreversible.
vagrant destroy -f Destroys the VM without prompting for confirmation. Useful in scripts or automation where user interaction is not possible.
vagrant destroy <name> Destroys a specific VM instance by name. Allows destruction of individual VMs in a multi-VM environment.

Example: Destroying a VM

$ vagrant destroy

Example: Forcefully Destroying a VM

$ vagrant destroy -f
Note: The vagrant destroy command removes the VM from the hypervisor and deletes all associated files. This action cannot be undone.

7. Checking VM Status

Command Description Output
vagrant status Shows the current status of all VMs defined in the Vagrantfile default: running (virtualbox)
vagrant status <vm-name> Shows the status of a specific VM web: running (virtualbox)

Example: Checking status of a VM

$ vagrant status
default: running (virtualbox)
Note: The status can be running, saved, poweroff, or not created.

8. Checking Global Status

Example: Vagrant Global Status

vagrant global-status
Field Description Notes
id Unique identifier for the VM Used internally by Vagrant
name Name of the VM Set in Vagrantfile or default to project name
provider Virtualization provider used e.g., virtualbox, vmware_fusion, libvirt
state Current state of the VM running, poweredoff, saved, etc.
directory Path to Vagrantfile Used to identify project location
Note: The vagrant global-status command shows all VMs managed by Vagrant across your system, including those not currently running.

9. Pruning Invalid Entries

Invalid entries in Vagrant's global state can accumulate over time, leading to outdated or broken configurations. Pruning these entries helps maintain a clean and functional Vagrant environment.

Entry Type Description Action
Invalid Boxes Boxes that are no longer accessible or have been removed from the system. Use vagrant box prune to remove outdated or unused boxes.
Invalid VMs Virtual machines that are no longer associated with valid Vagrant configurations. Run vagrant global-status --prune to clean up invalid VMs.
Orphaned State Files State files that remain after a VM has been destroyed or removed. Delete manually from ~/.vagrant.d/ if not handled by prune commands.

Example: Pruning Invalid Boxes

vagrant box prune

Example: Pruning Invalid VMs

vagrant global-status --prune
Note: The vagrant global-status --prune command will remove all invalid entries from the global status, including those that are not currently running.

10. Understanding Graceful vs Forced Shutdown

Shutdown Type Description Command Notes
graceful Allows running processes to finish before shutting down vagrant halt Default behavior; waits for processes to complete
forced Immediately terminates all processes without waiting vagrant halt --force Use only when graceful shutdown fails or is unresponsive

Example: Graceful shutdown

$ vagrant halt

Example: Forced shutdown

$ vagrant halt --force
Note: Use vagrant halt --force only when the VM is unresponsive or stuck in a non-graceful state.

Was this article helpful?