lxc
跳转到导航
跳转到搜索
Linux Containers,相当于 BSD 的 jail 的操作系统级虚拟化方案。
权限相关
- 主系统中可以看到 containers 中的进程(但是进程 ID 不一致)
- 主系统可以给 containers 中的进程发信号
- 主系统可以连接 containers 中的 UNIX 域套接字
命令
根据模板创建一个 Ubuntu 容器:
sudo MIRROR=http://mirrors.163.com/ubuntu/ PATH=$PATH:/usr/sbin:/sbin:/bin lxc-create -n ubuntu -t ubuntu --lxcpath $PWD -- -a amd64 -r trusty
Debian stretch(需要安装 debootstrap):
sudo MIRROR=https://mirrors.tuna.tsinghua.edu.cn/debian/ PATH=$PATH:/usr/sbin:/sbin:/bin lxc-create -n lxc-debian -t debian -- -a amd64 -r stretch
配置
网络配置
# Networking lxc.net.0.type = veth lxc.net.0.link = br0 lxc.net.0.flags = up lxc.net.0.name = eth0 # avoid MTU issues with OpenVPN over PPPoE lxc.net.0.mtu = 1460 lxc.net.0.ipv4.address = 192.168.57.5/24 lxc.net.0.ipv4.gateway = 192.168.57.1
文件系统
lxc.mount.entry = tmpfs home/lilydjwg/tmpfs tmpfs rw,uid=1000,gid=1000,mode=0750 0 0 lxc.mount.entry = /home/lilydjwg/tmpfs host none bind 0 0 lxc.mount.entry = /home/lilydjwg/.cargo/registry home/lilydjwg/.cargo/registry none bind 0 0 lxc.mount.entry = /home/lilydjwg/.cargo/git home/lilydjwg/.cargo/git none bind 0 0
其它
lxc.tty = 1
问题处理
ssh 无法登录
auth 日志报错:
pam_loginuid(sshd:session): set_loginuid failed error: PAM: pam_open_session(): Cannot make/remove an entry for the specified session
解决方法:将 /etc/pam.d/sshd
中的
session required pam_loginuid.so
改为
session optional pam_loginuid.so
即可。[1]
OpenVPN 无法启动
预先创建 tun 设备即可。
lxc.hook.autodev = /var/lib/lxc/mktun
mktun
文件内容:
#!/bin/bash -e
cd "${LXC_ROOTFS_MOUNT}/dev"
mkdir -p net
[[ -c net/tun ]] || mknod net/tun c 10 200
chmod 0666 net/tun
参见
外部链接
- lxc 初体验 - 依云's Blog
- 利用 Aufs 和 LXC 快速建立一个用于测试的系统副本 - 依云's Blog
- Linux Certif - Man lxc.conf(5)
- 在 Arch 中安装 Arch - K.I.S.S. - 简单哲学
- lxc路由模式 | shell's home
- lxc的double NAT模式无法使用dnsmasq的分析 | shell's home
- Exploring LXC Networking - Container Ops
- OpenVPN in a lxc container - ah
USERNS
- VPN in containers | Stéphane Graber's website
- LXC 1.0: Unprivileged containers [7/10] | Stéphane Graber's website
- lxc container in user namespace / Networking, Server, and Protection / Arch Linux Forums
- 在用户命名空间中运行 LXC 虚拟机 - 依云's Blog
安全相关
- Evading from linux containers, 使用 sysfs 文件系统