Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
Know it before you install
A box is a ready base image (for example bento/rockylinux-9). A provider runs the VMs (VirtualBox, libvirt/KVM, VMware, Hyper-V). Provisioners set up the VM after the first boot (shell, Ansible). Vagrant gives each VM SSH access, a synced folder and a private network.
| Item | Value |
|---|---|
| Package | vagrant (HashiCorp repository) |
| Project file | Vagrantfile (Ruby syntax) |
| Boxes | ~/.vagrant.d/boxes/ |
| Project state | .vagrant/ in the project folder (do not commit) |
| Providers | VirtualBox (virtualbox), KVM (vagrant-libvirt plugin) |
| Needs | CPU virtualization (VT-x/AMD-V) on, 8+ GB RAM for small labs |
Notitie
Vagrant uses the BSL license since 2.3.8. Many teams also use cloud VMs or containers for labs — Vagrant shines when you need real VMs offline.
Install on RHEL, Rocky, Alma and Fedora
Vagrant from the HashiCorp repo, and libvirt/KVM as provider (VirtualBox is not in RHEL):
dnf -y install dnf-plugins-core
dnf config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
dnf -y install vagrant
dnf -y install @virtualization libvirt-devel gcc make
systemctl enable --now libvirtd
usermod -aG libvirt alice
vagrant plugin install vagrant-libvirt
vagrant --versionLog out and in again so the libvirt group works.
Install on Ubuntu and Debian
curl -fsSL https://apt.releases.hashicorp.com/gpg | gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" > /etc/apt/sources.list.d/hashicorp.list
apt-get update
apt-get install -y vagrant virtualbox
vagrant --versionFor KVM on Ubuntu: apt-get install -y qemu-kvm libvirt-daemon-system libvirt-dev, add your user to libvirt, then vagrant plugin install vagrant-libvirt.
Check virtualization is on: grep -cE 'vmx|svm' /proc/cpuinfo must be more than 0.
First configuration: a multi-machine lab
One control node and two web servers on a private network, provisioned by Ansible:
Vagrant.configure("2") do |config|
config.vm.box = "bento/rockylinux-9"
config.vm.box_check_update = false
config.ssh.insert_key = true
config.vm.provider "virtualbox" do |vb|
vb.memory = 1024
vb.cpus = 1
vb.linked_clone = true
end
config.vm.provider "libvirt" do |lv|
lv.memory = 1024
lv.cpus = 1
end
(1..2).each do |i|
config.vm.define "web#{i}" do |web|
web.vm.hostname = "web#{i}.lab"
web.vm.network "private_network", ip: "192.168.56.1#{i}"
end
end
config.vm.define "db" do |db|
db.vm.hostname = "db.lab"
db.vm.network "private_network", ip: "192.168.56.20"
db.vm.provider("virtualbox") { |vb| vb.memory = 2048 }
db.vm.provision "ansible" do |ansible|
ansible.playbook = "site.yml"
ansible.limit = "all"
ansible.groups = { "web" => ["web1", "web2"], "db" => ["db"] }
end
end
end| Setting | Why |
|---|---|
config.vm.box | The base image; pin a version with config.vm.box_version for repeatable labs. |
private_network + fixed IPs | VMs talk to each other and to your host on a lab network. |
memory, cpus per provider | Labs fit in your RAM. |
linked_clone (VirtualBox) | Faster start, less disk. |
| Ansible provisioner on the last VM | Runs once after all VMs are up, with limit = "all" and groups. |
Daily use: the commands and the real uses
vagrant ssh web1
vagrant ssh-config >> ~/.ssh/config
vagrant provision db
vagrant reload web2 --provision
vagrant snapshot save db clean
vagrant snapshot restore db clean
vagrant halt
vagrant destroy -f
vagrant box list
vagrant box update
vagrant box prune
vagrant global-status --pruneReal uses at work:
- RHCSA / Linux practice: break and fix a VM, then restore a snapshot.
- Test Ansible roles and playbooks on clean VMs before production.
- Small multi-node labs (Kubernetes with kubeadm, HAProxy + web servers, database replicas).
Make it safe
Security checklist:
- Use boxes from trusted publishers (for example
bento/…, official distro boxes); pin versions. config.ssh.insert_key = true(default) — each VM gets its own key instead of the public insecure key.- Keep lab networks private (
private_network); do not forward lab ports to0.0.0.0on shared networks. - No real passwords or production data in Vagrantfiles or synced folders; add
.vagrant/to.gitignore. - Delete labs you do not use (
vagrant destroy,vagrant box prune) to free disk.
Update and remove
Fix common problems
| Problem | Cause | Fix |
|---|---|---|
VT-x is disabled in the BIOS / VM does not start | Virtualization off. | Turn on VT-x/AMD-V in BIOS/UEFI; on a cloud VM use a type with nested virtualization. |
VirtualBox: The IP address configured for the host-only network is not within the allowed ranges | VirtualBox allows only 192.168.56.0/21 by default. | Use 192.168.56.x, or allow ranges in /etc/vbox/networks.conf. |
| VirtualBox kernel module will not load | Secure Boot or missing kernel headers. | Install headers and vboxconfig; sign the module or turn off Secure Boot. |
libvirt: Permission denied / Call to virConnectOpen failed | Your user is not in libvirt. | usermod -aG libvirt USER, log in again. |
vagrant up hangs at "Waiting for SSH" | Network or the box did not boot. | vagrant up --debug; open the provider console; try another box version. |
The box 'x' could not be found | Wrong name or no box for your provider. | Search Vagrant Cloud for a box with your provider (virtualbox/libvirt). |
| VirtualBox and KVM fight | Both hypervisors loaded. | Use one; unload kvm modules when using VirtualBox (or use libvirt). |
Next steps
- Command page: vagrant.
- Provision the VMs: Install and set up Ansible. Build images: Packer.
- At work: Packer, Vagrant and cloud-init.
- Practise RHCSA tasks on the lab: RHCSA summary.
1Where do you put the Ansible provisioner in a multi-machine Vagrantfile, and why?
On the last VM, with limit = "all", so it runs once after all VMs exist.
2How do you go back to a clean VM after a broken exercise?
vagrant snapshot restore NAME SNAPSHOT (after vagrant snapshot save at the start).