Vagrant Cheat Sheet
Categories:
3 minute read
Show running Vagrant boxes
vagrant global-status
This command displays the status and information about all running Vagrant boxes on your system.
Display SSH configuration for a Vagrant
vagrant ssh-config
Detroy all Vagrant boxezs
vagrant global-status | awk '/virtualbox/ {print $1}' | xargs -I {} vagrant destroy -f {}
Environment
Start or create a Vagrant environment:
vagrant up
This command starts and provisions the Vagrant environment based on the Vagrantfile in the current directory.
Stop a running Vagrant environment:
vagrant halt
This command stops the running Vagrant environment by gracefully shutting down the Vagrant box.
Destroy a Vagrant environment:
vagrant destroy
This command destroys the Vagrant environment, deleting the associated Vagrant box and all its resources.
Suspend a running Vagrant environment:
vagrant suspend
This command suspends the Vagrant environment, saving its current state and allowing you to resume later.
Resume a suspended Vagrant environment:
vagrant resume
This command resumes a previously suspended Vagrant environment from its saved state.
Reload a Vagrant environment:
vagrant reload
This command reloads the Vagrant environment, applying any changes made to the Vagrantfile.
Provision a running Vagrant environment:
vagrant provision
This command re-runs the provisioning scripts specified in the Vagrantfile for the running Vagrant environment.
Display status information about the Vagrant environment:
vagrant status
This command shows the status of the Vagrant environment, indicating whether it is running, suspended, or powered off.
SSH
SSH into a running Vagrant box:
vagrant ssh
This command connects to the default SSH user on the currently running Vagrant box using SSH.
SSH into a specific Vagrant box:
vagrant ssh <vm-name>
If you have multiple Vagrant boxes defined in your Vagrantfile, you can specify the name of the box to SSH into a specific one.
Execute a command on a running Vagrant box:
vagrant ssh -c "command"
This command allows you to execute a specific command on the currently running Vagrant box without entering an interactive session. Replace “command” with the command you want to execute.
Copy files to a running Vagrant box using SCP:
vagrant scp /path/to/local/file <vm-name>:/path/to/destination
This command securely copies files from your local machine to the specified Vagrant box. Replace /path/to/local/file
with the path to the local file you want to copy, <vm-name>
with the name of the Vagrant box, and /path/to/destination
with the path on the Vagrant box where you want to copy the file.
Copy files from a running Vagrant box using SCP:
vagrant scp <vm-name>:/path/to/remote/file /path/to/destination
This command securely copies files from the specified Vagrant box to your local machine. Replace <vm-name>
with the name of the Vagrant box, /path/to/remote/file
with the path to the file on the Vagrant box you want to copy, and /path/to/destination
with the path on your local machine where you want to copy the file.
Configuration changes
Reapply a Vagrant configuration to update an already-running Vagrant environment. This command will first halt the running virtual machines, then apply the new configuration changes, and finally restart the virtual machines. Keep in mind that vagrant reload only applies changes related to Vagrant’s configuration, such as network settings, synced folders, or provider-specific settings.
vagrant reload
If you want to reapply provisioning scripts or configuration management tools like Ansible, Puppet, or Chef, use the following command:
vagrant reload --provision