V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
zx900930
V2EX  ›  Windows

解决 Win11 的 OpenSSH 无法用密钥登录的问题

  •  
  •   zx900930 · 2022-06-16 14:37:53 +08:00 · 2034 次点击
    这是一个创建于 652 天前的主题,其中的信息可能已经有所发展或是发生改变。

    这个问题有一阵了, 一直不知道啥原因

    检查了配置文件

    %PROGRAMDATA%/ssh/sshd_config
    
    

    里面

    PubkeyAuthentication yes
    AuthorizedKeysFile	.ssh/authorized_keys
    

    检查了在.ssh/authorized_keys 里面确实有我添加的公钥

    用 debug 模式运行 sshd, output 如下:

    debug1: trying public key file C:\\ProgramData\\ssh\\administrators_authorized_keys
    Failed publickey for xxxxxxx
    

    经检查, 根本就没有 C:\ProgramData\ssh\administrators_authorized_keys 这么个文件 我的公钥都在 .ssh/authorized_keys 里面.

    那么是什么导致 OpenSSH 去 administrators_authorized_keys 里面找公钥呢?

    翻到 sshd_config 这个配置文件的最下面, 发现有这么两行:

    Match Group administrators
           AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys
    

    直接注释掉:

    #Match Group administrators
    #       AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys
    

    重启 sshd 服务 再试试 ssh, 这次没有提示要输 password 而是直接用密钥登录了, 问题解决.

    PS: openssh 默认连进去是 cmd 而不是 powershell 可以把 default shell 改成 powershell 方便之后的操作 powershell 下运行:

    New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShell -Value "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -PropertyType String -Force
    

    重启 sshd 服务, 这下直接连进去就是 powershell 了

    4 条回复    2022-06-16 20:54:28 +08:00
    Mitt
        1
    Mitt  
       2022-06-16 15:23:20 +08:00
    所以你是本地 adminstrator 账户吗
    zx900930
        2
    zx900930  
    OP
       2022-06-16 15:39:45 +08:00
    @Mitt 是的, 是在 administrators 那个组里.
    xyfan
        3
    xyfan  
       2022-06-16 19:36:11 +08:00
    管理员账户的 key 就应该放在 administrators_authorized_keys 文件里,至少微软官方文档是这样说明的

    “Note that if the user belongs to the administrator group, %programdata%/ssh/administrators_authorized_keys is used instead.”
    https://docs.microsoft.com/en-us/windows-server/administration/openssh/openssh_server_configuration#authorizedkeysfile

    “The contents of your public key (~\.ssh\id_ed25519.pub) needs to be placed on the server into a text file called administrators_authorized_keys in C:\ProgramData\ssh\.”
    https://docs.microsoft.com/en-us/windows-server/administration/openssh/openssh_keymanagement#administrative-user
    zx900930
        4
    zx900930  
    OP
       2022-06-16 20:54:28 +08:00
    @xyfan 这个设计出发点可以理解, 是为了安全性.
    但是却和 ssh-copy-id 这样的 linux 命令行工具不兼容
    默认 openssh 的公钥也是存在~/.ssh/authorized_keys 里的.

    如果是防止普通用户越权连接
    管理员的 profile/.ssh/authorized_keys 普通用户本来就无法访问, 也不能添加 key 进去.
    普通用户的~/.ssh/authorized_keys 在它自己的 profile 里, 也无权限连接对应的 WSL 的管理员账户.

    这个放在另外一个文件里的设计就是徒增麻烦, 实际并没有增加安全性.
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5099 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 09:35 · PVG 17:35 · LAX 02:35 · JFK 05:35
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.