AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 505067
Accepted
l0b0
l0b0
Asked: 2019-03-08 19:14:48 +0800 CST2019-03-08 19:14:48 +0800 CST 2019-03-08 19:14:48 +0800 CST

如何配置非特权 Linux 容器?

  • 772

我正在尝试设置非特权 LXC 容器,但每次都失败。我想我已经遵循了指南的每一个相关步骤:

  • 普通用户可以创建非特权容器:

    $ sysctl kernel.unprivileged_userns_clone
    kernel.unprivileged_userns_clone = 1
    
  • 控制组 PAM 模块已启用:

    $ grep -F pam_cgfs.so /etc/pam.d/system-login
    session optional pam_cgfs.so -c freezer,memory,name=systemd,unified
    
  • 设置了 UID 和 GID 映射:

    $ cat /etc/lxc/default.conf
    lxc.idmap = u 0 100000 65536
    lxc.idmap = g 0 100000 65536
    lxc.net.0.type = veth
    lxc.net.0.link = lxcbr0
    lxc.net.0.flags = up
    lxc.net.0.hwaddr = 00:16:3e:xx:xx:xx
    $ cat /etc/subuid
    root:100000:65536
    $ cat /etc/subgid
    root:100000:65536
    
  • 网络设置:

    $ grep --invert-match --regexp='^#' --regexp='^$' /etc/default/lxc-net
    USE_LXC_BRIDGE="true"
    LXC_BRIDGE="lxcbr0"
    LXC_ADDR="10.0.3.1"
    LXC_NETMASK="255.255.255.0"
    LXC_NETWORK="10.0.3.0/24"
    LXC_DHCP_RANGE="10.0.3.2,10.0.3.254"
    LXC_DHCP_MAX="253"
    
  • 服务看起来不错:

    $ systemctl status --lines=0 --no-pager lxc.service lxc-net.service 
    ● lxc.service - LXC Container Initialization and Autoboot Code
       Loaded: loaded (/usr/lib/systemd/system/lxc.service; disabled; vendor preset: disabled)
       Active: active (exited) since Fri 2019-03-08 15:31:47 NZDT; 40min ago
         Docs: man:lxc-autostart
               man:lxc
     Main PID: 4147 (code=exited, status=0/SUCCESS)
        Tasks: 0 (limit: 4915)
       Memory: 0B
       CGroup: /system.slice/lxc.service
    
    ● lxc-net.service - LXC network bridge setup
       Loaded: loaded (/usr/lib/systemd/system/lxc-net.service; enabled; vendor preset: disabled)
       Active: active (exited) since Fri 2019-03-08 15:31:45 NZDT; 40min ago
     Main PID: 4099 (code=exited, status=0/SUCCESS)
        Tasks: 1 (limit: 4915)
       Memory: 8.4M
       CGroup: /system.slice/lxc-net.service
               └─4121 dnsmasq -u dnsmasq --strict-order --bind-interfaces --pid-file=/run/lxc/dnsm…
    
  • 软件包是最新的,我刚刚重新启动。

即便如此,我也无法创建容器:

$ lxc-create -n test -t download
lxc-create: test: parse.c: lxc_file_for_each_line_mmap: 100 No such file or directory - Failed to open file "/home/user/.config/lxc/default.conf"
lxc-create: test: conf.c: chown_mapped_root: 3179 No uid mapping for container root
lxc-create: test: lxccontainer.c: do_storage_create: 1310 Error chowning "/home/user/.local/share/lxc/test/rootfs" to container root
lxc-create: test: conf.c: suggest_default_idmap: 4801 You do not have subuids or subgids allocated
lxc-create: test: conf.c: suggest_default_idmap: 4802 Unprivileged containers require subuids and subgids
lxc-create: test: lxccontainer.c: do_lxcapi_create: 1891 Failed to create (none) storage for test
lxc-create: test: tools/lxc_create.c: main: 327 Failed to create container test

这个设置有什么明显的问题吗?链接文章中没有任何地方提到 ~/.config/lxc/default.conf,我不明白为什么它说我没有分配 subuids 和 subgids。

附加信息:

  • 以 root 身份运行lxc-create是可行的,但这显然是关于以普通用户身份创建容器。
  • cp /etc/lxc/default.conf ~/.config/lxc/default.conf摆脱了对配置文件的抱怨,但结果却是这样的消息:

    lxc-create:playtime:conf.c:chown_mapped_root:3279 lxc-usernsexec 失败:没有这样的文件或目录 - 无法打开 tty 没有这样的文件或目录 - 无法打开 tt

arch-linux lxc
  • 3 3 个回答
  • 4352 Views

