Skip to content
DBDeependra Bhatta~/notes
Infrastructure as Code#virtualbox · #ssh · #linux · #vagrant

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.

· updated · 8 min read
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 VMsWith Vagrant
Install the OS from an ISO for every VMStart from a ready-made image called a box
Click through settings by handNetwork, RAM, CPU and disk are set in the Vagrantfile
Hard to recreate the same VM, or many VMsvagrant destroy then vagrant up gives the same VM again
Setup steps live in documentsProvisioning scripts run automatically when the VM is created

Boxes are downloaded from the public catalog, HashiCorp Vagrant boxes.

Architecture

Vagrant architecture: vagrant commands read the Vagrantfile, pull boxes from Vagrant Cloud and drive a hypervisor that runs VMs

  • You run vagrant commands (up, halt, ssh, ...) in a project folder.
  • Vagrant reads the Vagrantfile in 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

  1. Install a hypervisor on your host machine, for example VirtualBox.

  2. Install Vagrant on the host machine (not inside a VM) and check it:

    terminal
    $ vagrant --version
  3. Create a project folder for the VM and generate a Vagrantfile:

    terminal
    $ mkdir -p ~/vms/ubuntu24 && cd ~/vms/ubuntu24
    $ vagrant init
  4. Replace the generated file with your own settings. The following Vagrantfile defines an Ubuntu 24.04 VM:

RUBVagrantfile
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
  1. 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.

CommandWhat it does
vagrant upCreates the VM on first run, or starts it
vagrant sshLogs in to the VM
vagrant haltPowers off the VM
vagrant reloadRestarts the VM and applies Vagrantfile changes
vagrant reload --provisionRestarts and runs the provisioning scripts again
vagrant destroyDeletes the VM
vagrant statusShows the state of the VM in this folder
vagrant global-statusShows all Vagrant VMs on the host (can include stale entries)
vagrant global-status --pruneRemoves stale entries from that list
vagrant ssh-configPrints the SSH settings for the VM (used below)

Vagrantfile settings explained

Network

  • private_network with a static IP (for example 192.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. The bridge: 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:

RUBruby
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:

terminal
$ vagrant reload --provision

SSH 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:

RUBVagrantfile
config.vm.network "private_network", ip: "192.168.56.220"
config.vm.hostname = "db-server"
terminal
$ cd /path/to/db-server
$ vagrant up

Step 2: Add the VM to ~/.ssh/config

Inside the VM's folder, print its SSH settings:

terminal
$ vagrant ssh-config

vagrant ssh-config output: Host default, HostName 127.0.0.1, Port 2222, User vagrant, IdentityFile path to private_key

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:

terminal
$ vagrant ssh-config --host db-server >> ~/.ssh/config

The result looks like this:

SSHconfig
~.sshconfig
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-rsa
  • HostName 127.0.0.1 and Port 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 .vagrant folder.
  • UserKnownHostsFile /dev/null and StrictHostKeyChecking 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:

terminal
$ ssh db-server

Alternative: 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:

SSHconfig
~.sshconfig
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
MethodHostNamePort
Port forwarding (Vagrant default)127.0.0.12222 (or the port Vagrant assigned)
Private network IP192.168.56.22022 (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:

SH.bashrc
~.bashrc
# 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:

terminal
$ source ~/.bashrc
$ db-up
$ ssh db-server

Key takeaways

  • A Vagrantfile describes 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; use vagrant reload --provision after changing it.
  • vagrant ssh-config --host <name> >> ~/.ssh/config lets you ssh <name> from anywhere; check the port per VM.
  • Aliases with a subshell cd let you start and stop each VM from any folder.