即使将密钥添加到 authorized_keys 后,SSH 仍然要求输入密码

即使将密钥添加到 authorized_keys 后,SSH 仍然要求输入密码

以下是我在本地计算机上执行的步骤:

ssh-keygen -t rsa
cat ~/.ssh/id_rsa | ssh [email protected] -p 1234 "cat > ~/.ssh/authorized_keys"

当我这样做时它仍然要求输入密码:ossh [email protected] -p 1234

[email protected]'s password:

为什么会这样?我已经将其添加到授权密钥中,但它仍然要求输入密码。sshd_config:

Port 1234
Protocol 2
SyslogFacility AUTHPRIV
PubkeyAuthentication yes
AuthorizedKeysFile      .ssh/authorized_keys
PasswordAuthentication yes
ChallengeResponseAuthentication no
GSSAPIAuthentication yes
GSSAPICleanupCredentials yes
UsePAM no
AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
AcceptEnv XMODIFIERS
Subsystem       sftp    /usr/libexec/openssh/sftp-server

结果如下-vvv

$ ssh [email protected] -p 1234 -vvv
OpenSSH_4.6p1, OpenSSL 0.9.8e 23 Feb 2007
debug2: ssh_connect: needpriv 0
debug1: Connecting to remotehost.com [12.123.123.123] port 1234.
debug1: Connection established.
debug1: identity file /c/Documents and Settings/user/.ssh/identity type -1
debug3: Not a RSA1 key file /c/Documents and Settings/user/.ssh/id_rsa.
debug2: key_type_from_name: unknown key type '-----BEGIN'
debug3: key_read: missing keytype
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug2: key_type_from_name: unknown key type '-----END'
debug3: key_read: missing keytype
debug1: identity file /c/Documents and Settings/user/.ssh/id_rsa type 1
debug1: identity file /c/Documents and Settings/user/.ssh/id_dsa type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.3
debug1: match: OpenSSH_5.3 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_4.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-g
roup-exchange-sha1,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1
debug2: kex_parse_kexinit: ssh-rsa,ssh-dss
debug2: kex_parse_kexinit: aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,arcfour1
28,arcfour256,arcfour,aes192-cbc,aes256-cbc,[email protected],aes128-c
tr,aes192-ctr,aes256-ctr
debug2: kex_parse_kexinit: aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,arcfour1
28,arcfour256,arcfour,aes192-cbc,aes256-cbc,[email protected],aes128-c
tr,aes192-ctr,aes256-ctr
debug2: kex_parse_kexinit: hmac-md5,hmac-sha1,hmac-ripemd160,hmac-ripemd160@open
ssh.com,hmac-sha1-96,hmac-md5-96
debug2: kex_parse_kexinit: hmac-md5,hmac-sha1,hmac-ripemd160,hmac-ripemd160@open
ssh.com,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-group-exchange-sha256,diffie-hellman-g
roup-exchange-sha1,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1
debug2: kex_parse_kexinit: ssh-rsa,ssh-dss
debug2: kex_parse_kexinit: aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour12
8,aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,rij
[email protected]
debug2: kex_parse_kexinit: aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour12
8,aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,rij
[email protected]
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]
debug2: kex_parse_kexinit: none,[email protected]
debug2: kex_parse_kexinit:
debug2: kex_parse_kexinit:
debug2: kex_parse_kexinit: first_kex_follows 0
debug2: kex_parse_kexinit: reserved 0
debug2: mac_init: found hmac-md5
debug1: kex: server->client aes128-cbc hmac-md5 none
debug2: mac_init: found hmac-md5
debug1: kex: client->server aes128-cbc hmac-md5 none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug2: dh_gen_key: priv key bits set: 135/256
debug2: bits set: 519/1024
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
debug3: put_host_port: [12.123.123.123]:1234
debug3: put_host_port: [remotehost.com]:1234
debug3: check_host_in_hostfile: filename /c/Documents and Settings/user/.ss
h/known_hosts
debug3: check_host_in_hostfile: match line 1
debug3: check_host_in_hostfile: filename /c/Documents and Settings/user/.ss
h/known_hosts
debug3: check_host_in_hostfile: match line 1
debug1: Host '[remotehost.com]:1234' is known and matches the RSA host key.
debug1: Found key in /c/Documents and Settings/user/.ssh/known_hosts:1
debug2: bits set: 515/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: SSH2_MSG_SERVICE_REQUEST sent
debug2: service_accept: ssh-userauth
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug2: key: /c/Documents and Settings/user/.ssh/identity (0x0)
debug2: key: /c/Documents and Settings/user/.ssh/id_rsa (0xa01a418)
debug2: key: /c/Documents and Settings/user/.ssh/id_dsa (0x0)
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mi
c,password
debug3: start over, passed a different list publickey,gssapi-keyex,gssapi-with-m
ic,password
debug3: preferred 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: Trying private key: /c/Documents and Settings/user/.ssh/identity
debug3: no such identity: /c/Documents and Settings/user/.ssh/identity
debug1: Offering public key: /c/Documents and Settings/user/.ssh/id_rsa
debug3: send_pubkey_test
debug2: we sent a publickey packet, wait for reply
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mi
c,password
debug1: Trying private key: /c/Documents and Settings/user/.ssh/id_dsa
debug3: no such identity: /c/Documents and Settings/user/.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
[email protected]'s password:

