我有两个主机
H1 具有ip=10.10.10.15
,OS=Windows Server 2016
并且username=administrator
H2 与 ip=10.10.10.16
,OS=Centos7
和username=user
我尝试在 H1 和 H2 之间建立 ssh 连接。
我的意思是,当我从 Windows 机器上录制时:,我不需要录制密码,而且我可以直接进行远程访问ssh [email protected]
user
使用生成密钥后ssh-keygen
,我尝试使用并输入密码ssh-copy-id -i id_rsa.pub [email protected]
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: ".ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.
但控制台总是要求输入密码user
有没有什么办法可以解决这个问题?
编辑 :
这是输出ssh -v [email protected]
OpenSSH_7.4p1 Debian-10+deb9u6, OpenSSL 1.0.2r 26 Feb 2019
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to 10.10.1.16 [10.10.1.16] port 22.
debug1: Connection established.
debug1: identity file /home/administrator/.ssh/id_rsa type 1
debug1: key_load_public: No such file or directory
debug1: identity file /home/administrator/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/administrator/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/administrator/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/administrator/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/administrator/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/administrator/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/administrator/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.4p1 Debian-10+deb9u6
debug1: Remote protocol version 2.0, remote software version OpenSSH_7.4
debug1: match: OpenSSH_7.4 pat OpenSSH* compat 0x04000000
debug1: Authenticating to 10.10.10.16:22 as 'user'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
debug1: kex: server->client cipher: [email protected] MAC: <implicit> compression: none
debug1: kex: client->server cipher: [email protected] MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:gIsbKkZPUqXJXkIwkjm0rNe+BS98+J0fQ+acbNgEzzI
debug1: Host '10.10.10.16' is known and matches the ECDSA host key.
debug1: Found key in /home/administrator/.ssh/known_hosts:25
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<rsa-sha2-256,rsa-sha2-512>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug1: Next authentication method: gssapi-keyex
debug1: No valid Key exchange context
debug1: Next authentication method: gssapi-with-mic
debug1: Unspecified GSS failure. Minor code may provide more information
No Kerberos credentials available (default cache: FILE:/tmp/krb5cc_1000)
debug1: Unspecified GSS failure. Minor code may provide more information
No Kerberos credentials available (default cache: FILE:/tmp/krb5cc_1000)
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /home/administrator/.ssh/id_rsa
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug1: Trying private key: /home/administrator/.ssh/id_dsa
debug1: Trying private key: /home/administrator/.ssh/id_ecdsa
debug1: Trying private key: /home/administrator/.ssh/id_ed25519
debug1: Next authentication method: password
sshd 日志的输出
May 14 14:08:12 dockerjava sshd[17170]: Authentication refused: bad ownership or modes for file /home/user/.ssh/authorized_keys
该文件是
-rw-rw-r--. 1 user user 1999 May 14 14:03 authorized_keys
-rw-------. 1 user user 1675 May 14 12:37 id_rsa
-rw-r--r--. 1 user user 398 May 14 12:37 id_rsa.pub
-rw-r--r--. 1 user user 1314 May 14 14:02 known_hosts
答案1
这说明了一切:
5 月 14 日 14:08:12 dockerjava sshd[17170]: 身份验证被拒绝:文件 /home/user/.ssh/authorized_keys 的所有权或模式错误
授权密钥文件和.ssh 目录必须只有用户可读。
登录到您的服务器并运行:
chown -R user:user ~/.ssh
chmod 700 ~/.ssh
chmod 600 ~/.ssh/*