Skip to content

Instantly share code, notes, and snippets.

@elyezer
Last active August 29, 2015 13:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elyezer/10751019 to your computer and use it in GitHub Desktop.
Save elyezer/10751019 to your computer and use it in GitHub Desktop.
Guide to create a base CentOS/RHEL vagrant box

Create a new VirtualBox machine

  • 512 RAM
  • 40 GB VDI dynamically allocated hard drive
  • Disable audio
  • Disable USB controller

Installing the OS

Load the DVD image and install the system. By convention the root password is vagrant.

Then do some initial configuration:

# Bring up eth0 which is down by default
ifup eth0

# Install additional packages
yum install -y openssh-clients man git vim wget curl ntp

# Enable ntpd on boot
chkconfig ntpd on

# Enable sshd service to start on boot
chkconfig sshd on

# Disable iptables and ip6tables
chkconfig iptables off
chkconfig ip6tables off

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

Then configure the vagrant user:

useradd vagrant
mkdir -m 0700 -p /home/vagrant/.ssh
curl https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub >> /home/vagrant/.ssh/authorized_keys
chmod 600 /home/vagrant/.ssh/authorized_keys
chown -R vagrant:vagrant /home/vagrant/.ssh

Do some sudo configuration

sed -i 's/^\(Defaults.*requiretty\)/#\1/' /etc/sudoers
echo "vagrant ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers

Edit /etc/sysconfig/network-scripts/ifcfg-eth0 and make sure to have the following configuration:

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

Remove udev persisten rules:

rm -f /etc/udev/rules.d/70-persistent-net.rules

Do some clean up and shutdown the machine:

# Clean up yum
yum clean all

# Remove any temporary file or directory
rm -rf /tmp/*

# Clean up the logs
rm -f /var/log/wtmp /var/log/btmp

# Clean up the history
history -c

# Shutdown the machine
shutdown -h now

Finally remove the Controller: IDE (CD/DVD) on the storage tab in settings.

Exporting to a vagrant box

Execute the following command to export to a vagrant box:

vagrant package --output <output_name>.box --base <vm_name>

The vm_name must match the name you gave when creating the VirtualBox machine

Testing the base box

You could test the base box as follows:

# Add the base box
vagrant box add <desired_box_name> <output_name>.box

# Create a project and configure the Vagrantfile
mkdir -p ~/path/to/vagrant-test
cd ~/path/to/vagrant-test
vagrant init <desired_box_name>

As the base box does not have VBoxGuestAdditions disable the synced folder by adding the following line to your Vagrantfile:

config.vm.synced_folder ".", "/vagrant", disabled: true

Finally start using the machine

vagrant up

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment