Vagrant – Using Vagrantfile to manage virtual machines

Pinterest LinkedIn Tumblr

Vagrant is a tool for working with virtual machines. Vagrant provides a simple and easy to use command-line client for managing virtual machines. Vagrant is open source, which means that anyone can download it, modify it, and share it freely.
Virtual machine hypervisors like Virtualbox often provides their own command line interface for managing virtual machines (VBoxManage for virtual box). Vagrant provides an additional layer and thus simplify and unify the management by providing a consistent approach accross multiple systems.

Vagrantfile is a special file containing text-based definitions of what each environment looks like. Vagrant uses instructions in the Vagrantfile to manage the virtual environment. This common text-based format that is defined through code is advantageous in that many users can easily collaborate on it, it can be backed up, shared and tracked using a source code management system.

Terminology used by Vagrant.

  • Box: A box is a packaged Vagrant environment, typically a virtual machine.
  • Provider: A provider is the location in which the virtual environment runs. It can be local (the default is to use VirtualBox), remote, or even a special case like a Docker container.
  • Provisioner: A provisioner is a tool to set up the virtual environment, and can be as simple as a shell script, but alternatively a more advanced tool like Chef, Puppet, or Ansible can be used.


Vagrant binaries can be downloaded in the official download page here

You can also install from the commandline using these commands:

brew tap hashicorp/tap
brew install vagrant


sudo apt update
sudo apt install vagrat

Redhat based systems

sudo yum update
sudo yum install vagrant

Creating an Ubuntu 20.04 VM

To initiate a new Vagrantfile to be used to create an Ubuntu 20.04 virtual machine with using this box generic/ubuntu2004:

vagrant init generic/ubuntu2004

Lets check thecontent of the file:

cat Vagrantfile

Without the comments, this is the content of the Vagrantfile

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config| = "generic/ubuntu2004"

You can also download the boxes locally with these commannds:

# You will be prompted to choose your provider if no default is set
vagrant box add generic/ubuntu2004

# You can also specify the provider
vagrant box add generic/debian10 --provider libvirt
vagrant box add generic/fedora33 --provider virtualbox

To list available boxes:

vagrant box list


Here is a complicated Vagrantfile:

Vagrant.configure("2") do |config| = "generic/ubuntu2004"

    config.vm.provider "virtualbox" do |vb|
        vb.memory = "1024"

    hostname = 'ub-k0s'

    config.vm.define "#{hostname}" do |s|
        s.vm.hostname = "#{hostname}" 'private_network', ip: ""

        s.vm.provider 'virtualbox' do |v|
   = "#{hostname}"
            v.linked_clone = true

        s.vm.provision :ansible do |vm|
            vm.verbose = true
            vm.playbook = "provision/main.yaml"


Here is the content of the playbook provision/main.yaml

- hosts: all
  gather_facts: false
  become: true
    - name: Upgrade packages
        name: "*"
        state: latest
        update_cache: yes

    - name: Install required packages
          - vim
          - telnet
          - htop
        state: latest

To bring up the box:

vagrant up

To check current state of the machines, ensure you are in the dir that has the Vangrantfile and use this commands:

vagrant status

vagrant status debian-box


❯ vagrant status
Current machine states:

ub-k0s                    running (virtualbox)

The VM is running. To stop this VM, you can run `vagrant halt` to
shut it down forcefully, or you can run `vagrant suspend` to simply
suspend the virtual machine. In either case, to restart it again,
simply run `vagrant up`.

ssh into the box

vagrant ssh
vagrant ssh ub-k0s

Bringing up multiple instances

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure(2) do |config|

  config.vm.provision "shell", path: ""

  NodeCount = 2

  (1..NodeCount).each do |i|

    config.vm.define "ubuntuvm#{i}" do |node|               = "generic/ubuntu2004"
      node.vm.box_check_update  = false
      node.vm.box_version       = "3.3.2"
      node.vm.hostname          = "ubuntuvm#{i}" "private_network", ip: "{i}"

      node.vm.provider :virtualbox do |v|    = "ubuntuvm#{i}"
        v.memory  = 1024
        v.cpus    = 1

      node.vm.provider :libvirt do |v|
        v.nested  = true
        v.memory  = 1024
        v.cpus    = 1




This is the content of


# Enable ssh password authentication
echo "Enable ssh password authentication"
sed -i 's/^PasswordAuthentication no/PasswordAuthentication yes/' /etc/ssh/sshd_config
sed -i 's/.*PermitRootLogin.*/PermitRootLogin yes/' /etc/ssh/sshd_config
systemctl reload sshd

# Set Root password
echo "Set root password"
echo -e "admin\nadmin" | passwd root >/dev/null 2>&1

To login to a server:

vagrant ssh debian-box

To stop server:

vagrant halt debian-box

To clean up

vagrant destroy
vagrant destroy -f

I am a Devops Engineer, but I would describe myself as a Tech Enthusiast who is a fan of Open Source, Linux, Automations, Cloud and Virtualization. I love learning and exploring new things so I blog in my free time about Devops related stuff, Linux, Automations and Open Source software. I can also code in Python and Golang.

Write A Comment