在无根环境中的基本设置和使用Podman

本文详细介绍了在无root权限环境下如何配置和使用Podman,包括cgroup V2设置、slirp4netns和fuse-overlayfs的安装、/etc/subuid和/etc/subgid的配置、用户配置文件如container.conf、storage.conf和registries.conf的修改,以及授权文件、卷的使用和端口映射等问题。无根用户在运行Podman时需要注意容器的IP、文件权限和端口映射等事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在无根环境中的基本设置和使用Podman

用户操作

在允许没有root特权的用户运行Podman之前,管理员必须安装或构建Podman并完成以下配置:

cgroup V2Linux内核功能允许用户限制普通用户容器可以使用的资源,如果使用cgroupV2启用了运行Podman的Linux发行版,则可能需要更改默认的OCI运行时。某些较旧的版本runc不适用于cgroupV2,必须切换到备用OCI运行时crun。

[root@slave1 ~]# yum -y install crun
[root@slave1 ~]# vim /usr/share/containers/containers.conf
# Default OCI runtime
#
runtime = "crun"
#runtime = "runc"  //取消crun注释并将runc注释掉
[root@slave1 ~]# podman run -d --name web -p 80:80 docker.io/library/httpd 
514355e5e1269be07ba0783c5ae0c81435193e3c86c2767b5c32b2ad7405078b
[root@slave1 ~]# podman inspect web |grep crun
        "OCIRuntime": "crun",
            "crun",

安装slirp4netns和fuse-overlayfs

在普通用户环境中使用Podman时,建议使用fuse-overlayfs而不是VFS文件系统,至少需要版本0.7.6。现在新版本默认就是了。

[root@slave1 ~]# yum -y install slirp4netns
[root@slave1 ~]# yum -y install fuse-overlayfs
[root@slave1 ~]# vim /etc/containers/storage.conf
# directly.
mount_program = "/usr/bin/fuse-overlayfs"   //取消注释

/ etc / subuid和/ etc / subgid配置

Podman要求运行它的用户在/ etc / subuid和/ etc / subgid文件中列出一系列UID,shadow-utils或newuid包提供这些文件

[root@slave1 ~]# yum -y install shadow-utils
可以在/ etc / subuid和/ etc / subgid查看,每个用户的值必须唯一且没有任何重叠。
[root@slave1 ~]# useradd zz
[root@slave1 ~]# cat /etc/subuid
zz:427680:65536
[root@slave1 ~]# cat /etc/subgid
zz:427680:65536
// 启动非特权ping 
[root@slave1 ~]# vim /etc/sysctl.conf
# For more information, see sysctl.conf(5) and sysctl.d(5).
#
net.ipv4.ping_group_range=0 200000   //大于100000这个就表示用户可以操作podman  

这个文件的格式是 USERNAME:UID:RANGE中/etc/passwd或输出中列出的用户名getpwent。

  • 为用户分配的初始 UID。
  • 为用户分配的 UID 范围的大小。

该usermod程序可用于为用户分配 UID 和 GID,而不是直接更新文件。

[root@slave1 ~]# usermod --del-subuids 427680-493216 --del-subgids 427680-493216 zz
[root@slave1 ~]# usermod --add-subuids 200000-201000 --add-subgids 200000-201000 zz  
[root@slave1 ~]# grep zz /etc/subuid /etc/subgid
/etc/subuid:zz:200000:1001
/etc/subgid:zz:200000:1001

用户配置文件

三个主要的配置文件是container.conf、storage.conf和registries.conf。用户可以根据需要修改这些文件。

container.conf

// 用户配置文件
[root@slave1 ~]# cat /usr/share/containers/containers.conf
[root@slave1 ~]# cat /etc/containers/containers.conf
[root@slave1 ~]# cat ~/.config/containers/containers.conf //优先级最高

如果它们以该顺序存在。每个文件都可以覆盖特定字段的前一个文件。

配置storage.conf文件

1./etc/containers/storage.conf
2.$HOME/.config/containers/storage.conf

在普通用户中/etc/containers/storage.conf的一些字段将被忽略

[root@slave1 ~]# vim /etc/containers/storage.conf
# Default Storage Driver, Must be set for proper operation.
driver = "overlay"    #此处改为overlay
.......
mount_program = "/usr/bin/fuse-overlayfs"    #取消注释
[root@slave1 ~]# sysctl user.max_user_namespaces=15000
user.max_user_namespaces = 15000

在普通用户中这些字段默认

