Vagrant for Local VMs: Vagrantfile, Commands and SSH Shortcuts
Create and manage VirtualBox VMs with Vagrant: an Ubuntu 24.04 Vagrantfile, daily commands, synced folders, provisioning, and SSH config and aliases to reach VMs from anywhere.

ON THIS PAGE
Docker hosts, Jenkins agents, Ansible targets and Kubernetes nodes all need Linux machines that can be thrown away and rebuilt, and Vagrant turns local virtual machines into a file you can recreate in minutes. This guide explains how Vagrant works, walks through a Vagrantfile for Ubuntu 24.04 on VirtualBox, lists the everyday commands, and sets up SSH config and shell aliases that reach and start any VM from any folder.
Why Vagrant
Vagrant is a command-line tool that automates the full VM lifecycle: create, change, provision, delete and recreate. The entire machine is described in one file, the Vagrantfile.
| Manual VMs | With Vagrant |
|---|---|
| Install the OS from an ISO for every VM | Start from a ready-made image called a box |
| Click through settings by hand | Network, RAM, CPU and disk are set in the Vagrantfile |
| Hard to recreate the same VM, or many VMs | vagrant destroy then vagrant up gives the same VM again |
| Setup steps live in documents | Provisioning scripts run automatically when the VM is created |
Boxes are downloaded from the public catalog, HashiCorp Vagrant boxes.
Architecture

