【问题标题】:SSH to Docker: Permission denied (publickey)SSH 到 Docker:权限被拒绝(公钥)
【发布时间】:2017-06-03 01:04:45
【问题描述】:

我正在运行一个 docker 容器,并想使用 emacs 的 tramp 包 ssh 到它。我可以成功使用docker exec -it containername bash。但我只想用我的 emacs 来做配置工作。我已经将容器的 22 端口暴露给 localhost 的 22 端口。

顺便说一句,我的.ssh 文件夹中有id_rsa。

但是,即使我使用ssh -p 22 dwolf@localhost,它仍然无法正常工作。日志如下:

OpenSSH_7.4p1, LibreSSL 2.5.0
debug1: Reading configuration data /Users/spacegoing/.ssh/config
debug1: /Users/spacegoing/.ssh/config line 26: Applying options for *
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Connecting to localhost [::1] port 22.
debug1: connect to address ::1 port 22: Connection refused
debug1: Connecting to localhost [127.0.0.1] port 22.
debug1: Connection established.
debug1: identity file /Users/spacegoing/.ssh/id_rsa type 1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/spacegoing/.ssh/id_rsa-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.4
debug1: Remote protocol version 2.0, remote software version OpenSSH_6.0p1 Debian-4+deb7u3
debug1: match: OpenSSH_6.0p1 Debian-4+deb7u3 pat OpenSSH* compat 0x04000000
debug1: Authenticating to localhost:22 as 'dwolf'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: ecdh-sha2-nistp256
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
debug1: kex: server->client cipher: aes128-ctr MAC: umac-64@openssh.com compression: none
debug1: kex: client->server cipher: aes128-ctr MAC: umac-64@openssh.com compression: none
debug1: sending SSH2_MSG_KEX_ECDH_INIT
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:w7Y3BsQ1xof3U5cohsL5y9ctWvgNaTuXdbDFwQtE+Gc
debug1: Host 'localhost' is known and matches the ECDSA host key.
debug1: Found key in /Users/spacegoing/.ssh/known_hosts:26
debug1: rekey after 4294967296 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey after 4294967296 blocks
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /Users/spacegoing/.ssh/id_rsa
debug1: Authentications that can continue: publickey
debug1: No more authentication methods to try.
Permission denied (publickey).

【问题讨论】:

  • 将您的公钥添加到 docker 容器的 authorized_keys 文件中。 dd if=~/.ssh/id_rsa.pub | docker exec -it containername dd of=~/.ssh/authorized_keys(此命令可能不起作用,尝试将其与 docker 主机分开,但重点是)
  • @MattClark 非常感谢您的帮助!这个方法确实有效!
  • @tgogos 感谢您的回复。我正在研究这篇文章,很有帮助!

标签: docker ssh


【解决方案1】:

这三行:

debug1: Next authentication method: publickey
debug1: Offering RSA public key: /Users/spacegoing/.ssh/id_rsa
debug1: Authentications that can continue: publickey

表明你提供了一个公钥,但它被拒绝了;您不在目标主机的 authorized_keys 文件中。

要将你的公钥复制到docker镜像中,你可以使用这个oneliner,当然,还有很多其他的方法可以将你的公钥复制到机器中。

dd if=~/.ssh/id_rsa.pub | docker exec -it containername dd of=~/.ssh/authorized_keys

正如有人指出的那样,您的容器应该尽可能小,理想情况下甚至不需要自己的 SSH 服务器;但同样,每个人的用例都不同。

此命令将覆盖目标上任何现有的授权密钥

【讨论】:

    【解决方案2】:

    看看这一行:

    debug1: identity file /Users/spacegoing/.ssh/id_rsa type 1
    debug1: key_load_public: No such file or directory
    

    您的设备上似乎没有 id_rsa 密钥以及何时

    debug1: Next authentication method: publickey
    

    找不到它并退出,请尝试使用以下命令添加此密钥:

    cd ~/.ssh/
    ssh-keygen id_rsa
    

    然后对所有问题按 Enter 键并重复您的连接。

    【讨论】:

    • 感谢您的回答!但是我的.ssh 文件夹中确实有该文件。我也想知道那个警告:P
    • 消息key_load_public: No such file or directory 并不是说​​它没有找到私钥,而是说它没有加载公钥。这只是一个警告,而不是错误。可以忽略,不会阻止连接。
    猜你喜欢
    • 1970-01-01
    • 2019-11-24
    • 2014-11-08
    • 2019-03-11
    • 1970-01-01
    • 2019-03-08
    • 2019-08-20
    • 2016-08-13
    • 1970-01-01
    相关资源
    最近更新 更多