Pages

Tuesday, June 3, 2014

How to create a CentOS 6.5 Vagrant box (with VirtualBox)

This guide is inspired from the Vagrant official documentation.

Versions

  • Vagrant: 1.6.2
  • VirtualBox: 4.3.12

Creation and configuration of the virtual machine

CentOS 6.5 installation

Launch VirtualBox and create a new VM with:
  • CPU: 1 core
  • Memory: 512 MB
  • Storage : 200 GB (dynamically allocated)
  • No audio, no USB

Start the VM and install CentOS 6.5. During the installation, set:

Root password: vagrant

Once the installation is done and that the VM rebooted, log in as root.

"vagrant" user

Create a user:
  • User: vagrant
  • Password: vagrant
Type:

useradd vagrant
passwd vagrant

Sudoers

To edit the /etc/sudoers file, type:

visudo

"requiretty" is not required by Vagrant :-)


Locate the line:

Defaults    requiretty

and comment it to have this:

# Defaults    requiretty

Secure path

When you use the sudo command, the PATH is changed to have only paths considered as trustworthy. This is the secure_path parameter. I personally like to have the /usr/local/bin in it, that's why I append it to the secure_path.

# add /usr/local/bin to $PATH when using sudo
# Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin
Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin

Add the "vagrant" user in the sudoers

Locate the lines:

## Same thing without a password
# %wheel        ALL=(ALL)       NOPASSWD: ALL

and uncomment the 2nd line to have this:

## Same thing without a password
%wheel        ALL=(ALL)       NOPASSWD: ALL

Add the user vagrant in the group wheel by typing:

usermod -aG wheel vagrant

From the --help:

  -G, --groups GROUPS           new list of supplementary GROUPS
  -a, --append                  append the user to the supplemental GROUPS
                                mentioned by the -G option without removing
                                him/her from other groups

Another way to add a user in a group is to type:

sudo gpasswd -a username group

You can now log out as root, and log back in as vagrant.

Enable network


Yeah, it's stupid, network is NOT enabled by default in CentOS.

Edit this file:

sudo vi /etc/sysconfig/network-scripts/ifcfg-eth0

and set the line:

ONBOOT=yes

Start the network service:

sudo service network start

Tip: To be able to copy/paste, you can log into your VM with ssh from your host machine. To do so, go to your VM configuration and set the network to Bridged Adapter.


Then, in your VM console, type:

sudo service network restart

You should have an IP address on your physical network (providing that you have a working DHCP server). Check your IP by typing:

ifconfig

Let's assume that your VM has the IP 192.168.0.213. You can now connect to your VM with ssh, by typing in a console on your host machine:

ssh vagrant@192.168.0.213

Install EPEL repository

EPEL (Extra Packages for Enterprise Linux) repository contains some VERY useful packages. I can hardly think about a CentOS server without it.

To install EPEL repository, type:

sudo yum install -y http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm

Required packages

I can hardly imagine a server without these packages:

sudo yum install -y wget man

Optional packages (requires EPEL repository)

I personally recommend to have these packages, but it depends on your needs/habits.

sudo yum install -y vim-enhanced screen htop sysstat git tig bash-completion tree

SSH insecure keypair


mkdir ~/.ssh
chmod 700 ~/.ssh
wget --no-check-certificate -O .ssh/authorized_keys https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/vagrant.pub
chmod 600 ~/.ssh/*

Set GRUB timeout to 0s

Doing so will make your Vagrant box to boot 5 seconds faster. Hey, time is money... ;-)

Edit the file /boot/grub/grub.conf and set the timeout to zero second:

timeout=0

Turn on colors for "tree"


Add this line at the end of /etc/bashrc:



alias tree='tree -C'

Install VirtualBox Guest Additions

Update your kernel and reboot:

sudo yum update -y kernel*
sudo reboot 

Install additional packages:

sudo yum install -y gcc kernel-devel perl

Insert the VirtualBox Guest Additions ISO using the VirtualBox GUI and install them:

sudo mkdir /media/VirtualBoxGuestAdditions
sudo mount -r /dev/cdrom /media/VirtualBoxGuestAdditions
cd /media/VirtualBoxGuestAdditions
sudo ./VBoxLinuxAdditions.run
cd
sudo umount /media/VirtualBoxGuestAdditions
sudo rm -r /media/VirtualBoxGuestAdditions


Remove the VirtualBox Guest Additions ISO using the VirtualBox GUI.

Final cleaning

sudo yum clean all

Issues

SSH connection is slow

There are various reasons why SSH connection establishing is slow. Generally, it is due to the reverse DNS to avoid some IP spoofing or something like that. Well, a simple way to get rid of it is to disable the GSS API Authentication in SSH configuration.

Edit /etc/ssh/sshd_config and set the parameter GSSAPIAuthentication to no:

GSSAPIAuthentication no

Restart the SSH server:

sudo service sshd restart

VirtualBox DNS resolution doesn't work well with new Ubuntu 12.04 LTS /etc/resolv.conf

Best solution

Source : http://askubuntu.com/questions/262510/dns-suddenly-broken-in-ubuntu-virtualbox

Type:

sudo dpkg-reconfigure resolvconf

and answer "Yes" to the questions.

Other solution (not as good)

Source : https://forums.virtualbox.org/viewtopic.php?f=26&t=50387

Well, I consider that the DNS issue should be resolved on the host side instead of tweaking the VMs, but well, having some choices is not that bad sometimes, so there is a tip that might help you.

In a host terminal, try this:

VBoxManage modifyvm "VM name" --natdnshostresolver1 on

Replace the "VM name" with the name of your VM and don't forget the double-quotes " ".

If it works, you can set it in your Vagrantfile.

Set it in Vagrantfile

Add this in your Vagrantfile:

config.vm.provider "virtualbox" do |vb|
 vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
end

No comments:

Post a Comment