3 个回答

  • Voted
  1. Best Answer
    user73383
    2019-03-08T21:55:45+08:002019-03-08T21:55:45+08:00

    这是一个新项目,还是您有选择?为什么不使用 LXD 而不是 LXC - 使用起来更容易,而且你可以到达同一个地方。我从 lxc 开始并很快进行了切换,因为我对运行非特权容器感兴趣,这在 LXC 中并不容易,但在 LXD 中是默认的。

    看看这里开始: https ://discuss.linuxcontainers.org/t/comparing-lxd-vs-lxc/24

    我上次安装/使用它已经有几个月了,但这里是我的安装注意事项:

    随着 LXD 的快速发展,我们建议 Ubuntu 用户使用我们的 PPA:

    # add-apt-repository ppa:ubuntu-lxc/lxd-stable
    # apt-get update
    # apt-get dist-upgrade
    # apt-get install lxd     
    

    该软件包创建了一个新的“lxd”组,其中包含允许通过本地 unix 套接字与 lxd 对话的所有用户。“admin”和“sudoers”组的所有成员都会自动添加。如果您的用户不是这些组之一的成员,您需要手动将您的用户添加到该lxd 组。

    因为组成员资格仅在登录时应用,所以您要么需要关闭并重新打开您的用户会话,要么使用newgrp lxd 您要与 lxd 交互的 shell 中的命令。

    newgrp lxd
    

    https://blog.ubuntu.com/2015/03/20/installing-lxd-and-the-command-line-tool 2018/10/22


    据我所知,您甚至可以在虚拟机中运行 LXD,这样您就可以快速尝试一下,而不会弄乱您正在使用的任何系统。

    不完全是您提出的问题的答案,但我希望您发现它是一个有用的选择。

    • 0
  2. James Lockhart
    2019-08-14T04:46:04+08:002019-08-14T04:46:04+08:00

    我知道这是一篇旧帖子,但我最近遇到了同样的问题,这是通过遵循一个非常具体的错误来解决的:

    lxc-create: my-container: conf.c: suggest_default_idmap: 4738 You must either run as root, or define uid mappings
    lxc-create: my-container: conf.c: suggest_default_idmap: 4739 To pass uid mappings to lxc-create, you could create
    lxc-create: my-container: conf.c: suggest_default_idmap: 4740 ~/.config/lxc/default.conf:
    lxc-create: my-container: conf.c: suggest_default_idmap: 4741 lxc.include = /etc/lxc/default.conf
    lxc-create: my-container: conf.c: suggest_default_idmap: 4742 lxc.idmap = u 0 100000 65536
    lxc-create: my-container: conf.c: suggest_default_idmap: 4743 lxc.idmap = g 0 100000 65536
    lxc-create: my-container: lxccontainer.c: do_lxcapi_create: 1869 Failed to create (none) storage for my-container
    lxc-create: my-container: tools/lxc_create.c: main: 327 Failed to create container my-container
    

    这是一个很容易解决的问题:

    mkdir -p ~/.config/lxc
    touch ~/.config/lxc/default.conf
    echo "lxc.include = /etc/lxc/default.conf" >> ~/.config/lxc/default.conf
    echo "lxc.idmap = u 0 100000 65536" >> ~/.config/lxc/default.conf
    echo "lxc.idmap = g 0 100000 65536" >> ~/.config/lxc/default.conf
    

    记得更新用户和组 ID

    另外,如果您收到错误,请注意:

    Setting up the GPG keyring
    ERROR: Unable to fetch GPG key from keyserver
    

    正因为如此:https ://github.com/lxc/lxc/issues/3068

    要修复它,只需添加-- --keyserver hkp://p80.pool.sks-keyservers.net:80到您的创建命令的末尾,例如

    lxc-create -t download -n my-container -- --keyserver hkp://p80.pool.sks-keyservers.net:80
    

    希望这对其他人有帮助,如果它至少没有帮助你

    • 0
  3. swipon
    2022-09-26T23:21:27+08:002022-09-26T23:21:27+08:00

    我编写了一个简单的脚本来将经典的 LXC 容器特权转换为非特权:

    为 root 用户设置第二个 UID - GID (subuid guid):

    usermod --add-subuids 1258512-1324047 root
    usermod --add-subgids 1258512-1324047 root
    

    子集的替代手册(UID-GID):

    echo "root:1258512:65536" >> /etc/subuid
    echo "root:1258512:65536" >> /etc/subgid
    

    为非特权容器目录设置权限 注意:所有 UID 和 GID 都加上根 sub(uid-gid) 的偏移量

    subUid=$(grep root /etc/subuid | cut -d: -f2 | head -1) for i in $(find /var/lib/lxc/nameofcontainer/ -print) do
            ## Get numeric owner of current file/dir
            actProp=$(stat -c '%u:%g' "$i")
            actPropUid=$(echo ${actProp} | cut -d: -f1)
            actPropGid=$(echo ${actProp} | cut -d: -f2)
            ## Calculed shifted owner
            newUid=$(( $actPropUid + $subUid ))
            newGid=$(( $actPropGid + $subUid ))
            ## set new owner
            if [ $actPropUid -lt 65536 ] ; then
                    chown ${newUid}:${newGid} "$i"
            fi done 
    chown ${subUid}:${subUid} /var/lib/lxc/nameofcontainer/
    

    在 /var/lib/lxc/nameofcontainer/config 中设置启动非特权的选项:

    echo "lxc.include = /usr/share/lxc/config/debian.userns.conf" >> /var/lib/lxc/${CT}/config
    echo "lxc.idmap = u 0 $subUid 65536" >> /var/lib/lxc/${CT}/config
    echo "lxc.idmap = g 0 $subUid 65536" >> /var/lib/lxc/${CT}/config
    

    启用用户命名空间:

    echo "kernel.unprivileged_userns_clone=1" > /etc/sysctl.d/80-lxc-userns.conf
    sysctl --system
    

    更新 AppArmor LXC 的默认配置:

    cat <<EOF >> /etc/lxc/default.conf
    lxc.apparmor.allow_incomplete = 1
    EOF
    

    启用 cgroup 内核功能;编辑 /etc/default/grub 从

    GRUB_CMDLINE_LINUX_DEFAULT="quiet"
    

    至:

    GRUB_CMDLINE_LINUX_DEFAULT="cgroup_enable=memory swapaccount=1 quiet"
    

    这里有详细信息: https ://github.com/lucapiccio/LXC_to_Unprivileged/blob/main/convert.sh

    您可以在自述文件中找到所有需要的信息。

    • 0

相关问题

  • 为什么有时需要手动导入密钥?

  • 使用 LXC 2.x 将主机文件夹挂载到来宾,但它是只读的吗?

  • 无法在 Alpine Linux LXC 上启动 NFS 服务器

  • LXC 容器内部从零开始的 Linux

  • 在启动时加载设备

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve