使用密钥而不是密码在ssh中连接2 Pi

时间:2015-12-12 11:15:58

标签: networking ssh raspberry-pi raspbian

我在两个独立的网络上有2个Pi,运行raspbian,一个是Web服务器,另一个用于备份(让我们说pi服务器和pi-backup),我想要pi-server ssh到pi-backup以备份数据。我设法在pi服务器上输入:

ssh -p 4444 userBackup@pibackup.fr
userBackup@pibackup.fr's password :

我键入了我的密码然后我被提示进行pi-backup而且一切都很棒(pi-backup&#s; ssh在22上听,但我用路由器将4444重定向到22,听说它更安全)

由于我想编写连接脚本以便能够每天编写它,我无法通过密码进行身份验证,因此我尝试了密钥:我使用

在pi服务器上生成密钥
ssh-keygen

然后我在/home/userBackup/.ssh/authorized_keys中复制/粘贴了id_rsa_pub ......但是当我尝试连接时,我仍然需要输入密码。

我感到惊讶的是,我已经从我的主PC连接到pi备份,其中一个密钥(当然是不同的),它与pi-backup位于同一本地网络上。所以我在authorized_keys中有两行。

你知道可能出现什么问题吗?如果不是在同一个本地网络上应该不是问题吗?

[edit]当我尝试

ssh -v -i ~/.ssh/id_rsa -p 4444 userBackup@pibackup.fr

这是我得到的:

OpenSSH_6.0p1 Debian-4+deb7u2, OpenSSL 1.0.1e 11 Feb 2013
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to pibackup.fr [90.10.1.1] port 4444.
debug1: Connection established.
debug1: identity file /home/userServer/.ssh/id_rsa type 1
debug1: Checking blacklist file /usr/share/ssh/blacklist.RSA-2048
debug1: Checking blacklist file /etc/ssh/blacklist.RSA-2048
debug1: identity file /home/userServer/.ssh/id_rsa-cert type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_6.7p1 Raspbian-5
debug1: match: OpenSSH_6.7p1 Raspbian-5 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.0p1 Debian-4+deb7u2
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-sha1 none
debug1: kex: client->server aes128-ctr hmac-sha1 none
debug1: sending SSH2_MSG_KEX_ECDH_INIT
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ECDSA 6b:5f:aa:aa:aa0c:aa:33:97:aa:56:aa:39:00:6f:79
debug1: Host '[pibackup.fr]:4444' is known and matches the ECDSA host key.
debug1: Found key in /home/userBackup/.ssh/known_hosts:2
Warning: Permanently added the ECDSA host key for IP address '[90.10.1.1]:4444' to the list of known hosts.
debug1: ssh_ecdsa_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,password
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /home/userServer/.ssh/id_rsa
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: password
userBackup@piBackup.fr's password: 

我注意到在最后几行中,他说"提供RSA公钥:/home/userServer/.ssh/id_rsa"所以我尝试使用我的公钥,但没有任何改变。

2 个答案:

答案 0 :(得分:0)

您必须在ssh-keygen行中指定使用ssh生成的密钥。你可以这样做:

ssh -i ~/.ssh/blah.key -p 4444 userBackup@pibackup.fr它会加载您的密钥。

我希望它会对你有所帮助。

答案 1 :(得分:0)

我终于找到了问题:在serverBackup上,userBackup不是authorized_keys的所有者......这就是全部;)。我仍然不明白为什么当我从我的局域网连接时它工作,但现在一切都很好! Ankirama,thansk为您的时间和建议。

相关问题