将ssh公钥添加到服务器

问题描述 投票:2回答:3

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

我运行以下命令,

cat ~/.ssh/id_rsa.pub | ssh -v -p 12345 [email protected] '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).

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

ssh public-key
3个回答
3
投票

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

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


4
投票

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

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


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工作。

© www.soinside.com 2019 - 2024. All rights reserved.