Ubuntu

為什麼密碼修改失敗?chpasswd:權限被拒絕

  • October 6, 2022

我的 Dockerfile

FROM ubuntu:latest
RUN apt update
RUN apt install -y openssh-server sudo 
RUN useradd -ms /bin/bash -g root -G sudo -u 1000 remote_user
USER remote_user
WORKDIR /home/remote_user
RUN mkdir /home/remote_user/.ssh && chmod 700 /home/remote_user/.ssh
COPY remotecentos.pub /home/remote_user/.ssh/authorized_keys
RUN stat /etc/passwd
RUN  echo 'remote_user:*****2599*****' | chpasswd -c SHA256
RUN service ssh start
EXPOSE 22
CMD ["/usr/sbin/sshd","-D"]

我有錯誤

Step 9/13 : RUN stat /etc/passwd
---> Running in 7147677000bd
 File: /etc/passwd
 Size: 1325        Blocks: 8          IO Block: 4096   regular file
Device: 37h/55d Inode: 22961332    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2022-10-06 12:45:10.000000000 +0000
Modify: 2022-10-06 12:45:10.000000000 +0000
Change: 2022-10-06 12:45:11.197943157 +0000
Birth: 2022-10-06 12:45:11.197943157 +0000
Removing intermediate container 7147677000bd
---> 7b37835f7f2c
Step 10/13 : RUN  echo 'remote_user:******' | chpasswd -c SHA256
---> Running in faae63d7fd92
chpasswd: Permission denied.
chpasswd: cannot lock /etc/passwd; try again later.

如何修復容器內的權限?

我應該如何更改密碼行?

remote_user您的 Dockerfile在呼叫之前將目前使用者更改為chpasswd. 要使用chpasswd,您需要是 root。您可能希望將該RUN行(以及任何其他需要超級使用者權限的命令)移到該行之前USER。我也期待ssh服務的啟動會因為類似的原因而失敗。

引用自:https://unix.stackexchange.com/questions/720009