2014-10-29 60 views
2

我试图推我的id_rsa.pub到服务器,所以我可以ssh进入它。添加ssh公钥到服务器

我已经运行下面的命令,

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). 

我想不通的问题是什么.. 任何人可以帮我出来吗?

回答

3

基于密钥的身份验证只有在authorized_keys拥有 权限时才有效。

尝试做在chmod 600的〜/ .ssh/authorized_keys中

+0

你的意思是在我的电脑上?显然我没有一个名为authorized_keys的文件。这是我需要生成的东西吗? – ytrewq 2014-10-29 07:20:00

+0

对不起,那还不清楚。它不在本地计算机上,而是在远程计算机上。您最初运行的命令(来自您的问题)在远程计算机上生成了该文件。试着做一个ssh -v -p 12345 [email protected]'chmod 600〜/ .ssh/authorized_keys – 2014-10-29 08:27:35

+0

thanx,但是这也导致了同样的错误。 – ytrewq 2014-10-29 14:53:18

3

你的命令,如果你可以通过输入密码ssh到服务器将只工作。它还需要在远程服务器上创建dir〜/ .ssh,最后(如Jan-Erik Revsbech指出的)本地和远程〜/ .ssh的权限需要禁止其他人读取它。

如果您使用ssh-copy-id remote-server来复制您的密钥,那么远程系统上的dir和权限都将被正确设置。不过,它仍然会要求您第一次输入密码。

+0

显然ssh-copy-id不起作用Mac终端..是否有其他选择? – ytrewq 2014-10-29 07:33:08

+0

你正在做的是另一种方式,但在遥控器上你需要使用密码和'mkdir .ssh登录; chmod 700 .ssh; touch .ssh/authorized_keys; chmod 600 .ssh/authorized_keys',然后再运行命令。 – 2014-10-29 08:49:30

+0

*** SOOOOOOOO ***比确保所有权限设置正确要容易得多! – n0nag0n 2017-10-09 17:20:35