Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

vagrant

Make and destroy local virtual machines from one file — a repeatable lab for Linux, Ansible and RHCSA practice.

Level
DevOps tool
Group
Infrastructure as Code and cloud
Needs root
No
Package
vagrant

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

init BOX
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.
snapshot save|restore|list NAME
Snapshots.
box add|list|update|remove
Manage boxes.
ssh-config
SSH settings (for ssh or Ansible).

Options 4

Most used

Options

--provider libvirt|virtualbox
Choose the provider.
--provision / --no-provision
Run / skip provisioning with up or reload.
-f, --force
Do not ask (destroy).
--prune
Clean global-status.

Examples 10

$ run as a normal user# run as root (or with sudo)

  1. 1A first VM

    vagrant init generic/rocky9
    vagrant up
    vagrant ssh
  2. 2Two 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 up
  3. 3Provision with Ansible

    cat Vagrantfile
    config.vm.provision "ansible" do |a|
      a.playbook = "site.yml"
    end
    vagrant provision
  4. 4Snapshot before a risky exercise

    vagrant snapshot save server1 clean
    vagrant snapshot restore server1 clean
  5. 5Use KVM instead of VirtualBox

    vagrant up --provider libvirt
  6. 6SSH config for normal ssh and Ansible inventory

    vagrant ssh-config > ssh.cfg
    ssh -F ssh.cfg server1
  7. 7Status of all VMs on this machine

    vagrant global-status --prune
  8. 8Restart and apply Vagrantfile changes

    vagrant reload --provision
  9. 9Stop at night, delete when done

    vagrant halt
    vagrant destroy -f
  10. 10Update the base box

    vagrant box update

Watch out

  • Public boxes are made by others; use boxes from known publishers (generic/, bento/, rockylinux/).
  • The default vagrant user 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

0OK.
1Error.

Full manual

This page explains the command in simple words. The original manual has every detail.

In your terminal: man vagrant