如何使用 RSA 密钥设置无密码 ssh

如何使用 RSA 密钥设置无密码 ssh

我正在尝试在两台计算机之间设置无密码 SSH 配置,但遇到问题。我遵循了很多方法,但都没有成功。这是我已采取的步骤

  1. 在客户端上生成身份验证密钥。 (当提示输入密码时按 Enter 键[root@box1:.ssh/$] ssh-keygen -t rsa

  2. 将公钥复制到服务器。 [root@box1:.ssh/$] scp id_rsa.pub root@box2:.ssh/authorized_keys

  3. 验证授权密钥已在服务器上成功创建

  4. 执行以下命令: [root@box1:.ssh/$] ssh root@box2 ls

仍然提示我输入密码。我读到一篇指南的注释说“取决于正在运行的 SSH 版本...”(尽管它没有指定哪些版本需要这个),它可能需要:

  • .ssh/authorized_keys2 中的公钥
  • .ssh的权限为700
  • .ssh/authorized_keys2 的权限为 640

我也按照这些步骤进行操作,但没有成功。我已经验证了 home、root 和 .ssh 目录不可按组写入(根据https://unix.stackexchange.com/tags/ssh/info)。

有人知道我缺少什么吗?

谢谢

编辑:我还使用 ssh-copy-id 命令将公钥复制到第二个盒子并生成文件.ssh/authorized_keys

[root@box1:.ssh/$] ssh-copy-id -i id_rsa.pub root@box2

EDIT2:包括版本信息

// box1(系统密钥生成于)

  • Linux 2.6.34
  • OpenSSH_5.5p1 Debian-6、OpenSSL 0.9.8o 2010 年 6 月 1 日

// 盒子2

  • Linux 2.6.33
  • Dropbear客户端 v0.52

EDIT3:调试输出

[root@box1:.ssh/$] ssh -vvv root@box2 ls
OpenSSH_5.5p1 Debian-6, OpenSSL 0.9.8o 01 Jun 2010
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Applying options for *
debug2: ssh_connect: needpriv 0
debug1: Connecting to box2 [box2] port 22.
debug1: Connection established.
debug1: permanently_set_uid: 0/0
debug3: Not a RSA1 key file /root/.ssh/id_rsa.
debug2: key_type_from_name: unknown key type '-----BEGIN'
debug3: key_read: missing keytype
debug3: key_read: missing whitespace
debug2: key_type_from_name: unknown key type '-----END'
debug3: key_read: missing keytype
debug1: identity file /root/.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 /root/.ssh/id_rsa-cert type -1
debug1: identity file /root/.ssh/id_dsa type -1
debug1: identity file /root/.ssh/id_dsa-cert type -1
debug1: Remote protocol version 2.0, remote software version dropbear_0.52
debug1: no match: dropbear_0.52
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_5.5p1 Debian-6
debug2: fd 3 setting O_NONBLOCK
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug2: kex_parse_kexinit:
diffie-hellman-group-exchange-sha256,diffie-hellman-group-exchange-sha1,diffie-hellman-    
group14-sha1,diffie-hellman-group1-sha1
debug2: kex_parse_kexinit:
[email protected],[email protected],ssh-rsa,ssh-dss
debug2: kex_parse_kexinit:
aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-cbc,3des-cbc,blowfish-    
cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,rijndael-cbc@lysatoe
debug2: kex_parse_kexinit:
aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-cbc,3des-cbc,blowfish-    
cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,rijndael-cbc@lysatoe
debug2: kex_parse_kexinit:
hmac-md5,hmac-sha1,[email protected],hmac-ripemd160,[email protected],hmac-    
sha1-96,hmac-md5-96
debug2: kex_parse_kexinit:
hmac-md5,hmac-sha1,[email protected],hmac-ripemd160,[email protected],hmac-
sha1-96,hmac-md5-96
debug2: kex_parse_kexinit: none,[email protected],zlib
debug2: kex_parse_kexinit: none,[email protected],zlib
debug2: kex_parse_kexinit:
debug2: kex_parse_kexinit:
debug2: kex_parse_kexinit: first_kex_follows 0
debug2: kex_parse_kexinit: reserved 0
debug2: kex_parse_kexinit: diffie-hellman-group1-sha1
debug2: kex_parse_kexinit: ssh-rsa,ssh-dss
debug2: kex_parse_kexinit:
aes128-ctr,3des-ctr,aes256-ctr,aes128-cbc,3des-cbc,aes256-cbc,twofish256-cbc,twofish-
cbc,twofish128-cbc,blowfish-cbc
debug2: kex_parse_kexinit:
aes128-ctr,3des-ctr,aes256-ctr,aes128-cbc,3des-cbc,aes256-cbc,twofish256-cbc,twofish-
cbc,twofish128-cbc,blowfish-cbc
debug2: kex_parse_kexinit: hmac-sha1-96,hmac-sha1,hmac-md5
debug2: kex_parse_kexinit: hmac-sha1-96,hmac-sha1,hmac-md5
debug2: kex_parse_kexinit: zlib,[email protected],none
debug2: kex_parse_kexinit: zlib,[email protected],none
debug2: kex_parse_kexinit:
debug2: kex_parse_kexinit:
debug2: kex_parse_kexinit: first_kex_follows 0
debug2: kex_parse_kexinit: reserved 0
debug2: mac_setup: found hmac-md5
debug1: kex: server->client aes128-ctr hmac-md5 none
debug2: mac_setup: found hmac-md5
debug1: kex: client->server aes128-ctr hmac-md5 none
debug2: dh_gen_key: priv key bits set: 132/256
debug2: bits set: 515/1024
debug1: sending SSH2_MSG_KEXDH_INIT
debug1: expecting SSH2_MSG_KEXDH_REPLY
debug3: check_host_in_hostfile: host 192.168.20.10 filename
/root/.ssh/known_hosts
debug3: check_host_in_hostfile: host 192.168.20.10 filename
/root/.ssh/known_hosts
debug3: check_host_in_hostfile: match line 3
debug1: Host 'box2' is known and matches the RSA host key.
debug1: Found key in /root/.ssh/known_hosts:3
debug2: bits set: 522/1024
debug1: ssh_rsa_verify: signature correct
debug2: kex_derive_keys
debug2: set_newkeys: mode 1
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug2: set_newkeys: mode 0
debug1: SSH2_MSG_NEWKEYS received
debug1: Roaming not allowed by server
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug2: service_accept: ssh-userauth
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug2: key: /root/.ssh/id_rsa (0x54b1c340)
debug2: key: /root/.ssh/id_dsa ((nil))
debug1: Authentications that can continue: publickey,password
debug3: start over, passed a different list publickey,password
debug3: preferred
gssapi-keyex,gssapi-with-mic,publickey,keyboard-interactive,password
debug3: authmethod_lookup publickey
debug3: remaining preferred: keyboard-interactive,password
debug3: authmethod_is_enabled publickey
debug1: Next authentication method: publickey
debug1: Offering public key: /root/.ssh/id_rsa
debug3: send_pubkey_test
debug2: we sent a publickey packet, wait for reply
debug1: Authentications that can continue: publickey,password
debug1: Trying private key: /root/.ssh/id_dsa
debug3: no such identity: /root/.ssh/id_dsa
debug2: we did not send a packet, disable method
debug3: authmethod_lookup password
debug3: remaining preferred: ,password
debug3: authmethod_is_enabled password
debug1: Next authentication method: password

EDIT4:另一个有趣的发展。我没有在 box1(运行 OpenSSH)上生成密钥并将它们复制到 box2(运行 dropbear),而是相反:

[root@box2:.ssh/$] dropbearkey -t rsa -f id_rsa [root@box2:.ssh/$] dropbearkey -y -f id_rsa | grep "^ssh-rsa" >> authorized_keys [root@box2:.ssh/$] scp authorized_keys root@box1:.ssh/

这样,只有当我指定 ID 文件时,我才能成功地从 box2 到 box1 发出无密码命令: [root@box2:.ssh/$] ssh -i id_rsa root@box1 ls

仍然无法从 box1 (OpenSSH) 向 box2 (dropbear) 发出命令。

答案1

我找到了问题的根源。里面有一条/var/log/messages关于奇怪的所有权的模糊信息给了我提示。于是我检查了一下, /root/root/.ssh、的权限/root/.ssh/*都是正确的(700),但所有权是default.default。我不知道这是怎么发生的......但我跑了:

[root@box1:.ssh/$] chown root.root /root
[root@box1:.ssh/$] chown root.root /root/.ssh
[root@box1:.ssh/$] chown root.root /root/.ssh/* 

将所有权更改为 root 和无密码登录是双向的。

答案2

您能确认是否允许 root 登录 ssh 吗? keygen 通常会提示输入密码。您在注册机时设置过密码吗?如果是,则提示输入该密码。如果您希望无头帐户进行无密码访问,请创建无密码私钥。

答案3

从调试来看:debug2: key_type_from_name: unknown key type '-----BEGIN'看起来您的authorized_keys文件格式不正确。
删除第一行(两行?)、最后一行(-----End)和任何其他换行符应该可以解决问题。

Linux 的密钥文件不使用与许多 Windows(和某些 Linux)生成器相同的密钥文件。例如,PuTTY 启动私钥,---- BEGIN SSH2 PUBLIC KEY ----但 Linux 正在寻找ssh-rsa AAAAB3NzaC1yc2E...G8HAaGz8ob6IXx3841ASs= Example@server 完整的规范可以在此处找到:http://man.he.net/man5/authorized_keys

但简短的版本是: *没有换行符
*以协议(ssh-rsa、ssh-dsa)开头
*公钥
*以“=”和密钥名称结尾

让我知道这是否有帮助

相关内容