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 配置
-
Port 22 PubkeyAuthentication yes PasswordAuthentication no PermitEmptyPasswords no AuthorizedKeysFile %programdata%/ssh/administrators_authorized_keys # PasswordAuthentication no 的原因是如果密鑰認證失敗,ssh使用密碼認證,jenkins會卡死,設為no,jenkins會立即返回失敗
- 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服務后密鑰連接正常 - 新部署服務器的時候,發現公鑰無法注冊,發現新版本有變動:
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
- 修改sshd_config
AuthorizedKeysFile %programdata%/ssh/administrators_authorized_keys
卸載OpenSSH
-
powershell.exe -ExecutionPolicy Bypass -File uninstall-sshd.ps1
-
rmdir /s /q C:\Program Files\OpenSSH
-
rmdir /s -q C:\Users\Administrator\.ssh
-
rmdir /s /q C:\ProgramData\ssh
- ssh客戶端配置文件也位於 %programdata%\ssh目錄中,默認沒有此文件,手動創建
Host * StrictHostKeyChecking no
默認StrictHostKeyChecking ask