【问题标题】:SSH connection asks for password connecting to Docker container [closed]SSH连接要求输入密码连接到Docker容器[关闭]
【发布时间】:2021-01-03 22:50:24
【问题描述】:

我已经寻找过类似的问题,并尝试更改配置中的一些内容,但无法找到解决方案。

我正在尝试通过 SSH 连接到 Docker 容器,这里是 Dockerfile:

FROM ubuntu

RUN apt-get update && \
 apt-get install -y openssh-server

RUN useradd remote_user && \
    echo "remote_user:test1234" | chpasswd && \
    mkdir /home/remote_user/.ssh -p && \
    chmod 700 /home/remote_user/.ssh && \
    mkdir -p -m0755 /var/run/sshd


COPY id_rsa.pub /home/remote_user/.ssh/authorized_keys

RUN chown remote_user:remote_user   -R /home/remote_user && \
    chmod 600 /home/remote_user/.ssh/authorized_keys

RUN apt-get install -y php php-mbstring php-xml php-bcmath php-fpm && \
    apt-get install -y composer && apt-get install -y vim
    
RUN apt-get install -y nginx

CMD /usr/sbin/sshd -D

一旦我尝试使用ssh -Tv remote_user@staging.local(其中“staging.local”是容器 IP)以“remote_user”身份连接到容器,我会收到以下消息:

...
...
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519,sk-ssh-ed25519@openssh.com,ssh-rsa,rsa-sha2-256,rsa-sha2-512,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,sk-ecdsa-sha2-nistp256@openssh.com>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: publickey
debug1: Offering public key: /home/xxx/.ssh/id_rsa RSA SHA256:5QNPe89pdQp+tgE61N9YPaIJEs8QR9DxaChmStfvzBU agent
debug1: Authentications that can continue: publickey,password
debug1: Offering public key: xxx@xxx RSA SHA256:C+VWlGUd4mVywHnh8JWtjL0gmO8cuqUEs4YYCbQGvaE agent
debug1: Authentications that can continue: publickey,password
debug1: Trying private key: /home/xxx/.ssh/id_dsa
debug1: Trying private key: /home/xxx/.ssh/id_ecdsa
debug1: Trying private key: /home/xxx/.ssh/id_ecdsa_sk
debug1: Trying private key: /home/xxx/.ssh/id_ed25519
debug1: Trying private key: /home/xxx/.ssh/id_ed25519_sk
debug1: Trying private key: /home/xxx/.ssh/id_xmss
debug1: Next authentication method: password
remote_user@staging.local's password:

如您所见,它无法连接并要求输入密码。

如果我在我的主机中ls -ll .ssh 文件夹文件,我有这个:

-rw------- 1 xxx xxx 2610 Jan  3 12:08 id_rsa
-rw-r--r-- 1 xxx xxx  577 Jan  3 12:08 id_rsa.pub
-rw-r--r-- 1 xxx xxx  222 Jan  3 12:25 known_hosts

如果我 docker exec 以 root 用户身份进入容器并查看 /home/remote_user/.ssh 的权限,我有:

  • home 文件夹权限: drwxr-xr-x 1 root root 4096 Jan 3 11:22 home

  • remote_user 文件夹权限: drwxr-xr-x 1 remote_user remote_user 4096 Jan 3 11:22 remote_user

  • .ssh 文件夹权限: drwx------ 1 remote_user remote_user 4096 Jan 3 11:22 .ssh

  • authorized_keys 文件权限: -rw------- 1 remote_user remote_user 577 Jan 3 11:08 authorized_keys

【问题讨论】:

    标签: linux docker ssh containers


    【解决方案1】:

    如果您转发端口,它应该可以工作:

    docker run -p 222:22 your-image
    

    然后:

    ssh -p 222 remote_user@localhost
    

    【讨论】:

      【解决方案2】:

      尝试将/home/remote_user/.ssh/authorized_keys 的权限更改为 400 而不是 600。除此之外,我看不出我自己的设置有任何区别。

      附言 我的设置使用 ubuntu 18.04 运行,所以也许他们从那时起改变了一些东西。

      【讨论】:

      • 谢谢,我试过了,还是一样的错误