Linux 无法在已装入/etc/passwd和/etc/shadow的docker容器中添加新用户 问题的例子:

Linux 无法在已装入/etc/passwd和/etc/shadow的docker容器中添加新用户 问题的例子:,linux,docker,passwd,etcpasswd,Linux,Docker,Passwd,Etcpasswd,使用passwd时会出现类似的问题: [root@681a5489f3b0 /]# passwd test Changing password for user test. New password: BAD PASSWORD: The password is shorter than 8 characters Retype new password: passwd: Authentication token manipulation error 我尝试过使用ubuntu映像,但同样的问题

使用passwd时会出现类似的问题:

[root@681a5489f3b0 /]# passwd test
Changing password for user test.
New password: 
BAD PASSWORD: The password is shorter than 8 characters
Retype new password: 
passwd: Authentication token manipulation error
我尝试过使用ubuntu映像,但同样的问题也出现了

我可以从容器中手动编辑passwd文件和shadow文件

我在以下两台机器上遇到同样的问题: 主机操作系统:CentOS 7-禁用SELinux
Docker版本:1.8.2,构建0a8c2e3

主机操作系统:CoreOS 766.4.0
Docker版本:1.7.1,构建df2f73d


我还打开了GitHub上的问题:

它失败了,因为
passwd
操作了一个临时文件,然后试图将其重命名为
/etc/shadow
。这会失败,因为
/etc/shadow
是一个挂载点,无法替换,这会导致此错误(使用
strace
捕获):

您可以从命令行简单地再现这一点:

# cd /etc
# touch foo
# mv foo shadow
mv: cannot move 'foo' to 'shadow': Device or resource busy
您可以通过将包含
my_shadow
my_passwd
的目录装载到其他地方,然后在容器中适当地对
/etc/passwd
/etc/shadow
进行符号链接来解决此问题:

$ docker run -it --rm -v $PWD/my_etc:/my_etc centos
[root@afbc739f588c /]# ln -sf /my_etc/my_passwd /etc/passwd
[root@afbc739f588c /]# ln -sf /my_etc/my_shadow /etc/shadow
[root@afbc739f588c /]# ls -l /etc/{shadow,passwd}
lrwxrwxrwx. 1 root root 17 Oct  8 17:48 /etc/passwd -> /my_etc/my_passwd
lrwxrwxrwx. 1 root root 17 Oct  8 17:48 /etc/shadow -> /my_etc/my_shadow
[root@afbc739f588c /]# passwd root
Changing password for user root.
New password: 
Retype new password: 
passwd: all authentication tokens updated successfully.
[root@afbc739f588c /]# 

使用此解决方案,useradd和adduser不再工作。该如何修复?我已经尝试过了,符号链接只是被删除,并替换为卷影文件的副本(在容器中),因此您不会在主机上获得更新。因此,如果这真的有效的话,对于debian/ubuntu来说,它现在肯定是无用的。我也尝试过这种方法,但失败了,但是它给了我使它工作所需要的东西。你所需要做的不是将/etc/{passwd,shadow}符号链接到/my_etc/,而是来回复制文件:
cp/metc/*/etc
->做你的事->
cp/etc/{passwd,shadow}/metc/
# cd /etc
# touch foo
# mv foo shadow
mv: cannot move 'foo' to 'shadow': Device or resource busy
$ docker run -it --rm -v $PWD/my_etc:/my_etc centos
[root@afbc739f588c /]# ln -sf /my_etc/my_passwd /etc/passwd
[root@afbc739f588c /]# ln -sf /my_etc/my_shadow /etc/shadow
[root@afbc739f588c /]# ls -l /etc/{shadow,passwd}
lrwxrwxrwx. 1 root root 17 Oct  8 17:48 /etc/passwd -> /my_etc/my_passwd
lrwxrwxrwx. 1 root root 17 Oct  8 17:48 /etc/shadow -> /my_etc/my_shadow
[root@afbc739f588c /]# passwd root
Changing password for user root.
New password: 
Retype new password: 
passwd: all authentication tokens updated successfully.
[root@afbc739f588c /]#