Additional reading

Vagrant getting started: https://learn.hashicorp.com/collections/vagrant/getting-started

Vagrant docs: https://www.vagrantup.com/docs

Notice VirtualBox specific commands in Providers -> VirtualBox

Motivation

Vagrant is a program used to build and manage virtual machine environments. Setting up virtual machines manually takes a long time, and using a management tool like Vagrant can speed up the process. You can use a vagrantfile to configure the operating systems, networks and programs on your virtual machines before installing them, and do the installation with a single command.

This tutorial is for Windows, adapt suitably from your machine's OS. Instruction can be found in the Vagrant getting started page

    1.       Install VirtualBox https://www.virtualbox.org/wiki/Downloads

    2.       Download Vagrant installer for your operating system and install the latest version of Vagrant

             After installation, run vagrant -v in windows cmd or powershell to check if the installation was successful. It should output the version of vagrant you installed.

             If windows can not find vagrant, a reboot might help

    3.       We wish to use VirtualBox on Windows, you must ensure that Hyper-V is not enabled on Windows and reboot your machine.                

    4.       Find the Vagrant box for 64-bit Ubuntu LTS 22.04 on https://app.vagrantup.com/boxes/search For this course we recommend to choose ubuntu/jammy64 Vagrant box.

    5.      Everything you need in a VM is defined in a vagrantfile. Create a vagrantfile with the following:

    •      3 VMs with the Ubuntu OS, names lab1, lab2 and lab3
    •      Virtualbox internal networks intnet1 between lab1 & lab2, and intnet2 between lab1 & lab3. Configure the systems to have static IP addresses
    •      Provision a shellscript that is run on each of the computers. Use it to install traceroute.

    Here is an example vagrantfile to create an Ubuntu VM. You should extend the script to add other 2 Ubuntu boxes:

    Example Vagrant File

    #Define Vagrant provider and vagrant Box. Number 2 in configure indicates configuration version. Check vagrant documentation for more details

    Vagrant.configure("2") do |config|             

          config.vm.define "lab1" do |subconfig|

                         subconfig.vm.box = "ubuntu/jammy64"

                         subconfig.vm.hostname="lab1"

                         subconfig.vm.network "private_network", ip: "192.168.1.1",virtualbox__intnet: true,virtualbox__intnet:"intneta"

                         subconfig.vm.network "private_network",ip: "192.168.2.1",virtualbox__intnet: true,virtualbox__intnet:"intnetb"

                         subconfig.vm.provider :virtualbox do |vb|

                                               # Custom CPU & Memory

                                                vb.customize ["modifyvm", :id, "--memory", "4096"]

                                                vb.customize ["modifyvm", :id, "--cpus", "2"]

                      end

                      #add provisioning scripts to Vagrantfile

                        subconfig.vm.provision "shell", inline: <<-SHELL

                                  sudo echo "192.168.1.3 lab2" | sudo tee -a /etc/hosts

                                  sudo echo "192.168.2.1 lab3" | sudo tee -a /etc/hosts

                                  sudo apt install net-tools

                            SHELL

             end

     end

     

    7.       In the folder containing your vagrantfile, open windows terminal and run vagrant up.

    8.       Run vagrant ssh <name of VM>This command will drop you into a full-fledged SSH session in vagrant. The default username and password is "vagrant"

    10.     To stop the running machine and destroy all it resources created for it, go to the folder containing your vagrantfile and run vagrant destroy.

    Senast redigerad: tisdag, 9 januari 2024, 11:55