将ssh公钥添加到服务器

时间:2014-10-29 07:10:36

标签: ssh public-key

我正在尝试将我的id_rsa.pub推送到服务器,以便我可以直接进入它。

我运行了以下命令,

cat ~/.ssh/id_rsa.pub | ssh -v -p 12345 myname@123.45.67.8 'cat >> ~/.ssh/authorized_keys'

返回以下调试:

debug1: Reading configuration data /etc/ssh_config
debug1: /etc/ssh_config line 20: Applying options for *
debug1: Connecting to 123.45.67.8 [123.45.67.8] port 12345.
debug1: Connection established.
debug1: identity file /Users/myname/.ssh/id_rsa type 1
debug1: identity file /Users/myname/.ssh/id_rsa-cert type -1
debug1: identity file /Users/myname/.ssh/id_dsa type -1
debug1: identity file /Users/myname/.ssh/id_dsa-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.2
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.9p1 Debian-5ubuntu1.4
debug1: match: OpenSSH_5.9p1 Debian-5ubuntu1.4 pat OpenSSH_5*
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-md5 none
debug1: kex: client->server aes128-ctr hmac-md5 none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Server host key: RSA [some server host key]
debug1: Host '[123.45.67.8]:12345' is known and matches the RSA host key.
debug1: Found key in /Users/myname/.ssh/known_hosts:6
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: Roaming not allowed by server
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /Users/myname/.ssh/github_rsa
debug1: Authentications that can continue: publickey
debug1: Offering RSA public key: /Users/myname/.ssh/id_rsa
debug1: Authentications that can continue: publickey
debug1: Trying private key: /Users/myname/.ssh/id_dsa
debug1: No more authentication methods to try.
Permission denied (publickey).

我无法弄清楚问题是什么.. 任何人都可以帮帮我吗?

3 个答案:

答案 0 :(得分:4)

只有输入密码才能通过ssh访问服务器,您的命令才有效。它还需要在远程服务器上创建dir~ / .ssh,最后(正如Jan-Erik Revsbech指出的那样)本地和远程的权限〜/ .ssh需要禁止其他人阅读它。

如果您使用ssh-copy-id remote-server复制密钥,则远程系统上的目录和权限都将正确设置。但是,它仍然需要您第一次输入密码。

答案 1 :(得分:3)

基于密钥的身份验证仅在authorized_keys有效时才有效 正确的权限。

尝试做一个chmod 600~ / .ssh / authorized_keys

答案 2 :(得分:0)

请确保还设置正确的所有者:)

我按照Ole的建议尝试了ssh-copy-id remote-server,但遇到了这样的错误:

无法写入.ssh / authorized_keys。权限被拒绝

服务器上已经存在目录~/.ssh和文件authorized_keys,并已通过正确的权限进行了设置……但是给错误的用户!这是解决问题的方法:

chown right-user:right-user .ssh
chown right-user:right-user .ssh/authorized_keys

我更改了文件和目录的所有者。然后ssh-copy-id工作了。