gpt4 book ai didi

shell - "I have no name!"作为用户在使用 Tini 的 docker 容器中登录 Jenkins

转载 作者:行者123 更新时间:2023-12-02 13:46:32 28 4
gpt4 key购买 nike

我目前在 docker container 中使用 Jenkins 实例.
此图片恰好使用 Tini作为PID 1。
当我尝试使用以下命令打开 shell 时:

$ docker exec -it jenkins /bin/bash

我得到这个作为用户名:
I have no name!@<container_id_hash>:/$

这使我无法使用来自在此容器内运行的 Jenkins 作业的 shell 生成的 ssh 命令:
$ ssh
$ No user exists for uid 497
$ id
$ uid=497 gid=495 groups=495

我尝试在/etc/passwd 中为该 uid 创建一个用户,并在/etc/group 中为该 gid 创建一个组,但没有成功!

如果我像这样以 jenkins 用户身份登录,我只能手动运行 ssh:
$ docker exec -it --user=jenkins jenkins /bin/bash

我可以使用 ssh 相关插件绕过它。但我真的很想知道为什么只有使用 Tini 作为入口点的 docker 图像才会发生这种情况。

更新 1

我在/etc/passwd 中做了这样的事情:
jenkins:x:497:495::/var/jenkins_home:/bin/bash
这在/etc/group 中:
jenkins:x:495:
还尝试了其他名称,例如 yesihaveanameyesihaveagroup而不是 jenkins
更新2

一直联系Tini的 developer并且他不相信这个问题的原因是 Tini,因为它不会乱七八糟的 uid 或 gid,任何其他线索都会被赞赏。

最佳答案

更新
很高兴知道(这很容易,所以我忽略了一段时间 *facepalm*):
要以 root 身份登录容器,只需提供 --user root到您的 exec 命令 - 如:docker exec -ti -u root mycontainername bash ...无需复制密码文件并设置密码哈希...

就像您发布的链接所说的那样,容器内的 UserID 可能没有分配名称。
(虽然我不使用 Tini... )我解决了这个问题如下:
1.) 在容器内执行 ( docker exec -ti mycontainername sh ):

id    # shows the userid (e.g. 1234) and groupid (e.g. 1235) of the current session
2.) 在容器外(在本地机器上)执行:
docker cp mycontainername:/etc/passwd /tmp   # this copies the passwd-file to from inside the container to my local /tmp-directory
echo "somename:x:1234:1235:somename:/tmp:/bin/bash" >> /tmp/passwd # add some username *!!with the userid and groupid from the output!!* of the `id` command inside the container (CAUTION: do NOT overwrite, do JUST APPEND to the file) - "1234" is just exemplary, do not use it
docker cp /tmp/passwd mycontainername:/etc/passwd # copy the file back, overwriting the /etc/passwd inside the container
现在再次登录到容器 ( docker exec -ti mycontainername sh )。
附言
如果您知道容器的 root 密码,您现在可以切换到 root
如果您没有它,您可以将“/etc/shadow”文件复制出容器(如上所示),然后使用已知密码哈希编辑根条目**,然后将其复制回容器中并然后登录容器并运行 su )
** 在您的本地系统上获取此密码哈希:
(1) 添加临时 testuser ( sudo useradd testdumpuser )
(2) 将此用户作为密码 ( sudo passwd testdumpuser )
(3) 看 /etc/shadow -file for the "testdumpuser"-entry 并在第一个 ":"之后复制这个长奇数字符串,直到第二个 ":"

关于shell - "I have no name!"作为用户在使用 Tini 的 docker 容器中登录 Jenkins,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37836124/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com