What it does
vagrant reads a Vagrantfile that describes one or more VMs: the base box (an OS image), CPU, memory, network and how to set them up (provisioning with a shell script or Ansible). vagrant up builds them; vagrant destroy throws them away. The same lab works on every laptop.
Providers: VirtualBox, libvirt/KVM, Hyper-V, VMware.
How to write it
vagrant COMMAND [VM] [OPTIONS]Main parts
Commands
initBOX- Make a Vagrantfile for a box.
up[VM]- Create and start the VMs.
ssh[VM]- Log in.
halt/reload/suspend/resume- Stop / restart / pause / continue.
destroy[-f]- Delete the VMs.
provision- Run the provisioning again.
status/global-status- State of the VMs here / everywhere.
snapshotsave|restore|listNAME- Snapshots.
boxadd|list|update|remove- Manage boxes.
ssh-config- SSH settings (for ssh or Ansible).
Options 4
Most used
Options
Nothing matches this filter.
Examples 10
$ run as a normal user# run as root (or with sudo)
1A first VM
vagrant init generic/rocky9 vagrant up vagrant ssh2Two servers for an RHCSA lab
cat Vagrantfile Vagrant.configure("2") do |config| config.vm.box = "generic/rocky9" %w[server1 server2].each_with_index do |name, i| config.vm.define name do |s| s.vm.hostname = "#{name}.example.com" s.vm.network "private_network", ip: "192.168.56.#{10 + i}" s.vm.provider "virtualbox" do |v| v.memory = 2048; v.cpus = 2 end end end end vagrant up3Provision with Ansible
cat Vagrantfile config.vm.provision "ansible" do |a| a.playbook = "site.yml" end vagrant provision4Snapshot before a risky exercise
vagrant snapshot save server1 clean vagrant snapshot restore server1 clean5Use KVM instead of VirtualBox
vagrant up --provider libvirt6SSH config for normal ssh and Ansible inventory
vagrant ssh-config > ssh.cfg ssh -F ssh.cfg server17Status of all VMs on this machine
vagrant global-status --prune8Restart and apply Vagrantfile changes
vagrant reload --provision9Stop at night, delete when done
vagrant halt vagrant destroy -f10Update the base box
vagrant box update
Nothing matches this filter.
Watch out
- Public boxes are made by others; use boxes from known publishers (generic/, bento/, rockylinux/).
- The default
vagrantuser has the insecure public key at first boot — never expose a lab VM to the internet.
Files
Vagrantfile- The VM description (Ruby).
.vagrant/- Local state; add to .gitignore.
~/.vagrant.d/boxes/- Downloaded boxes.
Tips to remember
- Vagrant + Ansible is a great way to test playbooks before they touch real servers.
Exit codes
0 | OK. |
1 | Error. |