Windows ssh: 权限被拒绝 (公钥)

4
我尝试使用公钥ssh到远程客户端,但是一直出现权限被拒绝(publickey)的错误,无法解决。我已经尝试了所有在线上的解决方案,但仍然无法工作。我已将某些信息用*和ip_add替换。如何解决这个问题?
    PS C:\WINDOWS\system32> ssh -v -i C:/Users/*/.ssh/id_rsa administrator@ip_add
    OpenSSH_for_Windows_7.6p1, LibreSSL 2.6.4
    debug1: Connecting to ip_add [ip_add] port 22.
    debug1: Connection established.
    debug1: identity file C:/Users/*/.ssh/id_rsa type 0
    debug1: key_load_public: No such file or directory
    debug1: identity file C:/Users/*/.ssh/id_rsa-cert type -1
    debug1: Local version string SSH-2.0-OpenSSH_for_Windows_7.6
    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 ip_add:22 as 'administrator'
    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:qjxnj9GZZ6kvey+qvHsEoJYnLTz4N82DtnmFMjRlrq8
    debug1: Host '192.168.1.135' is known and matches the ECDSA host key.
    debug1: Found key in C:\\Users\\*/.ssh/known_hosts:2
    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: pubkey_prepare: ssh_get_authentication_socket: No such file or directory
    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
    debug1: Next authentication method: publickey
    debug1: Offering public key: RSA SHA256:+ymSxB9gd+AJe9wYj0WgW70Qk+x4fWxEQhk1JCQyKWE C:/Users/*/.ssh/id_rsa
    debug1: Authentications that can continue: publickey
    debug1: No more authentication methods to try.
    administrator@ip_add: Permission denied (publickey).
5个回答

1
在Unix/Linux中,以“.”开头的文件或目录是隐藏的,但在Windows中不是。 .ssh 是一个常规的文件或目录名称。它可能是隐藏的,但你需要设置隐藏属性,例如使用命令 attrib +h .ssh。 另外,无需指定-i identityfilename,因为它默认为C:\users\<user>\.ssh\known_hosts。 你展示的日志也确认了主机“ip_add”在该文件中被找到。我想说的是密钥不匹配。解决最简单的方法是使用记事本或任何文本编辑器删除带有主机名或IP地址“ip_add”的行,并重复执行ssh -v administrator@ip_add。 如果存在具有该名称或地址的主机,这将提示你。
The authenticity of host 'ip_add (192.168.1.135)' can't be established.
ECDSA key fingerprint is SHA256:JxuqbzMam5Mgpa52B+xUsSd/3F6kb+yghZKnDX6ipQU.
Are you sure you want to continue connecting (yes/no/[fingerprint])?

当你回答时,主机将再次添加到known_hosts文件中,这一次会有正确的指纹。连接应该会建立。


0

这是一个晚回答,但我遇到了同样的问题

Windows无法看到隐藏的文件夹

首先(但与问题无关):避免使用Power Shell(它太强大了),使用简单的CMD或最好使用Git Bash(https://gitforwindows.org/

其次,您的语法几乎正确,但您将密钥放在了隐藏文件夹C:\Users\yourname\.ssh\id_rsa中,您应该将其放在不隐藏的文件夹中,如此C:\Users\yourname\ssh\id_rsa,然后您可以像这样访问它:

C:\WINDOWS\system32> cd C:\Users\yourname\

C:\Users\yourname/> ssh <user>@<ssh_server> -p 22 -i ~/ssl/id_rsa

你从哪里得知的?

debug1: No more authentication methods to try.

这意味着您的主机和远程主机上没有相同的密钥,或者这里有一个密钥而那里没有(或者这些的组合),它已经检查了所有密钥并表示“没有更多可尝试的密钥”。 您必须以系统可以读取的方式在两个位置放置相同的密钥。请注意以下两行:

debug1: identity file C:/Users/*/.ssh/id_rsa type 0 
debug1: key_load_public: No such file or directory

他无法读取本地隐藏文件夹中的密钥。因此,将其移动到非隐藏文件夹中即可解决问题。


0

我最近在Windows 10上遇到了类似的问题,并找到了根本原因(有点)和解决方案。 我的工具链包括: 使用ssh协议和密钥对的git的Visual Studio 2019 也使用git的Sourcetree 一个私人的bitbucket服务器。

我的情况是这样的:我安装了使用我的密钥对的ssh协议的git的Visual Studio 2019,并成功地使用它,然后我安装了Sourcetree(以获取hunk提交而不是整个文件提交),Sourcetree工作了,但Visual Studio不能再使用git同步/提交。

解决问题的关键是添加此环境变量:GIT_SSH_COMMAND=ssh -vvv

重新启动Visual Studio后,我可以获得有关使用git的ssh失败的详细信息。

重要的调试输出是这样的: debug1: send_pubkey_test: no mutual signature algorithm

这时我意识到我的密钥对和bitbucket服务器ssh算法之间存在不兼容。我没有诊断确切的问题(我认为是由于过时的RSA密钥),而是简单地创建了一个新的密钥对,使用: ssh-keygen -t ed25519 然后上传到bitbucket服务器,"bob's your uncle"

花了12个小时找到问题,只用了2分钟修复。


0

还有一个问题我在其他地方没有看到过的,与authorized_keys文件的文件格式有关,它需要是UTF8(无BOM)。

我正在使用自动化流程创建这个文件的一部分:

echo "<my_pub_key" > ~/.ssh/authorized_keys

这将创建一个UTF16-BOM文件类型,Windows OpenSSH可以读取该文件类型,并且您可以在日志文件中看到公钥,但它将无法匹配私钥。

解决方案是使用默认情况下使用UTF8的IO.File。

[IO.File]::WriteAllLines(~\.ssh\authorized_keys, "<my_pub_key")

我希望这能帮助到其他人,检查一下你的authorized_keys文件格式,它让我困扰了很多个小时


0

我遇到了同样的错误,搜索了很长时间,更改了私钥的权限,创建了一个新的非管理员用户进行连接,并尝试了其他一些方法。参考了以下链接(以及其他链接):
https://geekrewind.com/how-to-set-up-ssh-key-login-with-windows-11/
https://learn.microsoft.com/en-us/windows-server/administration/openssh/openssh_keymanagement

对我来说,问题在于我使用puttygen和OpenSSH创建了我的密钥对,而Windows上的OpenSSH不喜欢我的公钥格式(我已经将私钥导出为.pem格式)。

在我的authorized_keys文件中,原始格式如下:

---- BEGIN SSH2 PUBLIC KEY ----
Comment: "xxxx"
xxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxx
---- END SSH2 PUBLIC KEY ----

将其更改为:

ssh-rsa xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

全部在一行上,没有换行符,没有注释,一切正常。

我知道这是一个旧的线程,但希望这能帮助某些人。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接