答案1

~/.ssh必须是chmod 700~/.ssh/authorized_keys必须是chmod 600。 两者都必须归您所有。

如果这些文件/目录更加宽松,SSH 将默默地恢复到密码登录,尽管从内存中,它确实记录了一些有关“坏模式”的信息。

另外,检查是否~/.ssh/authorized_keys确实包含某些内容。我感觉该命令可能创建了一个空文件。我认为您想要的是:

cat ~/.ssh/id_rsa.pub | ssh [email protected] -p 1234 "cat - > ~/.ssh/authorized_keys"

答案2

正如 Ladadadada 所指出的,您有:

cat ~/.ssh/id_rsa | ssh[电子邮件保护]-p 1234 “cat > ~/.ssh/authorized_keys”

这会将您的私钥/公钥对复制到 authorized_keys 文件中。服务器上的 authorized_keys 文件不应包含私钥。我建议如下:

  • 完全删除远程系统上的authorized_keys文件。
  • 如果尚未完成,请临时更改 .ssh 的权限以使其可写。
  • 在本地系统上,导航到 .ssh 目录并确保您有一个 id_rsa 和 id_rsa.pub 文件。ssh-keygen 命令应该已经为您创建了这两个文件
  • 执行Ladadada提供的命令:

cat ~/.ssh/id_rsa.pub | ssh[电子邮件保护]-p 1234 “cat->~/.ssh/authorized_keys”

注意:这会将您的公钥写入 authorized_keys 文件。请仔细检查以确保您的本地 id_rsa.pub 副本与 authorized_keys 文件中的密钥匹配。

您的 id_rsa.pub 密钥的内容将类似于(为简洁起见,剪切了一部分):

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDLrS9t0lEdxk52v8Jt/EJMNm9::::clipped:::::wu1WzNCvrkUDnqS/aQZJ4rR4J+GoMLxP2NT 你@somehostname

此密钥 id_rsa.pub 应该与 known_hosts 文件中的 ssh 密钥匹配。

已编辑:修复了文件名中的错误。我赞同 Ladadadada 的回答,因为他发现了这个问题,但我认为 OP 没有注意到这个问题

答案3

总结

在客户端:

  • 打开配置文件/etc/ssh/ssh_config
  • 这里寻找PreferredAuthentications
  • 确保password在后面publickey,而不是相反

就我的情况而言password,之前已经写好了publickey,所以即使我已将我的 pub_key 复制到服务器上,ssh 仍会提示我输入密码。

使用详细功能可以轻松发现此问题:

ssh -v compute@compute1 ... ... debug1: Authentications that can continue: publickey,password debug1: Next authentication method: password

正如您所见,password在尝试使用公钥之前已经进行了选择。

修改后/etc/ssh/ssh_config移动passwordpublickey

PreferredAuthentications 键盘交互,publickey,password基于主机,gssapi-with-mi

现在您无需输入密码即可登录。

答案4

根据输出,我认为您需要检查密钥的格式。如果您不确定,请查看以下网站:

http://the.earth.li/~sgtatham/putty/0.60/htmldoc/Chapter8.html

http://www.ietf.org/rfc/rfc4716.txt

相关内容