windows使用sshd
安装并启用openssh
https://docs.microsoft.com/zh-cn/windows-server/administration/openssh/openssh_overview
Start-Service sshd
Stop-Service sshd
Restart-Service sshd
配置文件:%programdata%\ssh\sshd_config
开启密钥登录
https://zhuanlan.zhihu.com/p/404179039
- 更改
authorized_keys
文件权限:禁用继承,删除所有权限,添加SYSTEM和Administrators的完全控制权限
icacls.exe "C:\Users\username\.ssh\authorized_keys" /inheritance:r /grant "Administrators:F" /grant "SYSTEM:F"
- 更改sshd_config,改3处
#允许公钥授权访问,确保条目不被注释
PubkeyAuthentication yes
#授权文件存放位置,确保条目不被注释
AuthorizedKeysFile .ssh/authorized_keys
#可选,关闭密码登录,提高安全性
PasswordAuthentication no
#注释掉默认授权文件位置,确保以下条目被注释
#Match Group administrators
# AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys
重启sshd
FAQ
- 设置了文件权限,authorized_keys里也放了公钥,但还是提示:Permission denied (publickey,password,keyboard-interactive).
注意检查authorized_keys文件格式,必须是纯ASCII或者UTF-8。我是在powershell中用echo AAAA..... >> authorized_keys
,来生成authorized_keys,结果powershell这样自动是utf-16(LE)格式!害我研究了一个小时!