SSH 连接要求输入密码连接到 Docker 容器

时间:2021-01-03 11:51:34

标签: linux docker ssh containers

我已经在寻找类似的问题,并尝试更改配置中的几项内容,但找不到解决方案。

我正在尝试通过 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 以“remote_user”身份连接到容器(其中“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 的权限,我有:

  • 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

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

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

docker run -p 222:22 your-image

然后:

ssh -p 222 remote_user@localhost
相关问题