OpenSSH for windows

  

Windows Server 2019 和 Windows 10 1809 以上版本内置支持OpenSSH,除此以外只能手动安装

 

https://docs.microsoft.com/en-us/windows-server/administration/openssh/openssh_install_firstuse

 

 

 

本人只能手动安装OpenSSH服务

 

https://github.com/PowerShell/Win32-OpenSSH/releases

 

解压到C:\Program Files\OpenSSH目录

 

 

进到OpenSSH目录下执行

 

powershell.exe -ExecutionPolicy Bypass -File install-sshd.ps1

 

打开防火墙22号端口

 

配置服务自启动

 

sc config sshd start=auto

 

启动服务

 

net start sshd

 

进行免密登录,最好生成rsa格式公钥,windows暂时不支持ssh-copy-id,只能手动添加公钥

cd C:\Users\gfw
mkdir .ssh

手动copy公钥至authorized_keys文件中

 

 

 

 

 

 

Windows sshd Server端的默认配置文件路径为: C:\ProgramData\ssh\sshd_config,里面还保存主机身份信息,日志,pid文件

 

Windows默认OpenSSH路径为 C:\Windows\System32\OpenSSH

 

 

 

Path环境变量包含此路径

 

 

 

登录Windows域用户方式

ssh -l liuz@yangche.cn 192.168.8.1

 

可以通过设置AllowUsers,DenyUsers,AllowGroups,DenyGroups

 

Windows Server 配置

 

  1. 复制代码
    Port 22
    PubkeyAuthentication yes
    PasswordAuthentication no
    PermitEmptyPasswords no
    AuthorizedKeysFile %programdata%/ssh/administrators_authorized_keys
    
    # PasswordAuthentication no 的原因是如果密钥认证失败,ssh使用密码认证,jenkins会卡死,设为no,jenkins会立即返回失败
    复制代码

     

  2. Windows Server 即使配置 .ssh/authorized_keys,连接是依然显示无公钥,此为权限问题
    https://github.com/PowerShell/Win32-OpenSSH/wiki/OpenSSH-utility-scripts-to-fix-file-permissions
    进入C:\Program Files\OpenSSH,执行FixHostFilePermissions.ps1【使用PowerShell运行】,命令行提示全选是,重启sshd服务后密钥连接正常
  3. 新部署服务器的时候,发现公钥无法注册,发现新版本有变动:

     

     

     

     

    https://github.com/PowerShell/Win32-OpenSSH/wiki/Security-protection-of-various-files-in-Win32-OpenSSH#administrators_authorized_keys
    Administrators用户组的用户连接公钥,默认位置为

        C:\ProgramData\ssh\administrators_authorized_keys

    并且需要设置权限,执行脚本

    复制代码
    @echo off
    icacls administrators_authorized_keys /inheritance:r
    
    icacls administrators_authorized_keys /grant SYSTEM:(F)
    
    icacls administrators_authorized_keys /grant BUILTIN\Administrators:(F)
    
    pause
    复制代码

     




  4. 修改sshd_config
    AuthorizedKeysFile %programdata%/ssh/administrators_authorized_keys

     

 

卸载OpenSSH

  1. powershell.exe -ExecutionPolicy Bypass -File uninstall-sshd.ps1

     

  2. rmdir /s /q C:\Program Files\OpenSSH

     

     

     

  3. rmdir /s -q C:\Users\Administrator\.ssh

     

     

  4.  

    rmdir /s /q C:\ProgramData\ssh

     

  5. ssh客户端配置文件也位于 %programdata%\ssh目录中,默认没有此文件,手动创建
    Host *
        StrictHostKeyChecking no

    默认StrictHostKeyChecking ask

posted @   ascertain  阅读(1949)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示