仅当我使用 -i 指定密钥时 SSH 才有效

仅当我使用 -i 指定密钥时 SSH 才有效

我有一台只允许使用密钥进行 ssh 的服务器。但是,当我尝试通过 ssh 连接到这台机器时,出现错误“权限被拒绝(公钥)”。从我服务器上的 auth.log 来看,这是在身份验证前发生的。我的密钥和 .ssh 文件夹的权限看起来很好。另外,如果我在尝试 ssh 时使用 -vv ,它似乎不会尝试使用正确的密钥文件。

唯一有效的方法是使用 -i arg 并指定密钥的路径。 (位于我的 .ssh 文件夹中)我之前安装过密钥并且没有任何问题。唯一的区别是,这次我必须将密钥文件 scp 到服务器,然后使用 cat >> 将其添加到授权密钥文件中,而不是使用 ssh-copy-id。

有谁知道我如何进一步调试或解决问题,而不是让我的服务器暂时不安全,然后使用 ssh-copy-id?

-vv 输出(由于某种原因,有效的密钥甚至没有被尝试。)

OpenSSH_6.7p1 Raspbian-5+deb8u2, OpenSSL 1.0.1k 8 Jan 2015
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug2: ssh_connect: needpriv 0
debug1: Connecting to $host [$ip address] port $port.
debug1: Connection established.
debug1: key_load_public: No such file or directory
debug1: identity file /home/ben/.ssh/id_rsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/ben/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/ben/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/ben/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/ben/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/ben/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/ben/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/ben/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.7p1 Raspbian-5+deb8u2
debug1: Remote protocol version 2.0, remote software version OpenSSH_6.7p1 Debian-5+deb8u2
debug1: match: OpenSSH_6.7p1 Debian-5+deb8u2 pat OpenSSH* compat 0x04000000
debug2: fd 3 setting O_NONBLOCK
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug2: kex_parse_kexinit: [email protected],ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha1,diffie-hellman-group1-sha1
debug2: kex_parse_kexinit: [email protected],[email protected],[email protected],ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,[email protected],[email protected],[email protected],[email protected],[email protected],ssh-ed25519,ssh-rsa,ssh-dss
debug2: kex_parse_kexinit: aes128-ctr,aes192-ctr,aes256-ctr,[email protected],[email protected],[email protected],arcfour256,arcfour128,aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,[email protected]
debug2: kex_parse_kexinit: aes128-ctr,aes192-ctr,aes256-ctr,[email protected],[email protected],[email protected],arcfour256,arcfour128,aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,[email protected]
debug2: kex_parse_kexinit: [email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],hmac-sha2-256,hmac-sha2-512,hmac-sha1,[email protected],[email protected],[email protected],[email protected],hmac-md5,hmac-ripemd160,[email protected],hmac-sha1-96,hmac-md5-96
debug2: kex_parse_kexinit: [email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],hmac-sha2-256,hmac-sha2-512,hmac-sha1,[email protected],[email protected],[email protected],[email protected],hmac-md5,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: [email protected],ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group14-sha1
debug2: kex_parse_kexinit: ssh-rsa,ssh-dss,ecdsa-sha2-nistp256,ssh-ed25519
debug2: kex_parse_kexinit: aes128-ctr,aes192-ctr,aes256-ctr,[email protected],[email protected],[email protected]
debug2: kex_parse_kexinit: aes128-ctr,aes192-ctr,aes256-ctr,[email protected],[email protected],[email protected]
debug2: kex_parse_kexinit: [email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],hmac-sha2-256,hmac-sha2-512,hmac-sha1
debug2: kex_parse_kexinit: [email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],hmac-sha2-256,hmac-sha2-512,hmac-sha1
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_setup: setup [email protected]
debug1: kex: server->client aes128-ctr [email protected] none
debug2: mac_setup: setup [email protected]
debug1: kex: client->server aes128-ctr [email protected] none
debug1: sending SSH2_MSG_KEX_ECDH_INIT
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ECDSA 10:b9:0c:fa:8f:69:f2:eb:84:bd:69:32:50:1b:dd:ee
debug1: Host '$host' is known and matches the ECDSA host key.
debug1: Found key in /home/ben/.ssh/known_hosts:1
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: /home/ben/.ssh/id_rsa ((nil)),
debug2: key: /home/ben/.ssh/id_dsa ((nil)),
debug2: key: /home/ben/.ssh/id_ecdsa ((nil)),
debug2: key: /home/ben/.ssh/id_ed25519 ((nil)),
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Trying private key: /home/ben/.ssh/id_rsa
debug1: Trying private key: /home/ben/.ssh/id_dsa
debug1: Trying private key: /home/ben/.ssh/id_ecdsa
debug1: Trying private key: /home/ben/.ssh/id_ed25519
debug2: we did not send a packet, disable method
debug1: No more authentication methods to try.
Permission denied (publickey).

目录和内容的详细信息.ssh

ls - ld
drwxr-xr-x 2 $user $user .

ls -l
total 16
-rw------- 1 $user $user 733 May 3 16:27 authorized_keys
-rw------- 1 $user $user 3243 May 9 15:33 key 
-rw-r--r-- 1 $user $user 751 May 9 15:33 key.pub 
-rw-r--r-- 1 $user $user 444 May 9 15:31 known_hosts

答案1

据我所知,ssh仅搜索名称为 的键id_rsaid_dsa以及其他一些键,它们都id_以问题的输出中显示的方式开始。如果您有名为其他名称的密钥,则必须在命令行或 ssh 配置文件中指定它们。

将文件重命名key为 ssh 搜索的内容,或者.ssh/config使用相关节进行更新,或者使用该-i选项。

你可以使用这样的东西.ssh/config

host my.target.server
IdentityFile ~/.ssh/key

您还可以使用,

host *
IdentityFile ~/.ssh/key

强制 ssh 用于~/.ssh/key所有连接。

id_dsa将密钥文件重命名为或虽然可能更容易id_rsa(假设该文件实际上key在输出中被调用)。

答案2

看看MaxAuthTries中的指令/etc/ssh/sshd_config。默认值设置为 6。如果您在使用该选项显式设置的密钥之前尝试了多个本地密钥-i,则如果尝试次数超过 的值,登录可能会被拒绝MaxAuthTries

欲了解更多信息,另请参阅man sshd_config

相关内容