Loading... # Windows 启用 OpenSSH Server ## 启用 OpenSSH Server 默认情况下Windows会启用OpenSSH Client,但不会默认启用OpenSSH Server,先启用相关功能。 ```powershell Get-WindowsCapability -Online | ? Name -like 'OpenSSH*' Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0 ``` 接下来将OpenSSH Server服务开启并设为配置启动启动。 ```powershell Set-Service -Name sshd -StartupType 'Automatic' Start-Service sshd ``` 配置防火墙规则,默认情况无需修改。 ```powershell Get-NetFirewallRule -Name *ssh* New-NetFirewallRule -Name sshd -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22 ``` ## 配置密钥登陆 将公钥放在`.ssh\authorized_keys`下,然后配置权限: ```powershell icacls.exe "C:\Users\username\.ssh\authorized_keys" /inheritance:r /grant "Administrators:F" /grant "SYSTEM:F" ``` 修改配置文件`C:\\ProgramData\ssh\sshd_config`: ```ini #允许公钥授权访问,确保条目不被注释 PubkeyAuthentication yes #授权文件存放位置,确保条目不被注释 AuthorizedKeysFile .ssh/authorized_keys #可选,关闭密码登录,提高安全性 PasswordAuthentication no #注释掉默认授权文件位置,确保以下条目被注释 #Match Group administrators # AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys ``` 然后重启服务。 ```powershell Restart-Service sshd ``` ## 使用 PowerShell 作为默认的 Shell ```powershell New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShell -Value "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -PropertyType String -Force ``` ## 参考 Windows OpenSSH 服务器启用密钥登录 - InsomniacWu的文章 - 知乎 https://zhuanlan.zhihu.com/p/404179039 OpenSSH Server configuration for Windows Server and Windows https://learn.microsoft.com/en-us/windows-server/administration/openssh/openssh_server_configuration Key-based authentication in OpenSSH for Windows https://learn.microsoft.com/en-us/windows-server/administration/openssh/openssh_keymanagement 最后修改:2024 年 01 月 28 日 © 允许规范转载 赞 1 如果觉得我的文章对你有用,请随意赞赏