windows使用sshd

安装并启用openssh

https://docs.microsoft.com/zh-cn/windows-server/administration/openssh/openssh_overview

1
2
3
Start-Service sshd
Stop-Service sshd
Restart-Service sshd

配置文件:%programdata%\ssh\sshd_config

开启密钥登录

https://zhuanlan.zhihu.com/p/404179039

  1. 更改authorized_keys文件权限:禁用继承,删除所有权限,添加SYSTEM和Administrators的完全控制权限

    1
    icacls.exe "C:\Users\username\.ssh\authorized_keys" /inheritance:r /grant "Administrators:F" /grant "SYSTEM:F"

  2. 更改sshd_config,改3处

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    #允许公钥授权访问,确保条目不被注释
    PubkeyAuthentication yes

    #授权文件存放位置,确保条目不被注释
    AuthorizedKeysFile .ssh/authorized_keys

    #可选,关闭密码登录,提高安全性
    PasswordAuthentication no

    #注释掉默认授权文件位置,确保以下条目被注释
    #Match Group administrators
    # AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys

重启sshd

FAQ

  1. 设置了文件权限,authorized_keys里也放了公钥,但还是提示:Permission denied (publickey,password,keyboard-interactive).

注意检查authorized_keys文件格式,必须是纯ASCII或者UTF-8。我是在powershell中用echo AAAA..... >> authorized_keys,来生成authorized_keys,结果powershell这样自动是utf-16(LE)格式!害我研究了一个小时!


windows使用sshd
https://jcdu.top/2022/09/12/windows使用sshd/
作者
horizon86
发布于
2022年9月12日
许可协议