如何使用不同的用户名设置 SSH 无密码登录?

如何使用不同的用户名设置 SSH 无密码登录?

我有 Windows 10 系统,用户名是“WindowsGod”。真有趣。:-)
我有一台名为“MyDisk”的 Synology NAS,它使用 Linux 作为操作系统,但我的用户名是“DiskMaster”。
据我所知,我已经正确安装了 SSH,并且我一直在尝试各种解决方案来执行命令“ ssh diskmaster@mydisk”,我希望我不必登录。这失败了...
所以我创建了一个公钥和私钥,并为用户 diskmaster 提供了一个“~/.ssh/authorized_keys”,我希望这就足够了。但事实并非如此,因为它一直要求我输入密码。我怀疑这与不同的用户名有关...
我是对的吗?

无论如何,要查看完整的调试概述:

OpenSSH_for_Windows_8.1p1, LibreSSL 3.0.2
debug3: Failed to open file:C:/Users/windo/.ssh/config error:2
debug3: Failed to open file:C:/ProgramData/ssh/ssh_config error:2
debug2: resolving "mydisk" port 22
debug2: ssh_connect_direct
debug1: Connecting to mydisk [fe80::211:32ff:fee1:7262%3] port 22.
debug1: Connection established.
debug3: Failed to open file:C:/Users/windo/.ssh/id_rsa error:2
debug3: Failed to open file:C:/Users/windo/.ssh/id_rsa.pub error:2
debug1: identity file C:\\Users\\windo/.ssh/id_rsa type -1
debug3: Failed to open file:C:/Users/windo/.ssh/id_rsa-cert error:2
debug3: Failed to open file:C:/Users/windo/.ssh/id_rsa-cert.pub error:2
debug1: identity file C:\\Users\\windo/.ssh/id_rsa-cert type -1
debug3: Failed to open file:C:/Users/windo/.ssh/id_dsa error:2
debug3: Failed to open file:C:/Users/windo/.ssh/id_dsa.pub error:2
debug1: identity file C:\\Users\\windo/.ssh/id_dsa type -1
debug3: Failed to open file:C:/Users/windo/.ssh/id_dsa-cert error:2
debug3: Failed to open file:C:/Users/windo/.ssh/id_dsa-cert.pub error:2
debug1: identity file C:\\Users\\windo/.ssh/id_dsa-cert type -1
debug1: identity file C:\\Users\\windo/.ssh/id_ecdsa type 2
debug3: Failed to open file:C:/Users/windo/.ssh/id_ecdsa-cert error:2
debug3: Failed to open file:C:/Users/windo/.ssh/id_ecdsa-cert.pub error:2
debug1: identity file C:\\Users\\windo/.ssh/id_ecdsa-cert type -1
debug3: Failed to open file:C:/Users/windo/.ssh/id_ed25519 error:2
debug3: Failed to open file:C:/Users/windo/.ssh/id_ed25519.pub error:2
debug1: identity file C:\\Users\\windo/.ssh/id_ed25519 type -1
debug3: Failed to open file:C:/Users/windo/.ssh/id_ed25519-cert error:2
debug3: Failed to open file:C:/Users/windo/.ssh/id_ed25519-cert.pub     error:2
debug1: identity file C:\\Users\\windo/.ssh/id_ed25519-cert type -1
debug3: Failed to open file:C:/Users/windo/.ssh/id_xmss error:2
debug3: Failed to open file:C:/Users/windo/.ssh/id_xmss.pub error:2
debug1: identity file C:\\Users\\windo/.ssh/id_xmss type -1
debug3: Failed to open file:C:/Users/windo/.ssh/id_xmss-cert error:2
debug3: Failed to open file:C:/Users/windo/.ssh/id_xmss-cert.pub error:2
debug1: identity file C:\\Users\\windo/.ssh/id_xmss-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_for_Windows_8.1
debug1: Remote protocol version 2.0, remote software version OpenSSH_8.2
debug1: match: OpenSSH_8.2 pat OpenSSH* compat 0x04000000
debug2: fd 3 setting O_NONBLOCK
debug1: Authenticating to mydisk:22 as 'diskmaster'
debug3: hostkeys_foreach: reading file "C:\\Users\\windo/.ssh/known_hosts"
debug3: record_hostkey: found key type ECDSA in fileC:\\Users\\windo/.ssh/known_hosts:1
debug3: load_hostkeys: loaded 1 keys from mydisk
debug3: Failed to open file:C:/Users/windo/.ssh/known_hosts2 error:2
debug3: Failed to open file:C:/ProgramData/ssh/ssh_known_hosts error:2
debug3: Failed to open file:C:/ProgramData/ssh/ssh_known_hosts2 error:2
debug3: order_hostkeyalgs: prefer hostkeyalgs: [email protected],[email protected],[email protected],ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
debug3: send packet: type 20
debug1: SSH2_MSG_KEXINIT sent
debug3: receive packet: type 20
debug1: SSH2_MSG_KEXINIT received
debug2: local client KEXINIT proposal
debug2: KEX algorithms: curve25519-sha256,[email protected],ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group14-sha256,diffie-hellman-group14-sha1,ext-info-c
debug2: host key algorithms: [email protected],[email protected],[email protected],ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,[email protected],[email protected],[email protected],[email protected],ssh-ed25519,rsa-sha2-512,rsa-sha2-256,ssh-rsa
debug2: ciphers ctos: [email protected],aes128-ctr,aes192-ctr,aes256-ctr,[email protected],[email protected]
debug2: ciphers stoc: [email protected],aes128-ctr,aes192-ctr,aes256-ctr,[email protected],[email protected]
debug2: MACs ctos: [email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],hmac-sha2-256,hmac-sha2-512,hmac-sha1
debug2: MACs stoc: [email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],hmac-sha2-256,hmac-sha2-512,hmac-sha1
debug2: compression ctos: none,[email protected],zlib
debug2: compression stoc: none,[email protected],zlib
debug2: languages ctos:
debug2: languages stoc:
debug2: first_kex_follows 0
debug2: reserved 0
debug2: peer server KEXINIT proposal
debug2: KEX algorithms: curve25519-sha256,[email protected],diffie-hellman-group-exchange-sha256,diffie-hellman-group14-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512
debug2: host key algorithms: rsa-sha2-512,rsa-sha2-256,ssh-rsa,ecdsa-sha2-nistp256,ssh-ed25519
debug2: ciphers ctos: aes128-ctr,[email protected],aes192-ctr,aes256-ctr,[email protected],[email protected]
debug2: ciphers stoc: aes128-ctr,[email protected],aes192-ctr,aes256-ctr,[email protected],[email protected]
debug2: MACs ctos: hmac-sha2-256,[email protected],hmac-sha2-512,[email protected],[email protected],[email protected]
debug2: MACs stoc: hmac-sha2-256,[email protected],hmac-sha2-512,[email protected],[email protected],[email protected]
debug2: compression ctos: none,[email protected]
debug2: compression stoc: none,[email protected]
debug2: languages ctos:
debug2: languages stoc:
debug2: first_kex_follows 0
debug2: reserved 0
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
debug3: send packet: type 30
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug3: receive packet: type 31
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:wkeoHiapgWG2GUEcQKZ5UJjLOg9INYhfvU1EM9U2ibM
debug3: hostkeys_foreach: reading file "C:\\Users\\windo/.ssh/known_hosts"
debug3: record_hostkey: found key type ECDSA in file C:\\Users\\windo/.ssh/known_hosts:1
debug3: load_hostkeys: loaded 1 keys from mydisk
debug3: Failed to open file:C:/Users/windo/.ssh/known_hosts2 error:2
debug3: Failed to open file:C:/ProgramData/ssh/ssh_known_hosts error:2
debug3: Failed to open file:C:/ProgramData/ssh/ssh_known_hosts2 error:2
debug3: hostkeys_foreach: reading file "C:\\Users\\windo/.ssh/known_hosts"
debug3: record_hostkey: found key type ECDSA in file C:\\Users\\windo/.ssh/known_hosts:2
debug3: load_hostkeys: loaded 1 keys from fe80::211:32ff:fee1:7262%3
debug3: Failed to open file:C:/Users/windo/.ssh/known_hosts2 error:2
debug3: Failed to open file:C:/ProgramData/ssh/ssh_known_hosts error:2
debug3: Failed to open file:C:/ProgramData/ssh/ssh_known_hosts2 error:2
debug1: Host 'mydisk' is known and matches the ECDSA host key.
debug1: Found key in C:\\Users\\windo/.ssh/known_hosts:1
debug3: send packet: type 21
debug2: set_newkeys: mode 1
debug1: rekey out after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug3: receive packet: type 21
debug1: SSH2_MSG_NEWKEYS received
debug2: set_newkeys: mode 0
debug1: rekey in after 134217728 blocks
debug3: unable to connect to pipe \\\\.\\pipe\\openssh-ssh-agent, error:     2
debug1: pubkey_prepare: ssh_get_authentication_socket: No such file or     directory
debug1: Will attempt key: C:\\Users\\windo/.ssh/id_rsa
debug1: Will attempt key: C:\\Users\\windo/.ssh/id_dsa
debug1: Will attempt key: C:\\Users\\windo/.ssh/id_ecdsa ECDSA SHA256:mHcwYJa38rVbir+7Jfa4/qes0Vzwh0CPs7OVdIoUIY4
debug1: Will attempt key: C:\\Users\\windo/.ssh/id_ed25519
debug1: Will attempt key: C:\\Users\\windo/.ssh/id_xmss
debug2: pubkey_prepare: done
debug3: send packet: type 5
debug3: receive packet: type 7
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519,[email protected],ssh-rsa,rsa-sha2-256,rsa-sha2-512,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,[email protected]>
debug3: receive packet: type 6
debug2: service_accept: ssh-userauth
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug3: send packet: type 50
debug3: receive packet: type 51
debug1: Authentications that can continue: publickey,password
debug3: start over, passed a different list publickey,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:\\Users\\windo/.ssh/id_rsa
debug3: no such identity: C:\\Users\\windo/.ssh/id_rsa: No such file or directory
debug1: Trying private key: C:\\Users\\windo/.ssh/id_dsa
debug3: no such identity: C:\\Users\\windo/.ssh/id_dsa: No such file or directory
**debug1: Offering public key: C:\\Users\\windo/.ssh/id_ecdsa ECDSA SHA256:mHcwYJa38rVbir+7Jfa4/qes0Vzwh0CPs7OVdIoUIY4**
debug3: **send packet: type 50**
debug2: we sent a publickey packet, wait for reply
debug3: **receive packet: type 51**
**debug1: Authentications that can continue: publickey,password**
debug1: Trying private key: C:\\Users\\windo/.ssh/id_ed25519
debug3: no such identity: C:\\Users\\windo/.ssh/id_ed25519: No such file or directory
debug1: Trying private key: C:\\Users\\windo/.ssh/id_xmss
debug3: no such identity: C:\\Users\\windo/.ssh/id_xmss: No such file or directory
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
debug3: failed to open file:C:/dev/tty error:3
debug1: read_passphrase: can't open /dev/tty: No such file or directory
diskmaster@mydisk's password:

