跳转至内容
0
  • 版块
  • 最新
  • 标签
  • 热门
  • 用户
  • 群组
  • 版块
  • 最新
  • 标签
  • 热门
  • 用户
  • 群组
皮肤
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • 默认(Yeti)
  • 不使用皮肤
折叠

9Rivers.BBS

  1. 主页
  2. 聊天室
  3. 聊技术
  4. OpenSSH on Windows

OpenSSH on Windows

已定时 已固定 已锁定 已移动 聊技术
tech talk
4 帖子 1 发布者 277 浏览
  • 从旧到新
  • 从新到旧
  • 最多赞同
回复
  • 在新帖中回复
登录后回复
此主题已被删除。只有拥有主题管理权限的用户可以查看。
  • ww9riversW 离线
    ww9riversW 离线
    ww9rivers
    写于 最后由 ww9rivers 编辑
    #1

    SSH is a secure shell, a remote access tool that Linux/UNIX users have been using for years and years. Many projects have attempted to get SSH onto Windows over the years, with various level of success. So I was thrilled when Microsoft finally seems to embrace it and made it a feature of Windows 10 and then 11.

    Windows is such a confused and confusing beast for so long! One has to marvel at its monstrosity!

    Back to OpenSSH on Windows. I recently got myself a Windows 11 mini PC. I normally just use Remote Desktop with it. But I want to use it for coding so I would like to get SSH working with a key instead of using password.

    I thought that would be easy!

    And I would be wrong!

    Windows is dead set to deny connection using an SSH key!

    It accepts my password. But when I put this in sshd_config:

    PasswordAuthentication no
    

    It simply responds with Permission denied (publickey).

    I asked Microsoft Copilot about the error. It suggested that I should make sure that my public key is in the C:\Users\<target-username>\.ssh\authorized_keys file and make sure that it has proper permissions:

    icacls "$env:USERPROFILE\.ssh\authorized_keys" /inheritance:r
    icacls "$env:USERPROFILE\.ssh\authorized_keys" /grant "$env:USERNAME:F"
    

    We went back and forth about icacls working differently in a PowerShell window vs. Command, verifying that the permissions were right, stopping and restarting sshd service numerous times, etc.. Nothing worked! On the client (Linux) side, ssh -vvv showed that various keys were presented to the server, none were accepted.

    Copilot suggested creating a new pair of keys and ssh-copy-id the new public key to the remote host and test with that specifically. Didn't work!

    Copilot suggested to debug with an interactive sshd.exe -ddd run in Windows. That got me some complaints about UNPROTECTED PRIVATE KEY FILE!. Now that seems to indicate that sshd may also have the same issue reading private host keys when starting as a service.

    Copilot thought so, too. It said that this is the breakthrough we needed.

    It told me to open up the Command Prompt as Administrator and ran these commands:

    cd C:\ProgramData\ssh
    
    :: Remove inheritance
    icacls ssh_host_rsa_key /inheritance:r
    icacls ssh_host_ecdsa_key /inheritance:r
    icacls ssh_host_ed25519_key /inheritance:r
    
    :: Remove all existing permissions
    icacls ssh_host_rsa_key /remove:g Everyone
    icacls ssh_host_ecdsa_key /remove:g Everyone
    icacls ssh_host_ed25519_key /remove:g Everyone
    
    :: Grant SYSTEM and Administrators full control
    icacls ssh_host_rsa_key /grant SYSTEM:F
    icacls ssh_host_rsa_key /grant Administrators:F
    
    icacls ssh_host_ecdsa_key /grant SYSTEM:F
    icacls ssh_host_ecdsa_key /grant Administrators:F
    
    icacls ssh_host_ed25519_key /grant SYSTEM:F
    icacls ssh_host_ed25519_key /grant Administrators:F
    

    But sshd still complained about UNPROTECTED PRIVATE KEY FILE!.

    Copilot gave more suggestions:

    cd C:\ProgramData\ssh
    
    :: Remove all existing permissions
    icacls ssh_host_rsa_key /reset
    icacls ssh_host_ecdsa_key /reset
    icacls ssh_host_ed25519_key /reset
    
    :: Remove inheritance
    icacls ssh_host_rsa_key /inheritance:r
    icacls ssh_host_ecdsa_key /inheritance:r
    icacls ssh_host_ed25519_key /inheritance:r
    
    :: Grant SYSTEM full control
    icacls ssh_host_rsa_key /grant SYSTEM:F
    icacls ssh_host_ecdsa_key /grant SYSTEM:F
    icacls ssh_host_ed25519_key /grant SYSTEM:F
    
    :: Grant Administrators full control
    icacls ssh_host_rsa_key /grant Administrators:F
    icacls ssh_host_ecdsa_key /grant Administrators:F
    icacls ssh_host_ed25519_key /grant Administrators:F
    

    We verified permissions:

    icacls ssh_host_rsa_key
    ssh_host_rsa_key BUILTIN\Administrators:(F)
                     NT AUTHORITY\SYSTEM:(F)
    
    Successfully processed 1 files; Failed processing 0 files
    

    At one point along the way, the icacls command complained that ssh_host_rsa_key: Access is denied.

    And we did more:

    takeown /F ssh_host_rsa_key
    takeown /F ssh_host_ecdsa_key
    takeown /F ssh_host_ed25519_key
    

    Reset permissions:

    icacls ssh_host_rsa_key /reset
    icacls ssh_host_ecdsa_key /reset
    icacls ssh_host_ed25519_key /reset
    

    Remove inheritance:

    icacls ssh_host_rsa_key /inheritance:r
    icacls ssh_host_ecdsa_key /inheritance:r
    icacls ssh_host_ed25519_key /inheritance:r
    

    Grant Only SYSTEM and Administrators Full Control:

    icacls ssh_host_rsa_key /grant "SYSTEM:F"
    icacls ssh_host_rsa_key /grant "Administrators:F"
    
    icacls ssh_host_ecdsa_key /grant "SYSTEM:F"
    icacls ssh_host_ecdsa_key /grant "Administrators:F"
    
    icacls ssh_host_ed25519_key /grant "SYSTEM:F"
    icacls ssh_host_ed25519_key /grant "Administrators:F"
    

    Now, sshd.exe -ddd no longer complained in Command Prompt. But the OpenSSH service would not start anymore.

    I was frustrated enough at that point. I removed the OpenSSH feature from Windows. I found an OpenSSH Preview package with version 9.8.3.0. I tried that.

    The OpenSSH Server service process would NOT start.

    Uninstalled that. Put the regular OpsnSSH feature back in.

    The OpenSSH Server service process would NOT start.

    I gave up! Even Copilot is confused. I am going to try StackOverflow, SuperUser, or some other community.

    [EDIT] 2025-09-28 -- Posted it to Superuser.

    ww9riversW 1 条回复 最后回复
    0
    • ww9riversW ww9rivers

      SSH is a secure shell, a remote access tool that Linux/UNIX users have been using for years and years. Many projects have attempted to get SSH onto Windows over the years, with various level of success. So I was thrilled when Microsoft finally seems to embrace it and made it a feature of Windows 10 and then 11.

      Windows is such a confused and confusing beast for so long! One has to marvel at its monstrosity!

      Back to OpenSSH on Windows. I recently got myself a Windows 11 mini PC. I normally just use Remote Desktop with it. But I want to use it for coding so I would like to get SSH working with a key instead of using password.

      I thought that would be easy!

      And I would be wrong!

      Windows is dead set to deny connection using an SSH key!

      It accepts my password. But when I put this in sshd_config:

      PasswordAuthentication no
      

      It simply responds with Permission denied (publickey).

      I asked Microsoft Copilot about the error. It suggested that I should make sure that my public key is in the C:\Users\<target-username>\.ssh\authorized_keys file and make sure that it has proper permissions:

      icacls "$env:USERPROFILE\.ssh\authorized_keys" /inheritance:r
      icacls "$env:USERPROFILE\.ssh\authorized_keys" /grant "$env:USERNAME:F"
      

      We went back and forth about icacls working differently in a PowerShell window vs. Command, verifying that the permissions were right, stopping and restarting sshd service numerous times, etc.. Nothing worked! On the client (Linux) side, ssh -vvv showed that various keys were presented to the server, none were accepted.

      Copilot suggested creating a new pair of keys and ssh-copy-id the new public key to the remote host and test with that specifically. Didn't work!

      Copilot suggested to debug with an interactive sshd.exe -ddd run in Windows. That got me some complaints about UNPROTECTED PRIVATE KEY FILE!. Now that seems to indicate that sshd may also have the same issue reading private host keys when starting as a service.

      Copilot thought so, too. It said that this is the breakthrough we needed.

      It told me to open up the Command Prompt as Administrator and ran these commands:

      cd C:\ProgramData\ssh
      
      :: Remove inheritance
      icacls ssh_host_rsa_key /inheritance:r
      icacls ssh_host_ecdsa_key /inheritance:r
      icacls ssh_host_ed25519_key /inheritance:r
      
      :: Remove all existing permissions
      icacls ssh_host_rsa_key /remove:g Everyone
      icacls ssh_host_ecdsa_key /remove:g Everyone
      icacls ssh_host_ed25519_key /remove:g Everyone
      
      :: Grant SYSTEM and Administrators full control
      icacls ssh_host_rsa_key /grant SYSTEM:F
      icacls ssh_host_rsa_key /grant Administrators:F
      
      icacls ssh_host_ecdsa_key /grant SYSTEM:F
      icacls ssh_host_ecdsa_key /grant Administrators:F
      
      icacls ssh_host_ed25519_key /grant SYSTEM:F
      icacls ssh_host_ed25519_key /grant Administrators:F
      

      But sshd still complained about UNPROTECTED PRIVATE KEY FILE!.

      Copilot gave more suggestions:

      cd C:\ProgramData\ssh
      
      :: Remove all existing permissions
      icacls ssh_host_rsa_key /reset
      icacls ssh_host_ecdsa_key /reset
      icacls ssh_host_ed25519_key /reset
      
      :: Remove inheritance
      icacls ssh_host_rsa_key /inheritance:r
      icacls ssh_host_ecdsa_key /inheritance:r
      icacls ssh_host_ed25519_key /inheritance:r
      
      :: Grant SYSTEM full control
      icacls ssh_host_rsa_key /grant SYSTEM:F
      icacls ssh_host_ecdsa_key /grant SYSTEM:F
      icacls ssh_host_ed25519_key /grant SYSTEM:F
      
      :: Grant Administrators full control
      icacls ssh_host_rsa_key /grant Administrators:F
      icacls ssh_host_ecdsa_key /grant Administrators:F
      icacls ssh_host_ed25519_key /grant Administrators:F
      

      We verified permissions:

      icacls ssh_host_rsa_key
      ssh_host_rsa_key BUILTIN\Administrators:(F)
                       NT AUTHORITY\SYSTEM:(F)
      
      Successfully processed 1 files; Failed processing 0 files
      

      At one point along the way, the icacls command complained that ssh_host_rsa_key: Access is denied.

      And we did more:

      takeown /F ssh_host_rsa_key
      takeown /F ssh_host_ecdsa_key
      takeown /F ssh_host_ed25519_key
      

      Reset permissions:

      icacls ssh_host_rsa_key /reset
      icacls ssh_host_ecdsa_key /reset
      icacls ssh_host_ed25519_key /reset
      

      Remove inheritance:

      icacls ssh_host_rsa_key /inheritance:r
      icacls ssh_host_ecdsa_key /inheritance:r
      icacls ssh_host_ed25519_key /inheritance:r
      

      Grant Only SYSTEM and Administrators Full Control:

      icacls ssh_host_rsa_key /grant "SYSTEM:F"
      icacls ssh_host_rsa_key /grant "Administrators:F"
      
      icacls ssh_host_ecdsa_key /grant "SYSTEM:F"
      icacls ssh_host_ecdsa_key /grant "Administrators:F"
      
      icacls ssh_host_ed25519_key /grant "SYSTEM:F"
      icacls ssh_host_ed25519_key /grant "Administrators:F"
      

      Now, sshd.exe -ddd no longer complained in Command Prompt. But the OpenSSH service would not start anymore.

      I was frustrated enough at that point. I removed the OpenSSH feature from Windows. I found an OpenSSH Preview package with version 9.8.3.0. I tried that.

      The OpenSSH Server service process would NOT start.

      Uninstalled that. Put the regular OpsnSSH feature back in.

      The OpenSSH Server service process would NOT start.

      I gave up! Even Copilot is confused. I am going to try StackOverflow, SuperUser, or some other community.

      [EDIT] 2025-09-28 -- Posted it to Superuser.

      ww9riversW 离线
      ww9riversW 离线
      ww9rivers
      写于 最后由 编辑
      #2

      One of the frustrating thing about OpenSSH on Windows is that all the logs are in Windows Event Logs, and there's not much it does log about what is going on, even though Copilot had me change the log level in sshd_config:

      LogLevel DEBUG3
      

      Will try to get more out of the logs later.

      1 条回复 最后回复
      0
      • ww9riversW 离线
        ww9riversW 离线
        ww9rivers
        写于 最后由 编辑
        #3

        It seems that I cannot even add the OpenSSH Client feature using the Windows Settings app.

        It tries. Then it just says Couldn't add.

        Adding OpenSSH Server seems to work -- It says Added.

        Get-WindowsCapability -Online | ? Name -like 'OpenSSH*'
        
        Name  : OpenSSH.Client~~~~0.0.1.0
        State : NotPresent
        
        Name  : OpenSSH.Server~~~~0.0.1.0
        State : Installed
        

        However, there is no OpenSSH Server service in the Services app.

        Sigh!

        ww9riversW 1 条回复 最后回复
        0
        • ww9riversW ww9rivers

          It seems that I cannot even add the OpenSSH Client feature using the Windows Settings app.

          It tries. Then it just says Couldn't add.

          Adding OpenSSH Server seems to work -- It says Added.

          Get-WindowsCapability -Online | ? Name -like 'OpenSSH*'
          
          Name  : OpenSSH.Client~~~~0.0.1.0
          State : NotPresent
          
          Name  : OpenSSH.Server~~~~0.0.1.0
          State : Installed
          

          However, there is no OpenSSH Server service in the Services app.

          Sigh!

          ww9riversW 离线
          ww9riversW 离线
          ww9rivers
          写于 最后由 编辑
          #4

          Try again!

          Removing it first:

          > Remove-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
          
          Path          :
          Online        : True
          RestartNeeded : False
          
          

          Checking:

          > Get-WindowsCapability -Online | ? Name -like 'OpenSSH*'
          
          Name  : OpenSSH.Client~~~~0.0.1.0
          State : NotPresent
          
          Name  : OpenSSH.Server~~~~0.0.1.0
          State : NotPresent
          

          Install again:

          > Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
          
          Add-WindowsCapability : The operation could not be completed due to pending operations.
          At line:1 char:1
          + Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
          + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
              + CategoryInfo          : NotSpecified: (:) [Add-WindowsCapability], COMException
              + FullyQualifiedErrorId : Microsoft.Dism.Commands.AddWindowsCapabilityCommand
          

          Really?

          When in doubt, reboot!?

          Rebooted. Try again! Same message!!

          Trying to post a question in Microsoft Windows Community. I got a stern warning:

          Violation of Code of Conduct

          1 条回复 最后回复
          0
          回复
          • 在新帖中回复
          登录后回复
          • 从旧到新
          • 从新到旧
          • 最多赞同


          • 登录

          • 没有帐号? 注册

          • 登录或注册以进行搜索。
          • 第一个帖子
            最后一个帖子