如何在无特权的LXC容器内创build/ dev / tun设备?

这个问题与openvpn的lxc guest中的No tun设备类似。 LXC已经发展起来,最近推出了无特权的LXC容器,为打破监狱提供了另一层安全保障。

我需要在一个非特权容器内创build一个OpenVPN服务器。 我不知道如何让容器创build一个私有的tunnetworking设备。

我确实追加了lxc.cgroup.devices.allow = c 10:200 rwm~/.local/share/lxc/mylxc/config

启动容器后, mknod /dev/net/tun c 10 200返回mknod: '/dev/net/tun': Operation not permitted容器内部mknod: '/dev/net/tun': Operation not permitted

我使用一个香草Ubuntu 14.04 64位作为主机和一个容器创build

 lxc-create -t download -n mylxc -- -d ubuntu -r trusty -a amd64 

有没有人设法让/dev/tun设备在非特权LXC下运行?

您需要明确地将CAP_MKNOD function添加到您的容器 。

  lxc.cap.keep Specify the capability to be kept in the container. All other capabilities will be dropped. When a special value of "none" is encountered, lxc will clear any keep capabilities specified up to this point. A value of "none" alone can be used to drop all capabilities. 

你也可以尝试自动化(如果你碰巧在容器中使用systemd ),使用:

  lxc.hook.autodev A hook to be run in the container's namespace after mounting has been done and after any mount hooks have run, but before the pivot_root, if lxc.autodev == 1. The purpose of this hook is to assist in populating the /dev directory of the container when using the autodev option for systemd based containers. The container's /dev directory is relative to the ${LXC_ROOTFS_MOUNT} environment variable available when the hook is run. 

它可以指向一个运行mknod的脚本。

使用docker这是很容易完成的。 默认情况下,容器是非特权的 。

在这个例子中,我从registry中提取一个trusty容器:

 sudo -r sysadm_r docker pull corbinu/docker-trusty Pulling repository corbinu/docker-trusty ... Status: Downloaded newer image for corbinu/docker-trusty:latest 

我以交互模式启动它,通知我需要的function:

 sudo -r sysadm_r docker run --cap-drop ALL --cap-add MKNOD \ -i -t corbinu/docker-trusty bash root@46bbb43095ec:/# ls /dev/ console fd/ full fuse kcore mqueue/ null ptmx pts/ random shm/ stderr stdin stdout tty urandom zero root@46bbb43095ec:/# mkdir /dev/net root@46bbb43095ec:/# mknod /dev/net/tun c 10 200 root@46bbb43095ec:/# ls -lrt /dev/net/tun crw-r--r--. 1 root root 10, 200 Apr 6 16:52 /dev/net/tun 

而不是:

 sudo -r sysadm_r docker run --cap-drop ALL \ -i -t corbinu/docker-trusty bash root@9a4cdc75a5ec:/# mkdir /dev/net root@9a4cdc75a5ec:/# mknod /dev/net/tun c 10 200 mknod: '/dev/net/tun': Operation not permitted