使用 ssh 密钥登录到 windows openssh 服务器

Posted

技术标签:

【中文标题】使用 ssh 密钥登录到 windows openssh 服务器【英文标题】:login to windows openssh server with ssh key 【发布时间】:2021-09-14 00:12:55 【问题描述】:

我在 Windows 10 上设置了 OpenSSH 服务器,通过密码登录可以正常工作。然后,我使用 Windows ssh-keygen 生成 ssh 密钥对并将 pub 复制到 C:\Users\user.ssh\authorized_keys。但是,当我尝试登录时,它仍然要求输入密码。

下面是消息。请注意,“WINDOWSBOX”是我将 pub 复制到并希望使用 ssh 密钥登录的远程 Windows 机器。

C:\Users\user>ssh -vA WINDOWSBOX
OpenSSH_for_Windows_8.1p1, LibreSSL 3.0.2
debug1: Connecting to WINDOWSBOX [192.168.29.113] port 22.
debug1: Connection established.
debug1: identity file C:\\Users\\user/.ssh/id_rsa type 0
debug1: identity file C:\\Users\\user/.ssh/id_rsa-cert type -1
debug1: identity file C:\\Users\\user/.ssh/id_dsa type -1
debug1: identity file C:\\Users\\user/.ssh/id_dsa-cert type -1
debug1: identity file C:\\Users\\user/.ssh/id_ecdsa type -1
debug1: identity file C:\\Users\\user/.ssh/id_ecdsa-cert type -1
debug1: identity file C:\\Users\\user/.ssh/id_ed25519 type -1
debug1: identity file C:\\Users\\user/.ssh/id_ed25519-cert type -1
debug1: identity file C:\\Users\\user/.ssh/id_xmss type -1
debug1: identity file C:\\Users\\user/.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_for_Windows_8.1
debug1: match: OpenSSH_for_Windows_8.1 pat OpenSSH* compat 0x04000000
debug1: Authenticating to WINDOWSBOX:22 as 'homenet\\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:EG24XBpKUVCvkaahcuzTLHT8YIphfZ3Vccs3P2IqEKs
debug1: Host 'WINDOWSBOX' is known and matches the ECDSA host key.
debug1: Found key in C:\\Users\\user/.ssh/known_hosts:11
debug1: rekey out after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey in after 134217728 blocks
debug1: pubkey_prepare: ssh_get_authentication_socket: No such file or directory
debug1: Will attempt key: C:\\Users\\user/.ssh/id_rsa RSA SHA256:sU06INNHKnYbzMZiuLogq5j/0UMlWAQuKnBKJaJtN7c
debug1: Will attempt key: C:\\Users\\user/.ssh/id_dsa
debug1: Will attempt key: C:\\Users\\user/.ssh/id_ecdsa
debug1: Will attempt key: C:\\Users\\user/.ssh/id_ed25519
debug1: Will attempt key: C:\\Users\\user/.ssh/id_xmss
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519,ssh-rsa,rsa-sha2-256,rsa-sha2-512,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,password,keyboard-interactive
debug1: Next authentication method: publickey
debug1: Offering public key: C:\\Users\\user/.ssh/id_rsa RSA SHA256:sU06INNHKnYbzMZiuLogq5j/0UMlWAQuKnBKJaJtN7c
debug1: Authentications that can continue: publickey,password,keyboard-interactive
debug1: Trying private key: C:\\Users\\user/.ssh/id_dsa
debug1: Trying private key: C:\\Users\\user/.ssh/id_ecdsa
debug1: Trying private key: C:\\Users\\user/.ssh/id_ed25519
debug1: Trying private key: C:\\Users\\user/.ssh/id_xmss
debug1: Next authentication method: keyboard-interactive
debug1: Authentications that can continue: publickey,password,keyboard-interactive
debug1: Next authentication method: password
debug1: read_passphrase: can't open /dev/tty: No such file or directory
homenet\user@WINDOWSBOX's password:

【问题讨论】:

【参考方案1】:

然后,我使用 Windows ssh-keygen 生成 ssh 密钥对并将 pub 复制到 C:\Users\user.ssh\authorized_keys。

要明确一点:

您在一台计算机上生成密钥 将公钥复制到目标计算机(如“另一台计算机”中)%USERPROFILE%/.ssh/authorized_keys

它可以在同一台计算机上工作,但看起来很奇怪。

确保您已将公钥复制为目标authorized_keys 文件中的一个 行。

【讨论】:

注册机计算机和目标计算机不同,为清楚起见重新编辑。 @user3108468 好的。你检查你的遥控器%USERPROFILE%/.ssh/authorized_keys的内容了吗?

以上是关于使用 ssh 密钥登录到 windows openssh 服务器的主要内容,如果未能解决你的问题,请参考以下文章

转载 :配置ssh密钥认证自动登录

如何配置ssh使用密钥登录,禁止口令登录

windows下如何实现ssh免密码登录

SourceTree提示ssh密钥认证失败的解决

Xshell配置ssh使用密钥公钥(publice key)登录

登录 ssh 时出现“权限太开放”