- You run
vagrantcommands (up,halt,ssh, ...) in a project folder. - Vagrant reads the
Vagrantfilein that folder. It is written in Ruby and holds the box name, network, provisioning, synced folders, memory, CPU and disk settings. - Vagrant downloads the box if needed, then tells a provider (the hypervisor, such as VirtualBox or VMware Workstation) to create and run the VM.
Setup
-
Install a hypervisor on your host machine, for example VirtualBox.
-
Install Vagrant on the host machine (not inside a VM) and check it:
terminal$ vagrant --version -
Create a project folder for the VM and generate a
Vagrantfile:terminal$ mkdir -p ~/vms/ubuntu24 && cd ~/vms/ubuntu24 $ vagrant init -
Replace the generated file with your own settings. The following
Vagrantfiledefines an Ubuntu 24.04 VM:
Vagrant.configure("2") do |config|
config.vm.box = "bento/ubuntu-24.04"
config.vm.box_version = "202502.21.0"
config.vm.hostname = "Dipendra"
config.vm.network "private_network", ip: "192.168.56.210"
# config.vm.network "public_network", bridge: "Killer(TM) Wi-Fi 7 BE1750x 320MHz Wireless Network Adapter (BE200NGW)"
# config.vm.synced_folder "../Ubuntu24.04", "/home/ubuntu"
config.vm.provider "virtualbox" do |vb|
vb.name = "Ubuntu24"
vb.memory = 4024
vb.cpus = 4
end
config.vm.provision "shell", inline: <<-SHELL
apt-get update
# apt-get install -y apache2
SHELL
end-
Start the VM and log in:
terminal$ vagrant up $ vagrant ssh
vagrant ssh logs in as the vagrant user with a key that Vagrant manages. If you log in from the VirtualBox console instead, the default username and password on most boxes are both vagrant.
Daily commands
Run these inside the VM's project folder (the one with the Vagrantfile), except global-status.
| Command | What it does |
|---|---|
vagrant up | Creates the VM on first run, or starts it |
vagrant ssh | Logs in to the VM |
vagrant halt | Powers off the VM |
vagrant reload | Restarts the VM and applies Vagrantfile changes |
vagrant reload --provision | Restarts and runs the provisioning scripts again |
vagrant destroy | Deletes the VM |
vagrant status | Shows the state of the VM in this folder |
vagrant global-status | Shows all Vagrant VMs on the host (can include stale entries) |
vagrant global-status --prune | Removes stale entries from that list |
vagrant ssh-config | Prints the SSH settings for the VM (used below) |
Vagrantfile settings explained
Network
private_networkwith a static IP (for example192.168.56.210) gives the VM an address that only the host and other VMs on that network can reach. Assign each VM a different IP so the VMs can reach each other.public_network(bridged) places the VM on your physical network, as if it were another device on the LAN. Thebridge:value is the name of the host's network adapter.
Memory and CPU
Inside the virtualbox provider block, vb.memory sets RAM in MB and vb.cpus sets the number of CPU cores. vb.name is the name shown in VirtualBox.
Synced folders
By default, Vagrant shares the project folder on the host as /vagrant inside the VM. Add further mappings with config.vm.synced_folder:
config.vm.synced_folder "./shared_folder", "/home/vagrant/shared_folder"The first path is on the host (relative to the Vagrantfile); the second is inside the VM. Synced folders are useful to:
- keep files safe on the host in case the VM breaks or is destroyed;
- edit code in an IDE on the host and run it inside the VM.
Provisioning
Provisioning runs commands or scripts automatically when the VM is created (other tools call this bootstrapping). The inline shell block above runs apt-get update on the first vagrant up only; by default, later vagrant up calls skip provisioning. After changing the provisioning on an existing VM, apply it with:
$ vagrant reload --provisionSSH into any VM from anywhere
vagrant ssh works only inside the VM's folder. With several VMs (for example a db-server and a deployment VM), it is more practical to run ssh db-server from any directory. The setup takes two steps.
Step 1: Give the VM a hostname and IP
In the VM's Vagrantfile, set a unique hostname and static IP, then start it:
config.vm.network "private_network", ip: "192.168.56.220"
config.vm.hostname = "db-server"$ cd /path/to/db-server
$ vagrant upStep 2: Add the VM to ~/.ssh/config
Inside the VM's folder, print its SSH settings:
$ vagrant ssh-config
Copy this block into ~/.ssh/config and replace Host default with the name you want to use. Alternatively, have Vagrant write the block with that name:
$ vagrant ssh-config --host db-server >> ~/.ssh/configThe result looks like this:
Host db-server
HostName 127.0.0.1
User vagrant
Port 2222
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
PasswordAuthentication no
IdentityFile /path/to/db-server/.vagrant/machines/default/virtualbox/private_key
IdentitiesOnly yes
LogLevel FATAL
PubkeyAcceptedKeyTypes +ssh-rsa
HostKeyAlgorithms +ssh-rsaHostName 127.0.0.1andPort 2222: Vagrant forwards a port on the host to port 22 in the VM.IdentityFile: the private key Vagrant created for this VM, inside its.vagrantfolder.UserKnownHostsFile /dev/nullandStrictHostKeyChecking no: skip host key checks, because the key changes each time the VM is recreated. Use these settings only for local lab VMs.
Test the connection from any folder:
$ ssh db-serverAlternative: connect to the private IP
Instead of 127.0.0.1 and the forwarded port, connect to the VM's private network IP on the standard SSH port 22. The port then no longer depends on which VMs are running. Change the host block as follows:
Host db-server
HostName 192.168.56.220
User vagrant
# Port 22 is the default, so the Port line can be left out
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
PasswordAuthentication no
IdentityFile /path/to/db-server/.vagrant/machines/default/virtualbox/private_key
IdentitiesOnly yes
LogLevel FATAL
PubkeyAcceptedKeyTypes +ssh-rsa
HostKeyAlgorithms +ssh-rsa| Method | HostName | Port |
|---|---|---|
| Port forwarding (Vagrant default) | 127.0.0.1 | 2222 (or the port Vagrant assigned) |
| Private network IP | 192.168.56.220 | 22 (default, can be omitted) |
Start and stop VMs from anywhere with aliases
The SSH config handles logins, but vagrant up and vagrant halt still need the VM's folder. Shell aliases remove that requirement. Add the following to ~/.bashrc (or ~/.zshrc for Zsh), with your own name and path:
# Vagrant shortcuts for the DB server VM
alias db-up='(cd /path/to/db-server && vagrant up)'
alias db-down='(cd /path/to/db-server && vagrant halt)'The parentheses run the cd in a subshell, so your current directory does not change. Reload the file, then start the VM and connect:
$ source ~/.bashrc
$ db-up
$ ssh db-serverKey takeaways
- A
Vagrantfiledescribes the whole VM (box, network, RAM, CPU, synced folders, provisioning), so a VM can be recreated in minutes. - Give each VM its own hostname and private IP so VMs can reach each other.
- Provisioning runs only on first
vagrant up; usevagrant reload --provisionafter changing it. vagrant ssh-config --host <name> >> ~/.ssh/configlets youssh <name>from anywhere; check the port per VM.- Aliases with a subshell
cdlet you start and stop each VM from any folder.
Keep reading
- Getting Started with Ansible: Install, Inventory and Ad-hoc Commands
Install Ansible in a Python virtual environment, connect Vagrant VMs with passwords and SSH keys, write a first inventory and manage packages with ad-hoc commands.
- Logical Volume Management (LVM) in Linux
Add a disk to a VirtualBox VM and take it through every LVM layer: partition, physical volume, volume group and logical volume, then an ext4 mount in fstab.
- OpenTofu: Why It Exists, Installation and Migrating from Terraform
Why OpenTofu was forked from Terraform, how to install it on Ubuntu, and how to migrate an existing Terraform project with tofu init, including the lock file warning.