![Docker 通过 ssh 登录总是要求输入密码](https://linux22.com/image/1431728/Docker%20%E9%80%9A%E8%BF%87%20ssh%20%E7%99%BB%E5%BD%95%E6%80%BB%E6%98%AF%E8%A6%81%E6%B1%82%E8%BE%93%E5%85%A5%E5%AF%86%E7%A0%81.png)
我尝试创建一个 Ubuntu 映像,其中包含一个“ubuntu”用户,该用户可以通过 ssh 登录容器而无需使用密码。当我尝试登录时,它会要求输入密码。
这是 Dockerfile
FROM ubuntu:latest
ENV USER ubuntu
RUN apt-get update && apt-get install -y openssh-server
RUN mkdir /var/run/sshd
RUN adduser --disabled-password --gecos "" $USER
RUN adduser $USER sudo
ADD authorized_keys /home/$USER/.ssh/authorized_keys
RUN chown $USER /home/$USER/.ssh/authorized_keys
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]
知道我做错了什么吗?
谢谢
答案1
未正确设置 authorized_keys 上的权限。此 Dockerfile 有效:
FROM ubuntu:latest
ENV USER ubuntu
RUN apt-get update && apt-get install -y openssh-server
RUN mkdir /var/run/sshd
RUN adduser --disabled-password --gecos "" $USER
RUN adduser $USER sudo
ADD authorized_keys /home/$USER/.ssh/authorized_keys
RUN chown $USER /home/$USER/.ssh/authorized_keys
RUN chown -R $USER:$USER /home/$USER/.ssh/authorized_keys
RUN chmod 700 /home/$USER/.ssh/authorized_keys
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]