无密码 SSH 无法与 Mountain Lion 配合使用

无密码 SSH 无法与 Mountain Lion 配合使用

我们在小办公室里使用 Backuppc,里面有 Windows 7 PC,还有一些用户使用 Macbook。Backuppc 位于运行 Ubuntu 12.04.2 的服务器上。无密码 ssh 在 Snow Leopard 和 Lion 上工作正常,但我在 Mountain Lion 上却遇到了麻烦,因为它不断要求输入密码。我按照交换 rsa 公钥的程序操作,并将 Backuppc 用户的公钥添加到 Mountain Lion 主机。首先,我将密钥添加到 .ssh 目录中的“authorized_keys2”文件中。它不起作用。经过一番搜索,一些人建议改用“authorized_keys”,因为 Mountain Lion 似乎升级了 sshd 包。它也不起作用。我认为 .ssh 目录 (700) 和“authorized_keys”文件的权限设置正确(编辑:644)。每次我尝试从 Backuppc 用户 ssh 到装有 Mountain Lion 的机器时,它都会不断要求输入密码。真的很烦人。下面我附上了调试输出,以防有人可以提供一些提示。非常感谢您的帮助。

backuppc@ubuntu:~$ ssh -v [email protected]
OpenSSH_5.9p1 Debian-5ubuntu1, OpenSSL 1.0.1 14 Mar 2012
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to 192.168.10.55 [192.168.10.55] port 22.
debug1: Connection established.
debug1: identity file /var/lib/backuppc/.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 /var/lib/backuppc/.ssh/id_rsa-cert type -1
debug1: identity file /var/lib/backuppc/.ssh/id_dsa type -1
debug1: identity file /var/lib/backuppc/.ssh/id_dsa-cert type -1
debug1: identity file /var/lib/backuppc/.ssh/id_ecdsa type -1
debug1: identity file /var/lib/backuppc/.ssh/id_ecdsa-cert type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.9
debug1: match: OpenSSH_5.9 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_5.9p1 Debian-5ubuntu1
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 4c:c3:21:8f:6e:59:60:a9:5f:94:75:01:2d:e2:03:3e
debug1: Host '192.168.10.55' is known and matches the RSA host key.
debug1: Found key in /var/lib/backuppc/.ssh/known_hosts:2
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,keyboard-interactive
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /var/lib/backuppc/.ssh/id_rsa
debug1: Authentications that can continue: publickey,keyboard-interactive
debug1: Trying private key: /var/lib/backuppc/.ssh/id_dsa
debug1: Trying private key: /var/lib/backuppc/.ssh/id_ecdsa
debug1: Next authentication method: keyboard-interactive
Password:

根据评论中的建议,我在 Mountain Lion 目标机器上运行了“sshd -d”。首先,我停止了“ssh”,然后运行了“sshd -d”。我将结果粘贴在下面:

$sudo launchctl unload  /System/Library/LaunchDaemons/ssh.plist
$sudo /usr/sbin/sshd -d

