Windows-Server-2008

具有不同使用者的兩台主機之間的 SSH 連接

  • May 14, 2019

我有兩個主機

H1 與ip=10.10.10.15,OS=Windows Server 2016username=administrator

H2 與 ip=10.10.10.16,OS=Centos7username=user

我嘗試在 H1 和 H2 之間創建 ssh 連接。

我的意思是,當我從 Windows 機器上錄製時:ssh user@10.10.1.16,我不需要錄製密碼,user並且直接我可以遠端訪問

生成密鑰後ssh-keygen,我嘗試使用ssh-copy-id -i id_rsa.pub user@10.10.1.16並輸入密碼

/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
user@10.10.10.16's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'user@10.10.10.16'"
and check to make sure that only the key(s) you wanted were added.

但控制台總是要求密碼user

有沒有辦法解決這個問題?

編輯 :

這是的輸出ssh -v user@10.10.1.16

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: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com 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

這說明了一切:

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/*

引用自:https://serverfault.com/questions/967204