您需要将CAP_MKNOD 功能显式添加到您的容器中。
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
它非常容易完成。默认情况下,容器是无特权的。
在此示例中,我trusty
从注册表中提取了一个容器:
sudo -r sysadm_r docker pull corbinu/docker-trusty
Pulling repository corbinu/docker-trusty
...
Status: Downloaded newer image for corbinu/docker-trusty:latest
我以交互模式启动它,以告知我所需的功能:
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
lxc.mount.entry = /dev/net/tun dev/net/tun none bind,create=file
到容器配置文件中,如下所述:superuser.com/a/1205662/130915然后,我以root身份在容器内部使用sudo运行openvpn。