A vagrant box is a pre-packaged virtual machine image that can be used with Vagrant to create and configure virtual development environments. A box typically includes an operating system and any additional software or configuration settings needed for a specific project or development environment. These boxes can be easily shared with other developers, allowing them to quickly set up a consistent development environment without having to manually install and configure the operating system and other software. Boxes can be found on public or private repository called Vagrant Cloud, or they can be created by the user or an organization. Once you have a box, you can use the vagrant init command to create a new Vagrant environment and configure it to use the box.
In order to create your own Vagrant box, you will need the following:
- Virtualization Software: In this case, virtual box manager
- Vagrant: You will need to have Vagrant installed on your computer.. Vagrant will packed your box with ‘vagrant package’
- ISO File: You need the operating system for a fresh vagrant box
Steps to create a vagrant.box
In this tutorial, I will show you how to create a very basic RedHat9 vagrant.box.
1) First, create a virtual machine with the following criteria (this is optional, but I like it minimum):
- Memory: 1024MB
- CPU: 1 Core
- Storage size: 10GB
- Hard disk file type: VMDK (Virtual Machine Disk)
- Storage on physical hard disk: Dynamically allocated
2) Don’t start the machine yet, disable these few “unusable” things:
- System > Motherboard > untick Floppy
- Storage > Select your OperatingSystem.iso
- Network (ensure network adapter 1 is set to ‘NAT’)> Click “advance” dropdown icon > Port Forwarding:
Name: SSH | Protocol: TCP | Host IP: <leave it blank> | Host Port: 2222 | Guest IP: <leave it blank> | Guest Port: 22
- USB > untick USB controller
3) For the OS Installation, please done the following:
- Installation Destination > Automatic Partitioning
- kdump > untick ‘enable kdump’
- Software Selection > Minimal Install
- Root Password: vagrant
Inside the Operating System
If vagrant user does not exist, you need to create one. Else, skip this step.
1. Create a vagrant user with following command:
$ useradd vagrant
2. Add user to the sudo group:
$ usermod -aG wheel vagrant
3. Verify vagrant user:
$ id vagrant
Setup super user
1. The most efficient way to setup vagrant is to eliminate password requests from the vagrant user, and edit the file:
$ sudo visudo -f /etc/sudoers.d/vagrant
2. Add this line for vagrant not requesting passed
# add vagrant user vagrant ALL=(ALL) NOPASSWD:ALL
Install vagrant insecure key
1. Run this command to setup ssh password
$ mkdir -p /home/vagrant/.ssh $ chmod 0700 /home/vagrant/.ssh $ wget –no-check-certificate https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub -O /home/vagrant/.ssh/authorized_keys $ chmod 0600 /home/vagrant/.ssh/authorized_keys $ chown -R vagrant /home/vagrant/.ssh
2. Edit the ssh config file
sudo vi /etc/ssh/sshd_config
Then, find and change the following line, we need to tell the SSH server that the location where the insecure key we’ve just downloaded is authorized for SSH access:
Restart ssh after you applied:
sudo systemctl restart sshd
Zero out the drive
This is the final step before you package it. This process involves compression and this process also fixes fragmentation issues with the underlying virtual disk, which compresses it much more efficiently.
sudo dd if=/dev/zero of=/EMPTY bs=1M sudo rm -f /EMPTY
Packaging the box
Proceed to this step if you are ready to package your system. Take note, this command needs to be run on the host, not on VM’s machine.
vagrant package -–base “Name of your VM's in VirtualBox” --output "Your packages name".box
Let it run for you. It takes time to package.
Testing the box
First, go to .box directory.
Once you inside the box directory, run this command:
vagrant box add "your packages name".box
vagrant will add your box to your local repository. Then you can create the vagrantfile with your own box. Edit the vagrantfile with your box name.
After that, try to SSH into the system you just created with the following command: