Creating a Vagrant.box

Introduction

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.

Prerequisite

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.

Let’s Start!

In this tutorial, I will show you how to create a RedHat9 vagrant box. (The very basic)

VM.Config


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 Engine! Machine.

OS Setup

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 my commands:

Create vagrant user

Create a vagrant user as admin:

Adding vagrant user

useradd vagrant

Setting up user password

passwd vagrant

Place vagrant user in wheel group

usermod -aG wheel vagrant

Verify vagrant user

id vagrant

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:

sudo pwd

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

If you do not have the wget package installed, you can do so with curl, just rename and place it at the correct path

curl -OJ https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub

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

Configure 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:

AuthorizedKeysFile %h/.ssh/authorized_keys

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.

vagrant init

After that, try to SSH into the system you just created with the following command:

vagrant ssh

Congratulation

If Success,
You made it! Now it’s time to run that box and do your development activity efficiently.
Else,
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!

Advertisment
Advertisment
Featured Post

Unleash Windows 10 OpenSSH Setup: A Joyful Guide

SSH (Secure Shell) is the key to secure remote access, and Windows 10’s OpenSSH makes it effortless. In this guide, we simplify the setup, so whether you’re tech-savvy or not, you can unlock the power of OpenSSH. Get ready to enhance your Windows 10 experience and enjoy seamless, secure remote connections. Let’s dive in!

Read More »

Share this post