What is the Vagrant box? What do I need it for? We have Vagrant cloud for that.
Very well, there are several reasons why you might want to create your own Vagrant box:
- Customized environment: You may want to create a custom environment that includes specific software or configurations that are not available in existing boxes.
- Compliance: You may need to create a box that complies with specific security or regulatory requirements.
- Sharing: You may want to share a box with others in your organization or with the open-source community.
- Repeatability: Creating your own box can help ensure that the environment is consistently configured and that development and production environments are as similar as possible.
- Cost: Creating your own box can help save costs by avoiding the need to pay for expensive licenses for software that is already owned.
- Privacy: Creating your own box can help ensure that sensitive data is not shared with others.
- Performance: Creating a custom box can help optimize the performance of your development environment, by installing only the necessary dependencies and configure the environment to work better with the project.
Overall, creating your own Vagrant box can provide a great deal of flexibility and control over the development environment, allowing you to customize it to meet the specific needs of your project or organization.
What you really need is:
- Virtualization software: VirtualBox. You will need to have one of these installed on your computer.
- Vagrant: You will need to have Vagrant installed on your computer. Vagrant is available for Windows, Mac, and Linux.
- ISO file: You need an operating system.
- A text editor: You will need a text editor to create and edit the Vagrantfile, which is used to configure the box.
- Optionally, you will need access to the Internet to download any additional software or dependencies that are required for your project.
In this tutorial, I will show you how to create a RedHat9 vagrant box. (The very basic)
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
After you create, just don’t start the machine yet, disable these few “unusable” things:
- System > Motherboard > untick Floppy (who still uses floppy nowadays?)
- 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
That’s about it. Now Start the
On the RedHat Installation page, do this:
Under Installation Summary:
- Installation Destination > Automatic Partitioning
- kdump > untick ‘enable kdump’
- Software Selection > Minimal Install
Under User Settings:
- Root Password: vagrant
It will tell you the password is Weak. Toughen up, soldier! Just press ‘done’ twice.
Then “Begin Installation”. After the installation is complete, reboot the system.
Time to #Bash
After everything in place, now its time to follow the
Create vagrant user
Create a vagrant user as admin:
Adding vagrant user
Setting up user password
Place vagrant user in wheel group
usermod -aG wheel vagrant
Verify vagrant user
No password for vagrant user
The vagrant user must be able to run sudo commands without a password prompt, it is required to disable prompt password for the vagrant user every time you run sudo command (its just tiring thing)
sudo visudo -f /etc/sudoers.d/vagrant
With the file open, add this to the file.
# I hate to insert passwd (vagrant user add) vagrant ALL=(ALL) NOPASSWD:ALL
Ensure that it is working by typing this command:
The command should return you to the home directory without prompting you a password if everything has been setup right.
Install vagrant key
Between the host machine and guest machine. The only way for vagrant commands can communicate over SSH is to install “insecure vagrant key” to the guest machine. It called “insecure” because everyone can access to everyone’s vagrant box because of the same key applied.
p/s: vagrant boxes are not for production use, they are meant solely and purely for development only.
Create /.ssh directory
mkdir -p /home/vagrant/.ssh
Change file permission
chmod 700 /home/vagrant/.ssh
Install “insecure” key
wget –no-check-certificate https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub \ -O /home/vagrant/.ssh/authorized_keys
Change file permission after applied the insecure key
chmod 600 /home/vagrant/.ssh/authorized_keys
Change ownership to vagrant for /.ssh directory
chown -R vagrant /home/vagrant/.ssh
Edit ssh config file by running this command:
chown -R vagrant /home/vagrant/.ssh
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:
Dont forget to 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 you want it smaller don’t you? This process also fixes fragmentation issues with the underlying virtual disk, which compresses it much efficiently.
sudo dd if=/dev/zero of=/EMPTY bs=1M sudo rm -f /EMPTY
Packaging the box
Are you ready? 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, and sip the coffee…
Testing the .box
It will be a waste if you just make an awesome box but you didn’t test it. First, go to .box directory.
Once you inside the box directory, run this command:
vagrant box add "your desire box name" /path/to/your/box/whatever.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:
You made it! Now it’s time to run that box and do your development activity efficiently.
Well, if you didn’t make it, don’t worry. Just try again and learn what you’ve been missing in the above steps.
That’s about it!