graphroot=“ H O M E / . l o c a l / s h a r e / c o n t a i n e r s / s t o r a g e " r u n r o o t = " HOME/.local/share/containers/storage" runroot=" HOME/.local/share/containers/storage"runroot="XDG_RUNTIME_DIR/containers”

registries.conf

配置按此顺序读入,这些文件不是默认创建的,可以从/usr/share/containers或复制文件/etc/containers并进行修改。

1./etc/containers/registries.conf
2./etc/containers/registries.d/*
3.HOME/.config/containers/registries.conf

授权文件

此文件里面写了docker账号的密码,以加密方式显示

[root@slave1 ~]# podman login
Authenticating with existing credentials for docker.io
Existing credentials are valid. Already logged in to docker.io
[root@slave1 ~]# cat /run/user/0/containers/auth.json
{
	"auths": {
		"master.example.com": {
			"auth": "YWRtaW46SGFyYm9yMTIzNDU="
		}
	}

普通用户是无法看见root用户的镜像的

//root用户
[root@slave1 ~]# podman images
REPOSITORY                         TAG         IMAGE ID      CREATED        SIZE
docker.io/library/httpd            latest      dabbfbe0c57b  7 months ago   148 MB
docker.io/library/alpine           latest      c059bfaa849c  8 months ago   5.87 MB
master.example.com/library/alpine  latest      c059bfaa849c  8 months ago   5.87 MB
docker.io/library/registry         latest      b8604a3fe854  9 months ago   26.8 MB
quay.io/centos/centos              latest      300e315adb2f  20 months ago  217 MB
//普通用户
[root@slave1 ~]# su - zz
[zz@slave1 ~]$ podman images
REPOSITORY  TAG         IMAGE ID    CREATED     SIZE

无根用户创建的容器是没有IP的

[zz@slave1 ~]$ podman run -dit --name wbe -p 8080:80 httpd
✔ docker.io/library/httpd:latest
Trying to pull docker.io/library/httpd:latest...
Getting image source signatures
Copying blob 80e368ef21fc done  
Copying blob 4340e7be3d7f done  
Copying blob 1efc276f4ff9 done  
Copying blob 80cb79a80bbe done  
Copying blob aed046121ed8 done  
Copying config f2a976f932 done  
Writing manifest to image destination
Storing signatures
719ab2dd275d65c20131d0b4f5a40bd3f38179f62509c363999f12ea9b8775a
[zz@slave1 ~]$ podman inspect -l |grep -i address
            "IPAddress": "",
            "GlobalIPv6Address": "",
            "MacAddress": "",
            "LinkLocalIPv6Address": "",
            [zz@slave1 ~]$ podman exec -it wbe /bin/bash
root@719ab2dd275d:/usr/local/apache2# cd htdocs/
root@719ab2dd275d:/usr/local/apache2/htdocs# echo "hello" > index.html
root@719ab2dd275d:/usr/local/apache2/htdocs# exit
exit
[zz@slave1 ~]$ curl 192.168.58.135:8080
hello

  • 容器与root用户一起运行,则root容器中的用户实际上就是主机上的用户。
  • UID GID是在/etc/subuid和/etc/subgid等中用户映射中指定的第一个UID GID。
  • 如果普通用户的身份从主机目录挂载到容器中,并在该目录中以根用户身份创建文件,则会看到它实际上是你的用户在主机上拥有的。

使用卷

[zz@slave1 ~]$ podman run -dit --name web -v /home/zz/aa/:/adb -p 8080:80 httpd
ee28f1a9cf19745d2403c5106ae3ce485e10d7220507ced613c5cca65a7f2471
[zz@slave1 ~]$ podman ps
CONTAINER ID  IMAGE                           COMMAND           CREATED        STATUS            PORTS                 NAMES
ee28f1a9cf19  docker.io/library/httpd:latest  httpd-foreground  8 seconds ago  Up 9 seconds ago  0.0.0.0:8080->80/tcp  web
[zz@slave1 ~]$ podman exec -it web /bin/sh
# ls /
adb  boot  etc	 lib	media  opt   root  sbin  sys  usr
bin  dev   home  lib64	mnt    proc  run   srv	 tmp  var
# touch /adb/123
# ls -l
total 32
drwxr-xr-x 2 root root  276 Aug  2 04:35 bin
drwxr-xr-x 2 root root  167 Aug  2 04:35 build
drwxr-xr-x 2 root root   78 Aug  2 04:35 cgi-bin
drwxr-xr-x 4 root root   84 Aug  2 04:35 conf
drwxr-xr-x 3 root root 4096 Aug  2 04:35 error
drwxr-xr-x 2 root root   24 Aug  2 04:35 htdocs
drwxr-xr-x 3 root root 8192 Aug  2 04:35 icons
drwxr-xr-x 2 root root 4096 Aug  2 04:35 include
drwxr-xr-x 1 root root   23 Aug 17 03:11 logs
drwxr-xr-x 2 root root 8192 Aug  2 04:35 modules
//在主机查看
[root@slave1 ~]# ll /home/zz/aa/
total 0
-rw-r--r-- 1 zz zz 0 Aug 17 11:12 123
[root@slave1 ~]# echo "hello" >> /home/zz/aa/123
[root@slave1 ~]# cat /home/zz/aa/123 
hello
//在容器查看
# cat /adb/123
hello

使用普通用户映射容器端口时会报“ permission denied”的错误

[zz@slave1 ~]$ podman run  -d -p 80:80 httpd
Error: rootlessport cannot expose privileged port 80, you can add 'net.ipv4.ip_unprivileged_port_start=80' to /etc/sysctl.conf (currently 1024), or choose a larger port number (>= 1024): listen tcp 0.0.0.0:80: bind: permission denied

普通用户可以映射>= 1024的端口

[zz@slave1 ~]$ podman run  -d -p 1024:80 httpd
e8dd2473cedf573b2aedec211869f2dcd0a1eb32f48717d603459cffb9841b17
[zz@slave1 ~]$ ss -anlt
State  Recv-Q  Send-Q   Local Address:Port   Peer Address:Port Process                                                        
LISTEN 0       128            0.0.0.0:80          0.0.0.0:*                                                                   
LISTEN 0       128            0.0.0.0:22          0.0.0.0:*                                                                   
LISTEN 0       128            0.0.0.0:5000        0.0.0.0:*                                                                   
LISTEN 0       128                  *:8080              *:*                                                                   
LISTEN 0       128               [::]:22             [::]:*                                                                   
LISTEN 0       128                  *:1024              *:*                                                                   
LISTEN 0       80                   *:3306              *:* 

配置echo ‘net.ipv4.ip_unprivileged_port_start=80’ >> /etc/sysctl.conf后可以映射大于等于80的端口

[root@slave1 ~]# vim /etc/sysctl.conf
net.ipv4.ip_unprivileged_port_start=80
[root@slave1 ~]# sysctl -p
net.ipv4.ping_group_range = 0 200000
net.ipv4.ip_unprivileged_port_start = 80
[zz@slave1 ~]$ podman run -d -p 81:81 httpd
09d8226432f532405f04ce21111e8cfa64de5a9030c4c25c174943d6be2ab347
[zz@slave1 ~]$ ss -antl
State  Recv-Q  Send-Q   Local Address:Port   Peer Address:Port Process                                                        
LISTEN 0       128            0.0.0.0:80          0.0.0.0:*                                                                   
LISTEN 0       128            0.0.0.0:22          0.0.0.0:*                                                                   
LISTEN 0       128            0.0.0.0:5000        0.0.0.0:*                                                                   
LISTEN 0       128                  *:8080              *:*                                                                   
LISTEN 0       128                  *:81                *:*                                                                   
LISTEN 0       128               [::]:22             [::]:*                                                                   
LISTEN 0       128                  *:1024              *:*                                                                   
LISTEN 0       80                   *:3306              *:* 

普通用户运行一个容器时,容器文件的属主和属组都属于root,然后想让文件的属主和属组改变成普通用户本身,只要在运行容器的时候加上一个–userns=keep-id即可

[zz@slave1 ~]$  podman run -dit --name web -v /home/zz/aa/:/abc:Z --userns=keep-id -p 81:81 busybox
3245476766ac75036d68b97b2a05b852b9342efd800b3162239586c20eda1340
[zz@slave1 ~]$ podman ps
CONTAINER ID  IMAGE                             COMMAND     CREATED        STATUS            PORTS               NAMES
3245476766ac  docker.io/library/busybox:latest  sh          7 seconds ago  Up 7 seconds ago  0.0.0.0:81->81/tcp  web
[zz@slave1 ~]$ podman exec -it web /bin/sh
~ $ cd /abc
/abc $ touch 1
/abc $ ls -l 
total 0
-rw-r--r--    1 zz       zz               0 Aug 17 03:47 1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值