Skip to main content

Vagrant with VirtualBox on Debian 12

717 words·
Vagrant Virtualbox Debian Linux KVM

Installation
#

VirtualBox
#

  • Install VirtualBox
# Install dependencies
sudo apt install software-properties-common dirmngr -y

# Import GPG Key
wget -O- -q https://www.virtualbox.org/download/oracle_vbox_2016.asc | sudo gpg --dearmour -o /usr/share/keyrings/oracle_vbox_2016.gpg

# Add Repository
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/oracle_vbox_2016.gpg] http://download.virtualbox.org/virtualbox/debian bookworm contrib" | sudo tee /etc/apt/sources.list.d/virtualbox.list

# Install VirtualBox
sudo apt update && sudo apt install virtualbox-7.0 -y
# Check version
VBoxManage --version
  • Add user to group
# Add user to the vboxusers group
sudo adduser `whoami` vboxusers
  • Start VirtualBox GUI
# Start VirtualBox GUI
virtualbox

Vagrant
#

# Install Vagrant
sudo apt update && sudo apt install vagrant -y

# Check version
vagrant --version

Vagrant Overview
#

Vagrantfile
#

# Create directory for the Vagrant file
mkdir Vagrant-Project && cd Vagrant-Project

# Initialize a new Vagrantfile
vagrant init

# Open the Vagrantfile
vi Vagrantfile
# Define a VM image
Vagrant.configure("2") do |config|
  config.vm.box = "debian/bookworm64"
end

Provision VM
#

# provision / start VM
vagrant up

Vagrant SSH
#

# SSH into the VM using a built-in Vagrant shortcut
vagrant ssh

Default user & pw
#

When the VM is accessed via Virtualbox the default user & pw should be as follows:
Username: root
Password: vagrant


Stop & Delete VM
#

# Stop Vagrant VM
vagrant halt vmname

# Delete the Vagrant VM
vagrant destroy

Vagrantfile Examples
#

Public Network
#

Note: When setting up a bridged network for a VM in VirtualBox, it is not necessary to explicitly define a bridge on the host system in advance.

Vagrant.configure("2") do |config|
  config.vm.box = "debian/bookworm64"
  
  # Specify the name of the network interface to bridge to
  config.vm.network "public_network", bridge: "ens33", ip: "192.168.30.66"
end

For DHCP network configuration just use config.vm.network "public_network", bridge: "ens33"

More Settings
#

In this Vagrantfile the following modifications are done:

  • Enable SSH Password Auth
  • Install Apache2
  • Port forwarding
  • Host folder mapping
  • Define VM RAM
Vagrant.configure("2") do |config|

    # Provisioning script to enable SSH password authentication
    config.vm.provision "shell", inline: <<-SHELL
      echo "vagrant:vagrant" | sudo chpasswd
      sudo sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config
      sudo systemctl restart sshd
      apt-get update
      apt-get install -y apache2
    SHELL
  
    # VM Settings
    config.vm.define "debian_vm-1" do |debian_config|
      debian_config.vm.box = "debian/bookworm64"
      # Network
      debian_config.vm.network "public_network", bridge: "ens33", ip: "192.168.30.66"
      debian_config.vm.network "forwarded_port", guest: 80, host: 8080
      # Shared folder
      debian_config.vm.synced_folder "host_folder/", "/vagrant_data"
      # VirtualBox settings
      debian_config.vm.provider "virtualbox" do |vb|
        vb.memory = "1024"  # Set VM's RAM to 1024 MB
      end
    end
end

Several VMs
#

Example Several VMs
#

Vagrant.configure("2") do |config|
  
  # VM 1
  config.vm.define "debian_vm-1" do |debian_config|
    debian_config.vm.box = "debian/bookworm64"
    # Network
    debian_config.vm.network "public_network", bridge: "ens33", ip: "192.168.30.66"
  end
  
  # VM 2
  config.vm.define "debian_vm-2" do |debian_config|
    debian_config.vm.box = "debian/bookworm64"
    # Network
    debian_config.vm.network "public_network", bridge: "ens33", ip: "192.168.30.67"
  end

end

Enable SSH Password Auth
#

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

  # Provisioning script to enable SSH password authentication
  config.vm.provision "shell", inline: <<-SHELL
    echo "vagrant:vagrant" | sudo chpasswd
    sudo sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config
    sudo systemctl restart sshd
  SHELL

  # VM 1
  config.vm.define "debian_vm-1" do |debian_config|
    debian_config.vm.box = "debian/bookworm64"
    # Network
    debian_config.vm.network "public_network", bridge: "ens33", ip: "192.168.30.66"
  end
  
  # VM 2
  config.vm.define "debian_vm-2" do |debian_config|
    debian_config.vm.box = "debian/bookworm64"
    # Network
    debian_config.vm.network "public_network", bridge: "ens33", ip: "192.168.30.67"
  end

end

More Settings
#

In this Vagrantfile the following modifications are done:

  • Install Apache2
  • Port forwarding
  • Host folder mapping
  • Define VM RAM
Vagrant.configure("2") do |config|

    # Provisioning script to enable SSH password authentication
    config.vm.provision "shell", inline: <<-SHELL
      echo "vagrant:vagrant" | sudo chpasswd
      sudo sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config
      sudo systemctl restart sshd
      apt-get update
      apt-get install -y apache2
    SHELL
  
    # VM 1
    config.vm.define "debian_vm-1" do |debian_config|
      debian_config.vm.box = "debian/bookworm64"
      # Network
      debian_config.vm.network "public_network", bridge: "ens33", ip: "192.168.30.66"
      debian_config.vm.network "forwarded_port", guest: 80, host: 8080
      # Shared folder
      debian_config.vm.synced_folder "host_folder/", "/vagrant_data"
      # VirtualBox settings
      debian_config.vm.provider "virtualbox" do |vb|
        vb.memory = "1024"  # Set VM's RAM to 1024 MB
      end
    end
    
    # VM 2
    config.vm.define "debian_vm-2" do |debian_config|
      debian_config.vm.box = "debian/bookworm64"
      # Network
      debian_config.vm.network "public_network", bridge: "ens33", ip: "192.168.30.67"
      debian_config.vm.network "forwarded_port", guest: 80, host: 8081
      # Shared folder
      debian_config.vm.synced_folder "host_folder/", "/vagrant_data"
      # VirtualBox settings
      debian_config.vm.provider "virtualbox" do |vb|
        vb.memory = "1024"  # Set VM's RAM to 1024 MB
      end
    end
  
  end

Commands
#

# Start all VMs
vagrant up

# Start specific VM
vagrant up vmname

# Stop specific VM
vagrant halt vmname
# SSH into specific VM
vagrant ssh vmname

Links #

# Find Vagrant Boxes
https://vagrantcloud.com/search

# Official Documentation
https://developer.hashicorp.com/vagrant/docs/boxes