Enable tun for OpenVZ Ubuntu 12.04 containers in CentOS 6.4

2013-08-10
#tun #server #openvz #vpn #ubuntu #centos

To enable the tun device in OpenVZ containers, the tun module

  • needs to be loaded in the host system (in my case CentOS 6.4)
  • the container (in my case Ubuntu Server 12.04) must be allowed to use tun

I’ve grabbed the container configuration from askubuntu.

§ Load tun after boot automatically

Edit tun.modules:

1# vim /etc/sysconfig/modules/tun.modules
2#!/bin/sh
3/sbin/modprobe tun

Set permissions for tun.modules:

1chmod 755 /etc/sysconfig/modules/tun.modules

Reboot the host system and test that the tun module is loaded:

1$ lsmod | grep tun
2tun                    15493  2

§ Edit container configuration

Issue the following commands in the host terminal as root (change ‘101’ to your container ID):

1vzctl set 101 --devnodes net/tun:rw --save
2vzctl set 101 --devices c:10:200:rw --save
3vzctl set 101 --capability net_admin:on --save
4vzctl exec 101 mkdir -p /dev/net
5vzctl exec 101 mknod /dev/net/tun c 10 200
6vzctl exec 101 chmod 600 /dev/net/tun

Now configured OpenVPN will work correctly.