SSH到Docker:权限被拒绝(公钥)

SSH到Docker:权限被拒绝(公钥),docker,ssh,Docker,Ssh,我正在运行一个docker容器,并希望使用emacs的tramp包ssh到它。我可以成功使用docker exec-it containername bash。但我只想用我的emacs做配置工作。我已经将容器的端口22公开给本地主机端口22 顺便说一下,我的.ssh文件夹中确实有id\u rsa 然而,即使我使用ssh-p22dwolf@localhost它仍然不起作用。日志如下所示: OpenSSH_7.4p1, LibreSSL 2.5.0 debug1: Reading configura

我正在运行一个docker容器,并希望使用emacs的
tramp
包ssh到它。我可以成功使用
docker exec-it containername bash
。但我只想用我的emacs做配置工作。我已经将容器的端口22公开给本地主机端口22

顺便说一下,我的
.ssh
文件夹中确实有
id\u rsa

然而,即使我使用ssh-p22dwolf@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).
请看这一行:

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键输入所有问题并重复连接。

这三行:

debug1: Next authentication method: publickey
debug1: Offering RSA public key: /Users/spacegoing/.ssh/id_rsa
debug1: Authentications that can continue: publickey
表明您正在提供公钥,但它被拒绝;您不在目标主机的授权密钥文件中

要将公钥复制到docker映像中,您可以使用此oneliner,当然,还有许多其他方法可以将密钥复制到机器中

dd if=~/.ssh/id_rsa.pub | docker exec -it containername dd of=~/.ssh/authorized_keys
然而,正如所指出的,您的容器应该尽可能小,理想情况下甚至不需要自己的SSH服务器;但是,每个人的用例都是不同的


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

将您的公钥添加到docker容器授权密钥文件中
dd if=~/.ssh/id_rsa.pub | docker exec-它包含名称dd of=~/.ssh/authorized_keys
(此命令可能不起作用,尝试将其与docker主机隔开一行,但重点是)发件人:~jpetazzo@MattClark非常感谢您的帮助!这个方法确实有效@感谢您的回复。我正在学习这篇文章,它很有帮助!谢谢你的回答!但是,我的
.ssh
文件夹中确实有该文件。我还想知道这个警告:p消息
key\u load\u public:No这样的文件或目录
并不是说它找不到私钥,而是说它没有加载公钥。这只是一个警告,而不是一个错误。它可以被忽略,但不会阻止连接。