centos - 无法在 Docker centos 镜像中以普通用户身份 ping

标签 centos docker

我的 Dockerfile

FROM centos
RUN useradd me
CMD su -c "ping localhost" me

我的测试命令:

$ docker build -t test .
$ docker run --rm -it test
ping: icmp open socket: Operation not permitted

$ docker run --rm -it test /bin/bash    
[root@153c87b53b53 /]# ping localhost
PING localhost (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.126 ms

我的临时解决方案是 https://www.centos.org/forums/viewtopic.php?t=39341

chmod 4755 /usr/bin/ping

最佳答案

这不是“临时解决方案”,而是允许用户级别 ping 的实际解决方案 - 基本上 ping 需要根级别访问权限才能以原始模式打开套接字。因此,当它尝试执行此操作但未以 root 身份运行时,就会出现上述错误。

因此,为了使其工作,ping 必须是 setuid root,这就是您在 chmod 4755/bin/ping 时所做的 - 这意味着当您以普通用户身份运行 ping 时,您将权限提升为 root,但 ping 足够聪明,可以在打开套接字后直接将您返回到您的用户。

因此您的 Dockerfile 可能如下所示:

FROM centos
RUN chmod 4755 /bin/ping
RUN useradd me
CMD su -c "ping localhost" me

关于centos - 无法在 Docker centos 镜像中以普通用户身份 ping,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28553923/

相关文章:

php - 无法关闭 PHP 安全模式?

php - 如何让 php 使用 docker 与 mysql 对话?

git - 将 git 分支名称转换为有效的 docker 镜像标签

rubygems 1.3.6 安装失败

git - 通过 Dockerfile 安装包时出错

docker - 在 docker alpine 上安装软件包时无法满足的约束

python - 为什么Flask logger在前面使用UWSGI时不登录docker?

CentOS 8 Pane 已死 VMware Workstation 15.5

git 将本地分支推送到远程,RPC 失败

mysql - gem mysql2安装