首页 > 解决方案 > SSH 连接要求输入密码连接到 Docker 容器

问题描述

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

我正在尝试通过 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

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

...
...
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我拥有的权限:

标签: linuxdockersshcontainers

解决方案


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

docker run -p 222:22 your-image

然后 :

ssh -p 222 remote_user@localhost

推荐阅读