CentOS Virtualization with OpenVZ

OpenVZ is an open source container-based virtualization platform that allows you to run several virtual machine instances on a single piece of hardware. It’s the open source base of Parallels Server Bare Metal (formerly Virtuozzo) which is an enterprise software package used by many hosting providers offering virtual private servers or (VPS).

OpenVZ has almost no overhead because the virtual servers use the host system’s kernel instead of its own kernel on top of emulated hardware. You get all the benefits of isolating machine instances without adding another layer of abstraction between your OS and the bare-metal.

OpenVZ is a command line tool that is quick and easy to install, but there are several browser based control panels for it. Some free such as OpenVZ Web Panel, some commercial.

Installing and using OpenVZ on CentOS 6

OpenVZ is not in the EPEL repo like a lot of the software and tutorials you find here are, but fortunately they have their own repo, and you can wget the .repo file directly from them.

By default the openvz.repo has the RHEL5/CENT5 repo enabled and RHEL6/CENT6 disabled, so we want to edit the file and switch that (because this tutorial is for CentOS6). This is no longer the case

vi /etc/yum.repos.d/openvz.repo

Change enabled=1 to enabled=0 under openvz-kernel-rhel5
Change enabled=0 to enabled=1 under openvz-kernel-rhel6

Install the software

yum -y install vzkernel vzctl vzquota
# also needs ploop
yum -y install ploop

Need to change a few things in /etc/sysctl.conf

net.ipv4.ip_forward = 1
net.ipv4.conf.default.proxy_arp = 0
net.ipv4.conf.all.rp_filter = 1
kernel.sysrq = 1
net.ipv4.conf.default.send_redirects = 1
net.ipv4.conf.all.send_redirects = 0

Now make the changes to sysctl take affect:

sysctl -p

Now make sure SELinux is disabled because it likes to interfere with things.

sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux

Reboot the server to load the new kernel.


When the machine comes back up, login and ensure it loaded the correct kernel with uname.


Now that we have the OpenVZ kernel and software installed, lets download an OS image template from OpenVZ. You can choose from a number of distros there, but since this is a CentOS tutorial, lets download the CentOS6 image. The templates belong in /vz/template/cache/ folder. This step can be omitted if using one of the base default templates available on pre-created.

Now that we’ve got the template, lets create a virtual machine from it, we’ll give it a CID of 201, this is a unique identifier and give it the basic config. I typically match the CID with the last octet of the IP address to make things easier for me to remember but it’s not a requirement.

vzctl create 201 --ostemplate centos-6-x86 --config basic

Set a hostname, IP address, DNS servers

vzctl set 201 --hostname --save
vzctl set 201 --ipadd --save
vzctl set 201 --nameserver --save

Now set the virtual to start automatically when the machine boots, start it, and set a root password.

vzctl set 201 --onboot yes --save
vzctl start 201
vzctl exec 201 passwd

You can enter the new virtual container by typing

vzctl enter 201

Then leave the container by typing


Other useful OpenVZ commands:
vzctl stop 201 # stop a virtual
vzctl restart 201 # restart a virtual
vzctl destroy 201 # delete a virtual from disk
vzlist # show a list of running virtuals
vzlist -a # show a list of all virtuals running or stopped

My /etc/sysctl looks like this:

My /boot/grub/menu.lst looks like this:

Other stuff:
If you want to run virtual containers that are on a different subnet than the host machine, you need to edit /etc/vz/vz.conf and set NEIGHBOUR_DEVS=all.

To allow a container to connect to an nfs server and mount a partition, you must execute the following command from the OpenVZ server:

vzctl set CTID --features "nfs:on" --save

For more information check out the OpenVZ NFS documentation page.