How to create a CentOS 7 Vagrant Box

N.B. All commands in this tutorial have been run as the root user so if you are going to use this to create a box that will be used in a production environment please know that you must properly adjust your permissions and groups accordingly for it to actually work therefore I do not advise using this tutorial to create a production box.

The link below is an article that helped me when I began to learn about creating Vagrant boxes but I am not following it exactly as written because it took additional resources and some trial and error to get going down the right path during my initial attempts: CentOS 7 Vagrant base box how-to

The first order of business as you can expect is that you need to obtain a copy of CentOS 7 so that you can install and I recommend the CentOS 7 minimal install iso because the other images are overkill and one of the main goals is to keep the size of the final box small but you are free to choose a different image and the installation instructions will be very similar if not identical. Next is actually installing the image on your VirtualBox program running on your physical machine. Be sure to name it something simple that is easy for you to type so that you don't have possibly catastrophic issues (like i did... yikes!) when you type the command to pass the file's name to Vagrant to package the box after the installation and configuration steps are completed. In the settings select Linux Red Hat 64-bit and assign 512mb ram and 1-2 cpu(s) running on a dynamically allocated vdmk virtual hard drive with 30-40gb of storage. Next add an optical disk drive to the IDE controller then mount the downloaded CentOS 7 iso file onto it. Continue the install on VirtualBox by following this link: How to install CentOS 7

Make sure the root password is vagrant and that a user with administrator privileges is created with the username: vagrant and also set the password to: vagrant. After the installation on VirtualBox is finished log into the root user account and update the installed packages by running the following command:

# yum -y update

I usually update the system kernel to the most recent mainline version following initial installation and after updating via the yum package manager. This can be accomplished by running the following set of commands and I will explain each one. First, run this to install a plugin for yum that shortens your package installation time by finding the fastest repository mirror to down load package files from so it is worth having installed. It is normally installed by default even on the minimal CentOS 7 installation images but you should run the command just to make sure as it won't hurt anything and it only costs a few seconds.

# yum -y install yum-plugin-fastestmirror

The next command we will run returns information on the linux distribution and kernel version to the terminal.

# uname -msr

Run this next command to import the GPG encryption key for the el repo repository:

# rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org

The next command uses the Red Hat package manager (rpm) to download and install the el repo repository .rpm file allowing us to now use yum to install the updated kernel package with dependencies.

# rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-2.el7.elrepo.noarch.rpm

Running this will display yum's list of repositories.

# yum repolist

This next command will install the updated mainline kernel using the enablerepo flag to enable the elrepo repository

# yum --enablerepo=elrepo-kernel install kernel-ml

Run these commands to set grub to boot using the new kernel.

# grub2-set-default 0
# grub2-mkconfig -o /boot/grub2/grub.cfg

Running this next command will remove old repository files.

# reboot yum install yum-utils package-cleanup --oldkernels

Now we want to install a few packages that we will use shortly as well as configure ntpd to run on startup.

# yum install -y openssh-clients nano wget ntp curl # chkconfig ntpd on

Now we will run this command to stop ntpd.

# service ntpd stop

Now we sync the time to nist then start ntpd back up again.

# ntpdate time.nist.gov
# service ntpd start

Now we need to run this command to set SELINUX to permissive using sed to edit the config file.

# sed -i -e 's/^SELINUX=.*/SELINUX=permissive/' /etc/selinux/config

Now we need to create a user named vagrant as well as create a directory for our ssh keys.

# useradd vagrant    
# mkdir /home/vagrant/.ssh
# touch/home/vagrant/.ssh/authorized_keys

This next command will change ownership of the repository we will use for our ssh keys over to the vagrant user and group.

# chown -R vagrant:vagrant /home/vagrant/.ssh

Running this next command will use sed to edit the sudoers file in the /etc directory. Then subsequently we will append the vagrant password credentials to the sudoers file.

# sed -i 's/^\(Defaults.*requiretty\)/#\U/' /etc/sudoers 
# echo "vagrant ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
# vim /boot/grub2/grub.cfg

Find the line that states menu entry, find the vmlinuz line and add “net.ifnames=0 after the word “quiet”. The link below has more information as this seemed a bit tricky for me the first time I tried switching the network interface to eth0.

How to change the default network interface name

Now you must reboot your operating system which can simply be done with the reboot command as follows:

# reboot

Next we will run the following commands to copy and rename the interface configuration file under the eth0 name instead of enp0s3

# mv /etc/sysconfig/network-scripts/ifcfg-enp0s3 /etc/sysconfig/network-scripts/ifcfg-eth0
# vim /etc/sysconfig/network-scripts/ifcfg-eth0vm 

configure network by running:

# vim /etc/sysconfig/network-scripts/ifcfg-eth0 

edit this file as such:

DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=no
BOOTPROTO=dhcp

# reboot
# rm -f /etc/udev/rules.d/70-persistent-net.rules
# yum update 

Then, install RVM to manage Ruby and to install Ruby on Rails.

Ruby Version Manager

Next, install Node.js from source since the CentOS repositories only support Node.js version six but Heroku needs version eight or higher. I have posted a tutorial from DigialOcean.

Node.js on CentOS 7 tutorial

Go to nodejs.org and choose the version you want to install and download the tar.gz file.

Node 10.4.1
Run the following commands install build tools and ultimately Heroku, Ansible, and Maven.

# tar -xzvf node-v* 
# cd node-v* 
# yum install gcc gcc-c++ 
# ./configure 
# make 
# make install 
# npm install -g heroku install 

Follow these instructions to install Docker: Docker Install Docs

Install Ansible by running the following commands:

# yum install ansible 

Install Maven by following the instructions at the following link. Install Maven

Optionally you can install guest additions by following the instructions in the link below. Install Guest Additions

Now that we have finished installing all of the software that we want on CentOS we need to run this command substituting "my-virtual-machine" for the name of the your actual CentOS 7 virtual machine.

# vagrant package --base my-virtual-machine

This will output your completed base box as a file named package.box and you are almost ready to run this next command to start up your Vagrant box for use after editing your vagrantfile for your specific provider configuration.

# vagrant up --provider=vmware_fusion

Your CentOS 7 machine should now be running on your computer ready for you to ssh into for use.