Skip to content

How to Create a CentOS Vagrant Base Box

Gerard Ryan edited this page Jun 14, 2013 · 6 revisions

Tested with CentOS 6.2, Vagrant 1.0.2, VirtualBox 4.1.14 on Ubuntu 10.04 (s031).

I did locally on my laptop as you need the VirtualBox GUI and then copied the final centos62-32.box file up to s031.

1. Download and install VirtualBox

2. Download the CentOS 6.2 LiveCD

3. Make a new VM in VirtualBox:

  • Call it "vagrant-centos62-32"
  • Set OS to Linux and version to Red Hat
  • Go into the new VM's settings and disable audio and usb
4. Boot the VM and install CentOS 6.2 on it:
  • Set the root password to "vagrant":
  • For the non-root desktop user give username "vagrant" and password "vagrant"
  • Set the hostname to "vagrant-centos62"
5. Boot the VM into the CentOS, login to desktop as vagrant user, open terminal, su, install and enable openssh:
root@vagrant-centos62$ yum install openssh-server
root@vagrant-centos62$ service sshd start
root@vagrant-centos62$ chkconfig sshd on
root@vagrant-centos62$ netstat -tulpn | grep :22

6. Setup port forwarding in virtualbox. Shutdown the vm and from a host shell do:

you@host$ VBoxManage modifyvm "vagrant-centos62" --natpf1 "guestssh,tcp,,2222,,22"

7. Now you can ssh into the vm from a host shell:

you@host$ ssh -p 2222 root@127.0.0.1

8. Install some stuff on the VM:

root@vagrant-centos62$ yum install nano wget gcc bzip2 make kernel-devel-`uname -r`

9. Install guest additions, easiest way is using Devices -> Install Guest Additions in the VM's GUI window.

10. Add vagrant user to admin group, from a root shell on the vm:

root@vagrant-centos62$ groupadd admin
root@vagrant-centos62$ usermod -G admin vagrant

11. Change the sudoers file, from a root shell on the vm do visudo and:

  • Add SSH_AUTH_SOCK to the env_keep option
  • Comment out the Defaults requiretty line
  • Add the line %admin ALL=NOPASSWD: ALL
The vagrant user should now be able to sudo without typing a password, try sudo ls from vagrant user's shell:
vagrant@vagrant-centos62$ sudo ls

12. Add vagrant's public key so vagrant user can ssh without password. From vagrant user's shell on vm:

vagrant@vagrant-centos62$ mkdir .ssh
vagrant@vagrant-centos62$ curl -k https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub > .ssh/authorized_keys
vagrant@vagrant-centos62$ chmod 0700 .ssh
vagrant@vagrant-centos62$ chmod 0600 .ssh/authorized_keys

13. Enable the network interface to auto start on the Boot and get dynamic ip, provided by vagrant:

In file:

vagrant@vagrant-centos62$ vi /etc/sysconfig/network-scripts/ifcfg-eth0

Change:

ONBOOT=no

To parameters and values:

ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=dhcp

14. Finally package the box and add it to your vagrant boxes. From vagrant user's shell on vm:

vagrant@vagrant-centos62$ sudo yum clean all

Shutdown the vm, then from a host shell:

you@host$ vagrant package --output centos62-32.box --base vagrant-centos62-32
you@host$ vagrant box add centos62-32 centos62-32.box

Be sure the machine named after `--base` is a machine listed displayed in your Virtual Box. If you are creating a new vagrant box from an existing vagrant box, the name may be something like `<dirname></dirname>_1369964062`

You should now have a centos62-32 base box in your vagrant boxes:

you@host$ vagrant box list
centos62&#45;32

Now you can quickly create a Vagrant VM in any directory with:

you@host$ /tmp/my_vm $ vagrant init centos62&#45;32
you@host$ /tmp/my_vm $ vagrant up
you@host$ /tmp/my_vm $ vagrant ssh

To use the VM on a new host machine, copy the centos62&#45;32.box file to the new host and from the new host's shell run the vagrant box add command on it, now you should see the centos62-32 box in vagrant box list on the new host and you can use the box vagrant init centos62&#45;32.

Clone this wiki locally