这些信息太多了,而且有很多失败。但我确实注意到debug1: Offering public key: C:\\Users\\windo/.ssh/id_ecdsa ECDSA SHA256它找到了正确的私钥。发送了数据包类型 50,返回了类型 51。
奇怪的是,提到的文件包含私钥,但 SSH 似乎认为它是公钥?奇怪...

答案1

有可能。也许是区分大小写的问题?您提到了“DiskMaster”和“diskmaster”。如果 Synology 是任何类型的 *nix,那么用户名肯定区分大小写。

如果您使用剪切和粘贴,请检查 authorized_keys 文件(就像我最近做的那样),以确保新添加的密钥列表中没有任何不需要的换行符。

您可以检查 Synology 上的日志以深入了解登录提示的来源。您可能需要在通常位于 /etc 或 /etc/ssh 中的 sshd_config 中启用日志记录。

还要考虑远程授权密钥上的文件权限。您是否已将远程添加到已知主机列表中?(登录前会提示您执行此操作)。

最后,确保远程解析源地址的主机名,以便它与authorized_keys文件中的主机名匹配。

答案2

如果您已经在 SSH 命令中指定了远程用户名,那么拥有不同的用户名就无关紧要了。默认用户名应该是您的本地用户名。

但是,您需要为本地用户生成密钥对,并将公钥复制到远程用户主目录中的相应 .ssh/authorized_keys 文件中。此外,您必须确保在生成密钥对时没有指定密码。

如果私钥没有存储在预期位置,您实际上可以手动指定私钥。这可能因 ssh 客户端软件而异。

ssh -vvv -i path/myPrivate.key diskmaster@mydisk 

仅使用 -vvv 查看调试消息以帮助排除故障。

我怀疑基于密钥的身份验证失败了,因此它又回到了密码方法。祝你好运。

相关内容