Skip to content

Instantly share code, notes, and snippets.

@vancanhuit
Last active June 29, 2023 21:34
Show Gist options
  • Save vancanhuit/0d156e890e04be7d10f5430e746d201c to your computer and use it in GitHub Desktop.
Save vancanhuit/0d156e890e04be7d10f5430e746d201c to your computer and use it in GitHub Desktop.
Creating Vagrant box from scratch with VirtualBox

Creating a Vagrant box from scratch with VirtualBox

https://www.vagrantup.com/docs/providers/virtualbox/boxes

https://www.vagrantup.com/docs/boxes/base

  • Debian 10 OS:

    • User: vagrant.
    • Password: vagrant.
    • Root password: vagrant.
  • VM settings:

    • Memmory: 1GB.
    • Virtual disk: 20GB.
    • Audio and USB are disabled.
    • Network adapter 1 is NAT.
    • Port-forwarding rule: [Name: SSH, Protocol: TCP, Host Port: 2222, Guest Port: 22].
  • Configure password-less sudo:

    # Run as root user
    $ apt install sudo
    $ visudo

    Add the following line to the end of the file:

    vagrant ALL=(ALL:ALL) NOPASSWD: ALL
    

    https://www.digitalocean.com/community/tutorials/how-to-edit-the-sudoers-file

  • Update the OS and install necessary tools:

    $ sudo apt update && sudo apt upgrade -y
    $ sudo apt install curl wget git vim htop -y
  • Install VirtualBox Guest Additions:

    $ sudo apt install linux-headers-$(uname -r) build-essential dkms
    $ sudo mount /dev/cdrom /media/cdrom
    $ sudo /media/cdrom/VBoxLinuxAdditions.run
    $ sudo reboot

    https://www.virtualbox.org/manual/ch04.html#guestadd-install

  • Configure SSH:

    $ mkdir ~/.ssh
    $ chmod 0700 ~/.ssh
    $ wget https://raw.githubusercontent.com/hashicorp/vagrant/main/keys/vagrant.pub -O ~/.ssh/authorized_keys
    $ chmod 0600 ~/.ssh/authorized_keys
  • Cleanup and "zero out" the drive:

    $ sudo apt autoremove
    $ sudo apt autoclean
    $ sudo apt clean
    $ sudo dd if=/dev/zero of=/EMPTY bs=1M
    $ sudo rm -f /EMPTY
  • Package the base box:

    $ vagrant package --base <vbox-vm-name>
    $ vagrant box add debian10 package.box
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment