问题
一台windows电脑设立一个管理员用户A,并开启了sshd服务(openSSH)。
其他电脑将自己的ssh公钥添加到此电脑上,并开启ssh-agent服务。
其他电脑以用户A的身份ssh登入,如果采用密码的方法可以登入,但是采用密钥的方法登不上。
参考
https://superuser.com/questions/1445976/windows-ssh-server-refuses-key-based-authentication-from-client
https://learn.microsoft.com/en-us/windows-server/administration/openssh/openssh_keymanagement
解决方法
windows系统在%ProgramData%/ssh/sshd_config
配置文件有如下内容:
Match Group administrators
AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys
这表明对于管理员用户,公钥授权文件应该位于%ProgramData%/ssh/administrators_authorized_keys
中。
有两种解决方法:
- 将上述
sshd_config
内容注释掉,将其他电脑的ssh公钥添加到C:\Users\username\.ssh\authorized_keys
中。
#Match Group administrators
# AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys
- 添加其他电脑的ssh公钥到
%ProgramData%/ssh/administrators_authorized_keys
中,并修改%ProgramData%/ssh/administrators_authorized_keys
文件的权限。
$acl = Get-Acl C:\ProgramData\ssh\administrators_authorized_keys
$acl.SetAccessRuleProtection($true, $false)
$administratorsRule = New-Object system.security.accesscontrol.filesystemaccessrule("Administrators","FullControl","Allow")
$systemRule = New-Object system.security.accesscontrol.filesystemaccessrule("SYSTEM","FullControl","Allow")
$acl.SetAccessRule($administratorsRule)
$acl.SetAccessRule($systemRule)
$acl | Set-Acl
推荐使用第一种方法
注意
其他电脑的ssh-agent服务要打开,不然ssh连接时不会自动采用本地的ssh公钥文件。