Skip to content

How to install and manage OpenVZ on CentOS

Last updated on March 13, 2019

Installing OpenVZ on CentOS

Server preparation

$ sudo su

Update the list of packages:

# yum update -y

Install ntp to be able to synchronize time and wget for downloading files:

# yum install ntp wget -y

Set the time zone (choose your timezone) and synchronize it with the time server:

# cp /usr/share/zoneinfo/Europe/Chisinau/etc/localtime; ntpdate ru.pool.ntp.org

Create a task in cron to automatically synchronize the time every day at 00:00:

# mkdir -p /var/cron/tabs && echo '0 0 * * * /usr/sbin/ntpdate ru.pool.ntp.org' >> /var/cron/tabs/crontab && crontab /var/cron/ tabs/crontab && crontab -l

Setting up the system for the correct operation of OpenVZ
Edit the system sysctl file to run CentOS with the parameters optimal for OpenVZ:

# nano /etc/sysctl.conf
net.ipv4.ip_forward = 1
net.ipv6.conf.default.forwarding = 1
net.ipv6.conf.all.forwarding = 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

Disable SELINUX:

# echo "SELINUX = disabled"> /etc/sysconfig/selinux

To make the network work in a virtual machine, we clean the FORWARD chain in iptables and save the settings:

# iptables -F FORWARD && service iptables save

Since there are no necessary packages in the standard repositories, we will connect an additional one. To do this, enter the following two commands:

# wget -P /etc/yum.repos.d/http://ftp.openvz.org/openvz.repo

# rpm --import http://ftp.openvz.org/RPM-GPG-Key-OpenVZ

Install OpenVZ:

# yum install vzkernel -y

Also install utilities to manage our virtualization system:

# yum install vzctl vzquota ploop -y

Creating a virtual machine (container)
Create a container with the number 128:

# vzctl create 128 --config basic

We configure the network to auto-on at startup:

# vzctl set 128 --onboot yes --save

Set the containers name:

# vzctl set 128 --hostname vps128.local - save

Assign the IP address 192.168.1.10 to the container being created:

# vzctl set 128 --ipdel all --ipadd 192.168.1.10 - save

We set a name server (DNS) to enable the container to go online and interact with the corporate network:

# vzctl set 128 --nameserver 192.168.2.1 - save

Set the password for the vm’s root account:

# vzctl set 128 --userpasswd root:password --save

You can limit CPU usage by 15 percent, if you want:

# vzctl set 128 --cpulimit 15 --save

Set the disk quota:

# vzctl set 128 --diskspace 9000000:10000000 --save

Set a delay for exceeding the limit (grace period):

# vzctl set 128 --quotatime 600 --save

Determine the maximum amount of RAM for the container:

# vzctl set 128 --privvmpages 750M:768M - save

Set the guaranteed amount of memory that the virtual machine will receive:

# vzctl set 128 --vmguarpages 250M:256M - save

Using this command, start the created container:

# vzctl start 128

Frequently used commands to control OpenVZ
* 128 – container number. For each virtual machine it has its own.

Running the container:

# vzctl start 128

Container stop:

# vzctl stop 128

Restart the container:

# vzctl restart 128

Installing the packages in the container:

# vzyum 128 install packagename*

Removing package from a virtual machine:

# vzyum 128 remove samba

Run a command in a container (in this example, update the list of repo’s):

# vzctl exec 128 yum update -y

List all the installed containers:

# vzlist -a

Check containers status:

# vzctl status 128
Published inLinux