featured.png

在 Windows 上配置微软自带的 OpenSSH 服务器(并配置 pubkey 以进行身份认证和 DefaultShell)

Table of Contents

这,就是微软

你现在有一台全新安装的 Windows 计算机(Windows 10 某个版本以上)。

  1. 在设置的 应用 -> 应用和功能 中查找 可选功能 并安装 OpenSSH 服务器,安装完成之后在服务中启动:
    Start-Service sshd
    
  • 在较新版本的系统中直接在 系统 -> 可选功能 中就可发现。
  1. 准备好你的 authorized_keys 文件。注意它必须是 UTF-8 格式 ———— 如果你使用 PowerShell 进行公钥文件内容的写入,需要 指定所使用的编码 。不然之后可能会出现:

    debug3: mm_answer_keyallowed entering
    debug3: mm_answer_keyallowed: key_from_blob: 0000023B443DEBC0
    debug1: trying public key file C:\\Users\\nemo/.ssh/authorized_keys
    debug2: C:\\Users\\nemo/.ssh/authorized_keys:1: check options: '\377\376s'
    debug2: C:\\Users\\nemo/.ssh/authorized_keys:1: advance: ''
    debug3: mm_answer_keyallowed: publickey authentication: RSA key is not allowed
    Failed publickey for nemo from 192.168.42.20 port 59924 ssh2: RSA SHA256:KHuJelvRiIwx1C9mlrm1pBD11cgHFuW4lvsYT8zuIJk
    debug3: mm_request_send entering: type 23
    ...
    debug3: mm_answer_keyallowed entering
    debug3: mm_answer_keyallowed: key_from_blob: 0000023B443DE7D0
    debug1: trying public key file C:\\Users\\nemo/.ssh/authorized_keys
    debug2: C:\\Users\\nemo/.ssh/authorized_keys:1: check options: '\377\376s'
    debug2: C:\\Users\\nemo/.ssh/authorized_keys:1: advance: ''
    debug3: mm_answer_keyallowed: publickey authentication: ED25519 key is not allowed
    Failed publickey for nemo from 192.168.42.20 port 59924 ssh2: ED25519 SHA256:kqIfo5LYdYJc3rvTwOM8vVNJ8DhHl/6BSwOqNcsH6Fc
    debug3: mm_request_send entering: type 23
    
  2. 参照 这里 继续进行配置。对于管理员组用户,若是不喜欢在 $Env:ProgramData\ssh\ 下放置你的公钥文件,可以将其中 sshd_config 的如下内容注释:

    Match Group administrators
           AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys
    

    当然,有人对微软的这个做法提出了 质疑

    • 修改完配置文件别忘了 Restart-Service sshd
  3. 进行 DefaultShell 的 设置

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

    其中的 powershell.exe 当然也可以换成别的。

  4. 你已经可以准备摇滚了!

Nemo Xiong avatar
Nemo Xiong
我永远喜欢妃爱
comments powered by Disqus