debug1: sshd version OpenSSH_5.9p1
debug1: read PEM private key done: type RSA
debug1: private host key: #0 type 1 RSA
debug1: read PEM private key done: type DSA
debug1: private host key: #1 type 2 DSA
debug1: rexec_argv[0]='/usr/sbin/sshd'
debug1: rexec_argv[1]='-d'
debug1: Bind to port 22 on 0.0.0.0.
Server listening on 0.0.0.0 port 22.
debug1: Bind to port 22 on ::.
Server listening on :: port 22.
debug1: fd 6 clearing O_NONBLOCK
debug1: Server will not fork when running in debugging mode.
debug1: rexec start in 6 out 6 newsock 6 pipe -1 sock 9
debug1: inetd sockets after dupping: 5, 5
Connection from 192.168.10.100 port 34179
debug1: Client protocol version 2.0; client software version OpenSSH_5.9p1 Debian-5ubuntu1
debug1: match: OpenSSH_5.9p1 Debian-5ubuntu1 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_5.9
debug1: permanently_set_uid: 75/75 [preauth]
debug1: list_hostkey_types: ssh-rsa,ssh-dss [preauth]
debug1: SSH2_MSG_KEXINIT sent [preauth]
debug1: SSH2_MSG_KEXINIT received [preauth]
debug1: kex: client->server aes128-ctr hmac-md5 none [preauth]
debug1: kex: server->client aes128-ctr hmac-md5 none [preauth]
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST received [preauth]
debug1: SSH2_MSG_KEX_DH_GEX_GROUP sent [preauth]
debug1: expecting SSH2_MSG_KEX_DH_GEX_INIT [preauth]
debug1: SSH2_MSG_KEX_DH_GEX_REPLY sent [preauth]
debug1: SSH2_MSG_NEWKEYS sent [preauth]
debug1: expecting SSH2_MSG_NEWKEYS [preauth]
debug1: SSH2_MSG_NEWKEYS received [preauth]
debug1: KEX done [preauth]
debug1: userauth-request for user user1 service ssh-connection method none [preauth]
debug1: attempt 0 failures 0 [preauth]
debug1: PAM: initializing for "user1"
debug1: PAM: setting PAM_RHOST to "ubuntu"
debug1: userauth-request for user user1 service ssh-connection method publickey [preauth]
debug1: attempt 1 failures 0 [preauth]
debug1: test whether pkalg/pkblob are acceptable [preauth]
debug1: temporarily_use_uid: 502/20 (e=0/0)
debug1: trying public key file /Users/user1/.ssh/authorized_keys
debug1: fd 6 clearing O_NONBLOCK
Authentication refused: bad ownership or modes for directory /Users/user1
debug1: restore_uid: 0/0
debug1: temporarily_use_uid: 502/20 (e=0/0)
debug1: trying public key file /Users/user1/.ssh/authorized_keys2
debug1: fd 6 clearing O_NONBLOCK
Authentication refused: bad ownership or modes for directory /Users/user1
debug1: restore_uid: 0/0
Failed publickey for user1 from 192.168.10.100 port 34179 ssh2
debug1: audit_event: unhandled event 6
debug1: userauth-request for user user1 service ssh-connection method keyboard-interactive [preauth]
debug1: attempt 2 failures 1 [preauth]
debug1: keyboard-interactive devs  [preauth]
debug1: auth2_challenge: user=user1 devs= [preauth]
debug1: kbdint_alloc: devices 'pam' [preauth]
debug1: auth2_challenge_start: trying authentication method 'pam' [preauth]
Postponed keyboard-interactive for user1 from 192.168.10.100 port 34179 ssh2 [preauth]
debug1: do_pam_account: called
debug1: PAM: num PAM env strings 2
Postponed keyboard-interactive/pam for user1 from 192.168.10.100 port 34179 ssh2 [preauth]
debug1: do_pam_account: called
Accepted keyboard-interactive/pam for user1 from 192.168.10.100 port 34179 ssh2
debug1: monitor_read_log: child log fd closed
debug1: monitor_child_preauth: user1 has been authenticated by privileged process
debug1: PAM: establishing credentials
User child is on pid 3240
debug1: PAM: establishing credentials
debug1: permanently_set_uid: 502/20
debug1: Entering interactive session for SSH2.
debug1: server_init_dispatch_20
debug1: server_input_channel_open: ctype session rchan 0 win 1048576 max 16384
debug1: input_session_request
debug1: channel 0: new [server-session]
debug1: session_new: session 0
debug1: session_open: channel 0
debug1: session_open: session 0: link with channel 0
debug1: server_input_channel_open: confirm session
debug1: server_input_global_request: rtype [email protected] want_reply 0
debug1: server_input_channel_req: channel 0 request pty-req reply 1
debug1: session_by_channel: session 0 channel 0
debug1: session_input_channel_req: session 0 req pty-req
debug1: Allocating pty.
debug1: session_new: session 0
debug1: session_pty_req: session 0 alloc /dev/ttys001
debug1: Ignoring unsupported tty mode opcode 37 (0x25)
debug1: Ignoring unsupported tty mode opcode 52 (0x34)
debug1: Ignoring unsupported tty mode opcode 71 (0x47)
debug1: server_input_channel_req: channel 0 request env reply 0
debug1: session_by_channel: session 0 channel 0
debug1: session_input_channel_req: session 0 req env
debug1: server_input_channel_req: channel 0 request shell reply 1
debug1: session_by_channel: session 0 channel 0
debug1: session_input_channel_req: session 0 req shell
debug1: Setting controlling tty using TIOCSCTTY.

答案1

ibagur 的答案很接近,但他把关键点颠倒了。远程系统的公钥必须在您的本地~/.ssh/known_hosts文件中。 你的公钥必须位于远程系统的~/.ssh/**authorized_keys文件中。与上述帖子相反,authorized_keys文件必须有 600 个权限。执行的步骤如下

  1. ~/.ssh使用命令将您的公钥复制到远程系统的目录scp id_rsa.pub username@remotehost:/path/to/home/username/.ssh/mykey.tmp,确保您使用的文件名在远程系统上是唯一的。如果出现提示,请在验证远程系统的密钥确实是远程系统的正确密钥后接受远程系统的密钥(信任该密钥)。这会将远程系统的公钥添加到您的~/.ssh/known_hosts文件中。
  2. 使用密码验证登录远程系统。
  3. 将目录更改为~/.ssh使用cd .ssh
  4. 在“~/.ssh/authorized_keys file usingcat mykey.tmp >> authorized_keys”中安装你的密钥
  5. 确保authorized_keys文件模式为 600,使用chmod 600 authorized_keys
  6. 注销并测试,您将不再需要输入密码。

如果仍然有问题,您可以尝试使用 登录到远程系统以ssh -vv获取一些调试输出,但根据我的经验,客户端不会提供太多有用的信息。您可能需要按照原始问题中所述,在调试模式下运行远程系统的 SSH 守护程序。

答案2

好的,感谢大家在评论中提供的提示和建议。我终于成功了。Mountain Lion 用户目录(而不是 .ssh 和 authorized_keys 目录)的权限存在问题。因此,我总结了以下我采取的步骤,以防有人遇到类似问题:

  1. 修复 Mountain Lion 目标机器(我使用 server/backuppc 用户 ssh 连接的机器)上的权限。
  2. 确保 Mountain Lion 用户目录具有 755 权限。
  3. .ssh如果您之前创建过该目录,请删除它及其内容。
  4. 再次创建.ssh具有 700 个权限的目录以及一组新的 rsa 密钥供 Mountain Lion 用户使用ssh-keygen
  5. 将 Mountain Lion 用户公钥添加到服务器known_hosts文件。
  6. 在文件上添加服务器公钥authorized_keys(不要在 Mountain Lion 上使用“authorized_keys2”,并将用户许可设置为 644)。

答案3

我相信该authorized_keys文件必须将权限设置为 644。

如果权限是其他任何内容,则会完全忽略文件的存在,并且您将进入下一个身份验证方法,在本例中为密码。

相关内容